source: osm/applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java@ 24654

Last change on this file since 24654 was 24654, checked in by extropy, 14 years ago

Pfgimport: added option to filter orthogonal shapes (buildings)

File size: 27.2 KB
Line 
1package pdfimport;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.Color;
6import java.awt.Cursor;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.GridLayout;
10import java.awt.event.ActionEvent;
11import java.awt.event.ActionListener;
12import java.awt.event.WindowAdapter;
13import java.awt.event.WindowEvent;
14import java.io.File;
15import java.io.FileInputStream;
16import java.io.FileNotFoundException;
17import java.io.FileOutputStream;
18import java.io.IOException;
19import java.util.Collection;
20import java.util.Properties;
21
22import javax.swing.BorderFactory;
23import javax.swing.JButton;
24import javax.swing.JCheckBox;
25import javax.swing.JComboBox;
26import javax.swing.JFileChooser;
27import javax.swing.JFrame;
28import javax.swing.JLabel;
29import javax.swing.JOptionPane;
30import javax.swing.JPanel;
31import javax.swing.JProgressBar;
32import javax.swing.JTextField;
33import javax.swing.SwingUtilities;
34import javax.swing.filechooser.FileFilter;
35
36import org.openstreetmap.josm.Main;
37import org.openstreetmap.josm.data.coor.EastNorth;
38import org.openstreetmap.josm.data.coor.LatLon;
39import org.openstreetmap.josm.data.osm.DataSet;
40import org.openstreetmap.josm.data.osm.Node;
41import org.openstreetmap.josm.data.osm.OsmPrimitive;
42import org.openstreetmap.josm.data.projection.Projection;
43import org.openstreetmap.josm.gui.layer.OsmDataLayer;
44import org.openstreetmap.josm.gui.progress.ProgressMonitor;
45import org.openstreetmap.josm.gui.progress.ProgressRenderer;
46import org.openstreetmap.josm.gui.progress.SwingRenderingProgressMonitor;
47import org.openstreetmap.josm.io.OsmExporter;
48
49import pdfimport.pdfbox.PdfBoxParser;
50
51public class LoadPdfDialog extends JFrame{
52
53 class LoadProgressRenderer implements ProgressRenderer{
54 private final JProgressBar pBar;
55 private String title = "";
56
57 public LoadProgressRenderer(JProgressBar pb)
58 {
59 this.pBar =pb;
60 this.pBar.setMinimum(0);
61 this.pBar.setValue(0);
62 this.pBar.setMaximum(1);
63 this.pBar.setString("");
64 this.pBar.setStringPainted(true);
65
66 }
67
68 public void setCustomText(String message) {
69 this.pBar.setString(this.title + message);
70 }
71
72 public void setIndeterminate(boolean indeterminate) {
73 this.pBar.setIndeterminate(indeterminate);
74 }
75
76 public void setMaximum(int maximum) {
77 this.pBar.setMaximum(maximum);
78 }
79
80 public void setTaskTitle(String taskTitle) {
81 this.title = taskTitle;
82 this.pBar.setString(this.title);
83 }
84
85 public void setValue(int value) {
86 this.pBar.setValue(value);
87 }
88
89 public void finish() {
90 this.pBar.setString(tr("Finished"));
91 this.pBar.setValue(this.pBar.getMaximum());
92 }
93
94 }
95
96 private File fileName;
97 private PathOptimizer data;
98 private OsmDataLayer layer;
99
100 /**
101 * Combobox with all projections available
102 */
103 private JComboBox projectionCombo;
104 private JTextField minXField;
105 private JTextField minYField;
106 private JTextField minEastField;
107 private JTextField minNorthField;
108 private JButton getMinButton;
109 private JButton okButton;
110 private JButton cancelButton;
111 private JButton getMaxButton;
112 private JTextField maxNorthField;
113 private JTextField maxEastField;
114 private JTextField maxYField;
115 private JTextField maxXField;
116 private JButton loadFileButton;
117 private JButton showButton;
118 private JButton saveButton;
119 private JCheckBox debugModeCheck;
120 private JCheckBox mergeCloseNodesCheck;
121 private JTextField mergeCloseNodesTolerance;
122 private JCheckBox removeSmallObjectsCheck;
123 private JTextField removeSmallObjectsSize;
124 private JTextField colorFilterColor;
125 private JCheckBox colorFilterCheck;
126 private JCheckBox removeParallelSegmentsCheck;
127 private JTextField removeParallelSegmentsTolerance;
128 private JCheckBox removeLargeObjectsCheck;
129 private JTextField removeLargeObjectsSize;
130 private JProgressBar loadProgress;
131 protected OsmDataLayer newLayer;
132
133 private LoadProgressRenderer progressRenderer;
134 private JCheckBox limitPathCountCheck;
135 private JTextField limitPathCount;
136 private JCheckBox splitOnColorChangeCheck;
137 private JCheckBox splitOnShapeClosedCheck;
138 private JCheckBox splitOnSingleSegmentCheck;
139 private JCheckBox splitOnOrthogonalCheck;
140
141
142 public LoadPdfDialog() {
143 this.buildGUI();
144 FilePlacement pl = new FilePlacement();
145 this.setPlacement(pl);
146 this.addListeners();
147 this.removeLayer();
148 }
149
150 private void addListeners() {
151
152 this.loadFileButton.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent e) {
154 loadFilePressed();
155 }
156 });
157
158 this.okButton.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
160 okPressed();
161 }
162 });
163
164 this.saveButton.addActionListener(new ActionListener() {
165 public void actionPerformed(ActionEvent e) {
166 savePressed();
167 }
168 });
169
170 this.showButton.addActionListener(new ActionListener() {
171 public void actionPerformed(ActionEvent e) {
172 showPressed();
173 }
174 });
175
176 this.cancelButton.addActionListener(new ActionListener() {
177 public void actionPerformed(ActionEvent e) {
178 cancelPressed();
179 }
180 });
181
182 this.addWindowListener(new WindowAdapter()
183 {
184 @Override
185 public void windowClosing(WindowEvent e) {
186 cancelPressed();
187 }
188 });
189
190 this.getMinButton.addActionListener(new ActionListener() {
191 public void actionPerformed(ActionEvent e) {
192 getMinPressed();
193 }
194 });
195
196 this.getMaxButton.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent e) {
198 getMaxPressed();
199 }
200 });
201
202 }
203
204 private void buildGUI() {
205 GridBagConstraints c = new GridBagConstraints();
206 c.gridheight = 1;c.gridwidth = 1;c.weightx =1; c.weighty = 1; c.fill = GridBagConstraints.BOTH;
207
208 this.projectionCombo = new JComboBox();
209 this.projectionCombo.addItem("Select projection...");
210 for (Projection p: Projection.allProjections) {
211 this.projectionCombo.addItem(p);
212 }
213
214 this.loadFileButton = new JButton(tr("Load file..."));
215 this.okButton = new JButton(tr("Place"));
216 this.saveButton = new JButton(tr("Save"));
217 this.showButton = new JButton(tr("Show target"));
218 this.cancelButton = new JButton(tr("Discard"));
219 this.loadProgress = new JProgressBar();
220 this.progressRenderer = new LoadProgressRenderer(this.loadProgress);
221
222 this.minXField = new JTextField("");
223 this.minYField = new JTextField("");
224 this.minEastField = new JTextField("");
225 this.minNorthField = new JTextField("");
226 this.getMinButton = new JButton(tr("Take X and Y from selected node"));
227
228 this.maxXField = new JTextField("");
229 this.maxYField = new JTextField("");
230 this.maxEastField = new JTextField("");
231 this.maxNorthField = new JTextField("");
232 this.getMaxButton = new JButton(tr("Take X and Y from selected node"));
233
234 this.debugModeCheck = new JCheckBox(tr("Debug info"));
235 this.mergeCloseNodesCheck = new JCheckBox(tr("Merge close nodes"));
236 this.mergeCloseNodesTolerance = new JTextField("1e-3");
237
238 this.removeSmallObjectsCheck = new JCheckBox(tr("Remove objects smaller than"));
239 this.removeSmallObjectsSize = new JTextField("1");
240
241 this.removeLargeObjectsCheck = new JCheckBox(tr("Remove objects larger than"));
242 this.removeLargeObjectsSize = new JTextField("10");
243
244
245 this.colorFilterCheck = new JCheckBox(tr("Only this color"));
246 this.colorFilterColor = new JTextField("#000000");
247
248 this.removeParallelSegmentsCheck = new JCheckBox(tr("Remove parallel lines"));
249 this.removeParallelSegmentsTolerance = new JTextField("3");
250
251 this.limitPathCountCheck = new JCheckBox(tr("Take only first X paths"));
252 this.limitPathCount = new JTextField("10000");
253
254 this.splitOnColorChangeCheck = new JCheckBox(tr("Color/width change"));
255 this.splitOnShapeClosedCheck = new JCheckBox(tr("Shape closed"));
256 this.splitOnSingleSegmentCheck = new JCheckBox(tr("Single segments"));
257 this.splitOnOrthogonalCheck = new JCheckBox(tr("Orthogonal shapes"));
258
259 JPanel configPanel = new JPanel(new GridBagLayout());
260 configPanel.setBorder(BorderFactory.createTitledBorder(tr("Import settings")));
261 c.gridx = 0; c.gridy = 0; c.gridwidth = 1;
262 configPanel.add(this.mergeCloseNodesCheck, c);
263 c.gridx = 1; c.gridy = 0; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST;
264 configPanel.add(new JLabel("Tolerance :"), c);
265 c.gridx = 2; c.gridy = 0; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST;
266 configPanel.add(this.mergeCloseNodesTolerance, c);
267
268 c.gridx = 0; c.gridy = 1; c.gridwidth = 1;
269 configPanel.add(this.removeSmallObjectsCheck, c);
270 c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST;
271 configPanel.add(new JLabel("Tolerance :"), c);
272 c.gridx = 2; c.gridy = 1; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST;
273 configPanel.add(this.removeSmallObjectsSize, c);
274
275 c.gridx = 0; c.gridy = 2; c.gridwidth = 1;
276 configPanel.add(this.removeLargeObjectsCheck, c);
277 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST;
278 configPanel.add(new JLabel("Tolerance :"), c);
279 c.gridx = 2; c.gridy = 2; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST;
280 configPanel.add(this.removeLargeObjectsSize, c);
281
282 c.gridx = 0; c.gridy = 3; c.gridwidth = 1;
283 configPanel.add(this.removeParallelSegmentsCheck, c);
284 c.gridx = 1; c.gridy = 3; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST;
285 configPanel.add(new JLabel("Max distance :"), c);
286 c.gridx = 2; c.gridy = 3; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST;
287 configPanel.add(this.removeParallelSegmentsTolerance, c);
288
289
290 c.gridx = 0; c.gridy = 4; c.gridwidth = 2;
291 configPanel.add(this.limitPathCountCheck, c);
292 c.gridx = 2; c.gridy = 4; c.gridwidth = 1;
293 configPanel.add(this.limitPathCount, c);
294
295 c.gridx = 0; c.gridy = 5; c.gridwidth = 1;
296 configPanel.add(this.colorFilterCheck, c);
297 c.gridx = 2; c.gridy = 5; c.gridwidth = 1;
298 configPanel.add(this.colorFilterColor, c);
299
300 c.gridx = 0; c.gridy = 6; c.gridwidth = 2;
301 configPanel.add(this.debugModeCheck, c);
302
303
304 c.gridx = 0; c.gridy = 7; c.gridwidth = 1;
305 configPanel.add(new JLabel(tr("Introduce separate layers for:")), c);
306 c.gridx = 1; c.gridy = 7; c.gridwidth = 1;
307 configPanel.add(this.splitOnShapeClosedCheck, c);
308 c.gridx = 2; c.gridy = 7; c.gridwidth = 1;
309 configPanel.add(this.splitOnSingleSegmentCheck, c);
310 c.gridx = 1; c.gridy = 8; c.gridwidth = 1;
311 configPanel.add(this.splitOnColorChangeCheck, c);
312 c.gridx = 2; c.gridy = 8; c.gridwidth = 1;
313 configPanel.add(this.splitOnOrthogonalCheck, c);
314
315
316 JPanel projectionPanel = new JPanel(new GridBagLayout());
317 projectionPanel.setBorder(BorderFactory.createTitledBorder(tr("Bind to coordinates")));
318
319 c.gridx = 0; c.gridy = 0; c.gridwidth = 1;
320 projectionPanel.add(new JLabel(tr("Projection:")), c);
321 c.gridx = 1; c.gridy = 0; c.gridwidth = 1;
322 projectionPanel.add(this.projectionCombo);
323
324 c.gridx = 0; c.gridy = 1; c.gridwidth = 2;
325 projectionPanel.add(new JLabel(tr("Bottom left (min) corner:")), c);
326 c.gridx = 0; c.gridy = 2; c.gridwidth = 1;
327 projectionPanel.add(new JLabel(tr("PDF X and Y")), c);
328 c.gridx = 1; c.gridy = 2; c.gridwidth = 1;
329 projectionPanel.add(new JLabel(tr("East and North")), c);
330 c.gridx = 0; c.gridy = 3; c.gridwidth = 1;
331 projectionPanel.add(this.minXField, c);
332 c.gridx = 0; c.gridy = 4; c.gridwidth = 1;
333 projectionPanel.add(this.minYField, c);
334 c.gridx = 1; c.gridy = 3; c.gridwidth = 1;
335 projectionPanel.add(this.minEastField, c);
336 c.gridx = 1; c.gridy = 4; c.gridwidth = 1;
337 projectionPanel.add(this.minNorthField, c);
338 c.gridx = 0; c.gridy = 5; c.gridwidth = 1;
339 projectionPanel.add(this.getMinButton, c);
340
341
342 c.gridx = 0; c.gridy = 6; c.gridwidth = 2;
343 projectionPanel.add(new JLabel(tr("Top right (max) corner:")), c);
344 c.gridx = 0; c.gridy = 7; c.gridwidth = 1;
345 projectionPanel.add(new JLabel(tr("PDF X and Y")), c);
346 c.gridx = 1; c.gridy = 7; c.gridwidth = 1;
347 projectionPanel.add(new JLabel(tr("East and North")), c);
348 c.gridx = 0; c.gridy = 8; c.gridwidth = 1;
349 projectionPanel.add(this.maxXField, c);
350 c.gridx = 0; c.gridy = 9; c.gridwidth = 1;
351 projectionPanel.add(this.maxYField, c);
352 c.gridx = 1; c.gridy = 8; c.gridwidth = 1;
353 projectionPanel.add(this.maxEastField, c);
354 c.gridx = 1; c.gridy = 9; c.gridwidth = 1;
355 projectionPanel.add(this.maxNorthField, c);
356 c.gridx = 0; c.gridy = 10; c.gridwidth = 1;
357 projectionPanel.add(this.getMaxButton, c);
358
359
360 JPanel okCancelPanel = new JPanel(new GridLayout(1,3));
361 okCancelPanel.add(this.cancelButton);
362 okCancelPanel.add(this.showButton);
363 okCancelPanel.add(this.okButton);
364 okCancelPanel.add(this.saveButton);
365
366
367 JPanel panel = new JPanel(new GridBagLayout());
368 c.gridx = 0; c.gridy = 0; c.gridwidth = 1;
369 panel.add(configPanel, c);
370 c.gridx = 0; c.gridy = 1; c.gridwidth = 1;
371 panel.add(loadFileButton, c);
372 c.gridx = 0; c.gridy = 2; c.gridwidth = 1;
373 panel.add(projectionPanel, c);
374 c.gridx = 0; c.gridy = 3; c.gridwidth = 1;
375 panel.add(okCancelPanel, c);
376 c.gridx = 0; c.gridy = 4; c.gridwidth = 1;
377 panel.add(this.loadProgress, c);
378
379
380 this.setSize(450, 550);
381 this.setContentPane(panel);
382 }
383
384
385 private void loadFilePressed() {
386 final File newFileName = this.chooseFile();
387
388 if (newFileName == null) {
389 return;
390 }
391
392 this.removeLayer();
393
394 this.loadFileButton.setEnabled(false);
395 this.loadFileButton.setText(tr("Loading..."));
396
397 this.runAsBackgroundTask(
398 new Runnable() {
399 public void run() {
400 //async part
401 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer);
402 monitor.beginTask("Loading file", 1000);
403 data = loadPDF(newFileName, monitor.createSubTaskMonitor(500, false));
404 OsmBuilder.Mode mode = LoadPdfDialog.this.debugModeCheck.isSelected() ? OsmBuilder.Mode.Debug: OsmBuilder.Mode.Draft;
405
406 if (data!= null) {
407 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer(tr("PDF file preview"), new FilePlacement(), mode, monitor.createSubTaskMonitor(500, false));
408 }
409
410 monitor.finishTask();
411 progressRenderer.finish();
412 }
413 },
414 new ActionListener() {
415
416 public void actionPerformed(ActionEvent e) {
417 //sync part
418 if (data!= null) {
419 LoadPdfDialog.this.placeLayer(newLayer, new FilePlacement());
420 fileName = newFileName;
421 newLayer = null;
422 LoadPdfDialog.this.loadFileButton.setText(tr("Loaded"));
423 LoadPdfDialog.this.loadFileButton.setEnabled(true);
424 FilePlacement placement = LoadPdfDialog.this.loadPlacement();
425 LoadPdfDialog.this.setPlacement(placement);
426 }
427 }
428 });
429 }
430
431
432 private FilePlacement preparePlacement()
433 {
434 FilePlacement placement = this.parsePlacement();
435 if (placement == null){
436 return null;
437 }
438
439 String transformError = placement.prepareTransform();
440 if (transformError != null){
441 JOptionPane.showMessageDialog(Main.parent, transformError);
442 return null;
443 }
444
445 this.savePlacement(placement);
446
447 return placement;
448 }
449
450 private void okPressed() {
451
452 final FilePlacement placement = preparePlacement();
453 if (placement == null) {
454 return;
455 }
456
457 this.removeLayer();
458
459 this.runAsBackgroundTask(
460 new Runnable() {
461 public void run() {
462 //async part
463 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer);
464 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer(tr("Imported PDF: ") + fileName, placement, OsmBuilder.Mode.Final, monitor);
465 progressRenderer.finish();
466 }
467 },
468 new ActionListener() {
469
470 public void actionPerformed(ActionEvent e) {
471 //sync part
472 //rebuild layer with latest projection
473 LoadPdfDialog.this.placeLayer(newLayer, placement);
474 LoadPdfDialog.this.setVisible(false);
475 }
476 });
477 }
478
479 private void savePressed() {
480
481 final FilePlacement placement = preparePlacement();
482 if (placement == null) {
483 return;
484 }
485
486 final java.io.File file = this.chooseSaveFile();
487
488 if (file == null){
489 return;
490 }
491
492 this.removeLayer();
493
494 this.runAsBackgroundTask(
495 new Runnable() {
496 public void run() {
497 //async part
498 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer);
499 LoadPdfDialog.this.saveLayer(file, placement, monitor);
500 progressRenderer.finish();
501 }
502 },
503 new ActionListener() {
504
505 public void actionPerformed(ActionEvent e) {
506 //sync part
507 LoadPdfDialog.this.setVisible(false);
508 }
509 });
510 }
511
512
513 private void showPressed() {
514
515 FilePlacement placement = preparePlacement();
516 if (placement == null) {
517 return;
518 }
519
520 //zoom to new location
521 Main.map.mapView.zoomTo(placement.getWorldBounds(this.data));
522 Main.map.repaint();
523 }
524
525 private void cancelPressed() {
526 this.removeLayer();
527 this.setVisible(false);
528 }
529
530
531 private void getMinPressed() {
532 EastNorth en = this.getSelectedCoor();
533
534 if (en != null) {
535 this.minXField.setText(Double.toString(en.east()));
536 this.minYField.setText(Double.toString(en.north()));
537 }
538 }
539
540 private void getMaxPressed() {
541 EastNorth en = this.getSelectedCoor();
542
543 if (en != null) {
544 this.maxXField.setText(Double.toString(en.east()));
545 this.maxYField.setText(Double.toString(en.north()));
546 }
547 }
548
549 // Implementation methods
550
551 private EastNorth getSelectedCoor() {
552 Collection<OsmPrimitive> selected = Main.main.getCurrentDataSet().getSelected();
553
554 if (selected.size() != 1 || !(selected.iterator().next() instanceof Node)){
555 JOptionPane.showMessageDialog(Main.parent, tr("Please select exactly one node."));
556 return null;
557 }
558
559 LatLon ll = ((Node)selected.iterator().next()).getCoor();
560 FilePlacement pl = new FilePlacement();
561 return pl.reverseTransform(ll);
562 }
563
564
565 private java.io.File chooseFile() {
566 //get file name
567 JFileChooser fc = new JFileChooser();
568 fc.setAcceptAllFileFilterUsed(false);
569 fc.setMultiSelectionEnabled(false);
570 fc.setSelectedFile(this.fileName);
571 fc.setFileFilter(new FileFilter(){
572 @Override
573 public boolean accept(java.io.File pathname) {
574 return pathname.isDirectory() || pathname.getName().endsWith(".pdf");
575 }
576 @Override
577 public String getDescription() {
578 return tr("PDF files");
579 }
580 });
581 int result = fc.showOpenDialog(Main.parent);
582
583 if (result != JFileChooser.APPROVE_OPTION) {
584 return null;
585 }
586 else
587 {
588 return fc.getSelectedFile();
589 }
590 }
591
592 private java.io.File chooseSaveFile() {
593 //get file name
594 JFileChooser fc = new JFileChooser();
595 fc.setAcceptAllFileFilterUsed(true);
596 fc.setMultiSelectionEnabled(false);
597 fc.setFileFilter(new FileFilter(){
598 @Override
599 public boolean accept(java.io.File pathname) {
600 return pathname.isDirectory() || pathname.getName().endsWith(".osm");
601 }
602 @Override
603 public String getDescription() {
604 return tr("OSM files");
605 }
606 });
607 int result = fc.showOpenDialog(Main.parent);
608
609 if (result != JFileChooser.APPROVE_OPTION) {
610 return null;
611 }
612 else
613 {
614 return fc.getSelectedFile();
615 }
616 }
617
618 private void runAsBackgroundTask(final Runnable task, final ActionListener after) {
619 this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
620 Thread t = new Thread(new Runnable()
621 {
622 public void run() {
623 task.run();
624
625 SwingUtilities.invokeLater(new Runnable(){
626 public void run() {
627 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
628 after.actionPerformed(null);
629 }
630 });
631 }
632 });
633 t.start();
634 }
635
636 private PathOptimizer loadPDF(File fileName, ProgressMonitor monitor) {
637
638 monitor.beginTask(tr(""), 100);
639 monitor.setTicks(0);
640 monitor.setCustomText(tr("Preparing"));
641
642 double nodesTolerance = 0.0;
643 Color color = null;
644 int maxPaths = Integer.MAX_VALUE;
645
646 if (this.mergeCloseNodesCheck.isSelected()) {
647 try {
648 nodesTolerance = Double.parseDouble(this.mergeCloseNodesTolerance.getText());
649 }
650 catch (Exception e) {
651 JOptionPane
652 .showMessageDialog(
653 Main.parent,
654 tr("Tolerance is not a number"));
655 return null;
656 }
657 }
658
659 if (this.colorFilterCheck.isSelected()) {
660 try {
661 String colString = this.colorFilterColor.getText().replace("#", "");
662 color = new Color(Integer.parseInt(colString, 16));
663 }
664 catch (Exception e) {
665 JOptionPane
666 .showMessageDialog(
667 Main.parent,
668 tr("Could not parse color"));
669 return null;
670 }
671 }
672
673 if (this.limitPathCountCheck.isSelected()) {
674 try {
675 maxPaths = Integer.parseInt(this.limitPathCount.getText());
676 }
677 catch (Exception e) {
678 JOptionPane
679 .showMessageDialog(
680 Main.parent,
681 tr("Could not parse max path count"));
682 return null;
683 }
684 }
685
686
687 monitor.setTicks(10);
688 monitor.setCustomText(tr("Parsing file"));
689
690 PathOptimizer data = new PathOptimizer(nodesTolerance, color, this.splitOnColorChangeCheck.isSelected());
691
692 try {
693 PdfBoxParser parser = new PdfBoxParser(data);
694 parser.parse(fileName, maxPaths, monitor.createSubTaskMonitor(80, false));
695
696 } catch (FileNotFoundException e1) {
697 JOptionPane
698 .showMessageDialog(
699 Main.parent,
700 tr("File not found."));
701 return null;
702 } catch (Exception e) {
703 e.printStackTrace();
704 JOptionPane
705 .showMessageDialog(
706 Main.parent,
707 tr("Error while parsing: {0}", e.getMessage()));
708 return null;
709 }
710
711 monitor.setTicks(80);
712
713 if (this.removeParallelSegmentsCheck.isSelected()) {
714 try {
715 double tolerance = Double.parseDouble(this.removeParallelSegmentsTolerance.getText());
716 monitor.setCustomText(tr("Removing parallel segments"));
717 data.removeParallelLines(tolerance);
718 }
719 catch (Exception e) {
720 JOptionPane
721 .showMessageDialog(
722 Main.parent,
723 tr("Max distance is not a number"));
724 return null;
725 }
726 }
727
728 if (nodesTolerance > 0.0) {
729 monitor.setTicks(83);
730 monitor.setCustomText(tr("Joining nodes"));
731 data.mergeNodes();
732 }
733
734 monitor.setTicks(85);
735 monitor.setCustomText(tr("Joining adjacent segments"));
736 data.mergeSegments();
737
738 if (this.removeSmallObjectsCheck.isSelected()) {
739 try {
740 double tolerance = Double.parseDouble(this.removeSmallObjectsSize.getText());
741 monitor.setTicks(90);
742 monitor.setCustomText(tr("Removing small objects"));
743
744 data.removeSmallObjects(tolerance);
745 }
746 catch (Exception e) {
747 JOptionPane
748 .showMessageDialog(
749 Main.parent,
750 tr("Tolerance is not a number"));
751 return null;
752 }
753 }
754
755 if (this.removeLargeObjectsCheck.isSelected()) {
756 try {
757 double tolerance = Double.parseDouble(this.removeLargeObjectsSize.getText());
758 monitor.setTicks(90);
759 monitor.setCustomText(tr("Removing large objects"));
760 data.removeLargeObjects(tolerance);
761 }
762 catch (Exception e) {
763 JOptionPane
764 .showMessageDialog(
765 Main.parent,
766 tr("Tolerance is not a number"));
767 return null;
768 }
769 }
770
771 monitor.setTicks(95);
772 monitor.setCustomText(tr("Finalizing layers"));
773 data.splitLayersByPathKind(this.splitOnShapeClosedCheck.isSelected(), this.splitOnSingleSegmentCheck.isSelected(), this.splitOnOrthogonalCheck.isSelected());
774 data.finish();
775
776 monitor.finishTask();
777 return data;
778 }
779
780
781
782 private FilePlacement parsePlacement() {
783 Object selectedProjection = this.projectionCombo.getSelectedItem();
784
785 if (!(selectedProjection instanceof Projection))
786 {
787 JOptionPane.showMessageDialog(Main.parent, tr("Please set a projection."));
788 return null;
789 }
790
791 FilePlacement placement = new FilePlacement();
792
793 placement.projection = (Projection)this.projectionCombo.getSelectedItem();
794
795 try
796 {
797 placement.setPdfBounds(
798 Double.parseDouble(this.minXField.getText()),
799 Double.parseDouble(this.minYField.getText()),
800 Double.parseDouble(this.maxXField.getText()),
801 Double.parseDouble(this.maxYField.getText()));
802 placement.setEastNorthBounds(
803 Double.parseDouble(this.minEastField.getText()),
804 Double.parseDouble(this.minNorthField.getText()),
805 Double.parseDouble(this.maxEastField.getText()),
806 Double.parseDouble(this.maxNorthField.getText()));
807 }
808 catch (Exception e) {
809 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse numbers. Please check."));
810 return null;
811 }
812
813 return placement;
814 }
815
816 private void setPlacement(FilePlacement placement) {
817
818 if (placement == null) {
819 //use default values.
820 placement = new FilePlacement();
821 }
822
823 this.projectionCombo.setSelectedItem(placement.projection);
824 this.minXField.setText(Double.toString(placement.minX));
825 this.maxXField.setText(Double.toString(placement.maxX));
826 this.minYField.setText(Double.toString(placement.minY));
827 this.maxYField.setText(Double.toString(placement.maxY));
828 this.minEastField.setText(Double.toString(placement.minEast));
829 this.maxEastField.setText(Double.toString(placement.maxEast));
830 this.minNorthField.setText(Double.toString(placement.minNorth));
831 this.maxNorthField.setText(Double.toString(placement.maxNorth));
832 }
833
834
835 private FilePlacement loadPlacement() {
836 FilePlacement pl = null;
837 //load saved transformation
838 File propertiesFile = new File(fileName.getAbsoluteFile()+ ".placement");
839 try {
840
841 if (propertiesFile.exists()){
842 pl = new FilePlacement();
843 Properties p = new Properties();
844 p.load(new FileInputStream(propertiesFile));
845 pl.fromProperties(p);
846 }
847 }catch (Exception e){
848 pl = null;
849 e.printStackTrace();
850 }
851
852 return pl;
853 }
854
855 private void savePlacement(FilePlacement pl){
856 //load saved transformation
857 File propertiesFile = new File(fileName.getAbsoluteFile()+ ".placement");
858 try {
859 Properties p = pl.toProperties();
860 p.store(new FileOutputStream(propertiesFile), "PDF file placement on OSM");
861 } catch (Exception e){
862 e.printStackTrace();
863 }
864 }
865
866
867 private OsmDataLayer makeLayer(String name, FilePlacement placement, OsmBuilder.Mode mode, ProgressMonitor monitor) {
868 monitor.beginTask(tr("Building JOSM layer"), 100);
869 OsmBuilder builder = new OsmBuilder(placement);
870 DataSet data = builder.build(this.data.getLayers(), mode, monitor.createSubTaskMonitor(50, false));
871 monitor.setTicks(50);
872 monitor.setCustomText(tr("Postprocessing layer"));
873 OsmDataLayer result = new OsmDataLayer(data, name, null);
874 result.onPostLoadFromFile();
875
876 monitor.finishTask();
877 return result;
878 }
879
880 private void placeLayer(OsmDataLayer _layer, FilePlacement placement) {
881 this.removeLayer();
882 this.layer = _layer;
883 Main.main.addLayer(this.layer);
884 Main.map.mapView.zoomTo(placement.getWorldBounds(this.data));
885 }
886
887 private void removeLayer() {
888 if (this.layer != null) {
889 Main.main.removeLayer(this.layer);
890 this.layer.data.clear(); //saves memory
891 this.layer = null;
892 }
893 }
894
895 private void saveLayer(java.io.File file, FilePlacement placement, ProgressMonitor monitor) {
896 monitor.beginTask(tr("Saving to file."), 1000);
897
898 OsmBuilder builder = new OsmBuilder(placement);
899 DataSet data = builder.build(this.data.getLayers(), OsmBuilder.Mode.Final, monitor.createSubTaskMonitor(500, false));
900 OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file);
901
902 monitor.setCustomText(tr(" Writing to file"));
903 monitor.setTicks(500);
904
905 OsmExporter exporter = new OsmExporter();
906
907 try {
908 exporter.exportData(file, layer);
909 }
910 catch(IOException e){
911 //TODO:
912 }
913
914 monitor.finishTask();
915 }
916
917}
Note: See TracBrowser for help on using the repository browser.