Changeset 24189 in osm for applications/editors
- Timestamp:
- 2010-11-11T11:30:42+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/pdfimport/src/pdfimport
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pdfimport/src/pdfimport/FilePlacement.java
r24186 r24189 7 7 import java.util.Properties; 8 8 9 import org.openstreetmap.josm.data.Bounds; 9 10 import org.openstreetmap.josm.data.coor.EastNorth; 10 11 import org.openstreetmap.josm.data.coor.LatLon; … … 22 23 public double minNorth = 0; 23 24 public double maxNorth = 10000; 24 25 25 26 private AffineTransform transform; 26 27 … … 38 39 this.minNorth = minNorth; 39 40 this.maxNorth = maxNorth; 40 } 41 } 41 42 42 43 public Properties toProperties() { … … 98 99 } 99 100 100 101 101 102 public String prepareTransform() 102 103 { 103 if (this.minX > =this.maxX){104 if (this.minX > this.maxX){ 104 105 return tr("Transform error: Min X must be smaller than max"); 105 106 } 106 107 107 if (this.minY > =this.maxY){108 if (this.minY > this.maxY){ 108 109 return tr("Transform error: Min Y must be smaller than max"); 109 110 } 110 111 111 112 if (Math.abs(this.minY - this.maxY) < 1 && 113 Math.abs(this.minX - this.maxX) < 1) 114 { 115 return tr("Transform error: Points too close"); 116 } 117 else if (Math.abs(this.minX - this.maxX) < 1){ 118 //x axis equal, assume same scale in both dimensions 119 if (this.minEast == this.maxEast){ 120 //no rotation 121 this.maxX = this.minX + this.maxY - this.minY; 122 this.maxEast = this.minEast + this.maxNorth - this.minNorth; 123 } else if (this.minNorth == this.maxNorth) { 124 //needs rotated 90 degrees clockwise, or counter 125 this.maxX = this.minX + this.maxY - this.minY; 126 this.maxNorth = this.minNorth - (this.maxEast - this.minEast); 127 } else { 128 return tr("Transform error: Unsupported variant."); 129 } 130 } else if (Math.abs(this.minY - this.maxY) < 1) { 131 //Y axis equal, assume same scale in both dimensions 132 if (this.minNorth == this.maxNorth){ 133 //no rotation 134 this.maxY = this.minY + this.maxX - this.minX; 135 this.maxNorth = this.minNorth + this.maxEast - this.minEast; 136 } else if (this.minEast == this.maxEast){ 137 //needs rotated 90 degrees clockwise, or counter 138 this.maxY = this.minY + this.maxX - this.minX; 139 this.maxEast = this.minEast - (this.maxNorth - this.minNorth); 140 } else { 141 return tr("Transform error: Unsupported variant."); 142 } 143 } else { 144 return tr("Transform error: Unsupported variant."); 145 } 146 147 112 148 if (this.minEast < this.maxEast && this.minNorth < this.maxNorth) { 113 149 //no rotation 114 150 this.transform = new AffineTransform(); 115 this.transform.translate( -this.minX, -this.minY);151 this.transform.translate(this.minEast, this.minNorth); 116 152 this.transform.scale( 117 153 (this.maxEast - this.minEast) / (this.maxX - this.minX), 118 154 (this.maxNorth - this.minNorth) / (this.maxY - this.minY)); 119 this.transform.translate( this.minEast, this.minNorth);155 this.transform.translate(-this.minX, -this.minY); 120 156 } else if (this.minEast < this.maxEast && this.minNorth > this.maxNorth) { 121 157 //need to rotate 90 degrees counterclockwise 158 this.transform = new AffineTransform(); 122 159 //transform to 0..1, 0..1 range 123 this.transform. translate(-this.minX, -this.minY);124 this.transform. scale(1/(this.maxX - this.minX), 1/(this.minY - this.maxY));125 160 this.transform.preConcatenate(AffineTransform.getTranslateInstance(-this.minX, -this.minY)); 161 this.transform.preConcatenate(AffineTransform.getScaleInstance(1/(this.maxX - this.minX), 1/(this.minY - this.maxY))); 162 126 163 //rotate -90 degs around center 127 this.transform. quadrantRotate(-1, 0.5, 0.5);128 164 this.transform.preConcatenate(AffineTransform.getQuadrantRotateInstance(-1, 0.5, 0.5)); 165 129 166 //transform back to target range 130 this.transform. scale(167 this.transform.preConcatenate(AffineTransform.getScaleInstance( 131 168 (this.maxEast - this.minEast), 132 (this.minNorth - this.maxNorth)) ;133 this.transform. translate(this.minEast, this.maxNorth);169 (this.minNorth - this.maxNorth))); 170 this.transform.preConcatenate(AffineTransform.getTranslateInstance(this.minEast, this.maxNorth)); 134 171 } else if (this.minEast > this.maxEast && this.minNorth < this.maxNorth) { 135 172 //need to rotate 90 degrees clockwise 173 this.transform = new AffineTransform(); 136 174 //transform to 0..1, 0..1 range 137 this.transform. translate(-this.minX, -this.minY);138 this.transform. scale(1/(this.maxX - this.minX), 1/(this.maxY - this.minY));139 175 this.transform.preConcatenate(AffineTransform.getTranslateInstance(-this.minX, -this.minY)); 176 this.transform.preConcatenate(AffineTransform.getScaleInstance(1/(this.maxX - this.minX), 1/(this.maxY - this.minY))); 177 140 178 //rotate 90 degs around center 141 this.transform. quadrantRotate(1, 0.5, 0.5);142 179 this.transform.preConcatenate(AffineTransform.getQuadrantRotateInstance(1, 0.5, 0.5)); 180 143 181 //transform back to target range 144 this.transform. scale(182 this.transform.preConcatenate(AffineTransform.getScaleInstance( 145 183 (this.minEast - this.maxEast), 146 (this.maxNorth - this.minNorth)) ;147 this.transform. translate(this.maxEast, this.minNorth);148 } 184 (this.maxNorth - this.minNorth))); 185 this.transform.preConcatenate(AffineTransform.getTranslateInstance(this.maxEast, this.minNorth)); 186 } 149 187 else 150 188 { 151 189 return tr("Transform error: Unsupported orientation"); 152 190 } 153 191 154 192 return null; 155 156 } 157 193 194 } 195 158 196 EastNorth en = new EastNorth(0, 0); 159 197 Point2D src = new Point2D.Double(); 198 199 200 public Bounds getWorldBounds(PathOptimizer data) { 201 LatLon min = this.tranformCoords(new Point2D.Double(data.bounds.getMinX(), data.bounds.getMinY())); 202 LatLon max = this.tranformCoords(new Point2D.Double(data.bounds.getMaxX(), data.bounds.getMaxY())); 203 return new Bounds(min, max); 204 } 160 205 161 206 public LatLon tranformCoords(Point2D pt) { -
applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java
r24186 r24189 29 29 import javax.swing.JOptionPane; 30 30 import javax.swing.JPanel; 31 import javax.swing.JProgressBar; 31 32 import javax.swing.JTextField; 32 33 import javax.swing.SwingUtilities; … … 41 42 import org.openstreetmap.josm.data.projection.Projection; 42 43 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 44 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 45 import org.openstreetmap.josm.gui.progress.ProgressRenderer; 46 import org.openstreetmap.josm.gui.progress.SwingRenderingProgressMonitor; 43 47 import org.openstreetmap.josm.io.OsmExporter; 44 48 45 49 import pdfimport.pdfbox.PdfBoxParser; 46 50 47 public class LoadPdfDialog extends JFrame { 51 public 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 } 48 95 49 96 private File fileName; 50 97 private PathOptimizer data; 51 private final FilePlacement placement;52 98 private OsmDataLayer layer; 53 99 … … 82 128 private JCheckBox removeLargeObjectsCheck; 83 129 private JTextField removeLargeObjectsSize; 130 private JProgressBar loadProgress; 131 protected OsmDataLayer newLayer; 132 133 private LoadProgressRenderer progressRenderer; 134 84 135 85 136 public LoadPdfDialog() { 86 87 this.placement = new FilePlacement();88 89 137 this.buildGUI(); 138 FilePlacement pl = new FilePlacement(); 139 this.setPlacement(pl); 90 140 this.addListeners(); 91 141 this.removeLayer(); … … 161 211 this.showButton = new JButton(tr("Show target")); 162 212 this.cancelButton = new JButton(tr("Discard")); 163 164 this.minXField = new JTextField(""+this.placement.minX); 165 this.minYField = new JTextField(""+this.placement.minY); 166 this.minEastField = new JTextField(""+this.placement.minEast); 167 this.minNorthField = new JTextField(""+this.placement.minNorth); 213 this.loadProgress = new JProgressBar(); 214 this.progressRenderer = new LoadProgressRenderer(this.loadProgress); 215 216 this.minXField = new JTextField(""); 217 this.minYField = new JTextField(""); 218 this.minEastField = new JTextField(""); 219 this.minNorthField = new JTextField(""); 168 220 this.getMinButton = new JButton(tr("Take X and Y from selected node")); 169 221 170 this.maxXField = new JTextField("" +this.placement.maxX);171 this.maxYField = new JTextField("" +this.placement.maxY);172 this.maxEastField = new JTextField("" +this.placement.maxEast);173 this.maxNorthField = new JTextField("" +this.placement.maxNorth);222 this.maxXField = new JTextField(""); 223 this.maxYField = new JTextField(""); 224 this.maxEastField = new JTextField(""); 225 this.maxNorthField = new JTextField(""); 174 226 this.getMaxButton = new JButton(tr("Take X and Y from selected node")); 175 227 … … 235 287 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; 236 288 selectFilePanel.add(this.loadFileButton, c); 237 238 289 239 290 JPanel projectionPanel = new JPanel(new GridBagLayout()); … … 280 331 projectionPanel.add(this.getMaxButton, c); 281 332 333 282 334 JPanel okCancelPanel = new JPanel(new GridLayout(1,3)); 283 335 okCancelPanel.add(this.cancelButton); … … 285 337 okCancelPanel.add(this.okButton); 286 338 okCancelPanel.add(this.saveButton); 339 287 340 288 341 JPanel panel = new JPanel(new GridBagLayout()); … … 295 348 c.gridx = 0; c.gridy = 3; c.gridwidth = 1; 296 349 panel.add(okCancelPanel, c); 297 298 this.setSize(400, 520); 350 c.gridx = 0; c.gridy = 4; c.gridwidth = 1; 351 panel.add(this.loadProgress, c); 352 353 354 this.setSize(450, 550); 299 355 this.setContentPane(panel); 300 356 } … … 316 372 new Runnable() { 317 373 public void run() { 318 data = loadPDF(newFileName); 374 //async part 375 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 376 monitor.beginTask("Loading file", 1000); 377 data = loadPDF(newFileName, monitor.createSubTaskMonitor(500, false)); 378 OsmBuilder.Mode mode = LoadPdfDialog.this.debugModeCheck.isSelected() ? OsmBuilder.Mode.Debug: OsmBuilder.Mode.Draft; 379 380 if (data!= null) { 381 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer(tr("PDF file preview"), new FilePlacement(), mode, monitor.createSubTaskMonitor(500, false)); 382 } 383 384 monitor.finishTask(); 385 progressRenderer.finish(); 319 386 } 320 387 }, … … 322 389 323 390 public void actionPerformed(ActionEvent e) { 391 //sync part 324 392 if (data!= null) { 325 OsmBuilder.Mode mode = LoadPdfDialog.this.debugModeCheck.isSelected() ? OsmBuilder.Mode.Debug: OsmBuilder.Mode.Draft;326 LoadPdfDialog.this.fileName = newFileName;327 LoadPdfDialog.this.makeLayer(tr("PDF file preview"), new FilePlacement(), mode);393 LoadPdfDialog.this.placeLayer(newLayer, new FilePlacement()); 394 fileName = newFileName; 395 newLayer = null; 328 396 LoadPdfDialog.this.loadFileButton.setText(tr("Loaded")); 329 397 LoadPdfDialog.this.loadFileButton.setEnabled(true); 330 LoadPdfDialog.this.loadPlacement();331 LoadPdfDialog.this.setPlacement( );398 FilePlacement placement = LoadPdfDialog.this.loadPlacement(); 399 LoadPdfDialog.this.setPlacement(placement); 332 400 } 333 401 } … … 335 403 } 336 404 337 338 private booleanpreparePlacement()405 406 private FilePlacement preparePlacement() 339 407 { 340 boolean ok = this.parsePlacement(); 341 if (!ok){ 342 JOptionPane.showMessageDialog(Main.parent, tr("Problems parsing placement.")); 343 return false; 344 } 345 346 String transformError = this.placement.prepareTransform(); 408 FilePlacement placement = this.parsePlacement(); 409 if (placement == null){ 410 return null; 411 } 412 413 String transformError = placement.prepareTransform(); 347 414 if (transformError != null){ 348 JOptionPane.showMessageDialog(Main.parent, transformError); 349 } 350 351 this.savePlacement(); 352 353 return true; 415 JOptionPane.showMessageDialog(Main.parent, transformError); 416 return null; 417 } 418 419 this.savePlacement(placement); 420 421 return placement; 354 422 } 355 423 356 424 private void okPressed() { 357 425 358 if (!preparePlacement()) { 426 final FilePlacement placement = preparePlacement(); 427 if (placement == null) { 359 428 return; 360 429 } 361 430 362 //rebuild layer with latest projection 363 this.makeLayer(tr("Imported PDF: ") + this.fileName, this.placement, OsmBuilder.Mode.Final); 364 365 this.setVisible(false); 431 this.removeLayer(); 432 433 this.runAsBackgroundTask( 434 new Runnable() { 435 public void run() { 436 //async part 437 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 438 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer(tr("Imported PDF: ") + fileName, placement, OsmBuilder.Mode.Final, monitor); 439 progressRenderer.finish(); 440 } 441 }, 442 new ActionListener() { 443 444 public void actionPerformed(ActionEvent e) { 445 //sync part 446 //rebuild layer with latest projection 447 LoadPdfDialog.this.placeLayer(newLayer, placement); 448 LoadPdfDialog.this.setVisible(false); 449 } 450 }); 366 451 } 367 452 368 453 private void savePressed() { 369 454 370 if (!preparePlacement()) { 455 final FilePlacement placement = preparePlacement(); 456 if (placement == null) { 371 457 return; 372 } 373 374 java.io.File file = this.chooseSaveFile();458 } 459 460 final java.io.File file = this.chooseSaveFile(); 375 461 376 462 if (file == null){ 377 463 return; 378 } 379 380 //rebuild layer with latest projection 464 } 465 381 466 this.removeLayer(); 382 467 383 384 this.saveLayer(file); 385 this.setVisible(false); 468 this.runAsBackgroundTask( 469 new Runnable() { 470 public void run() { 471 //async part 472 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 473 LoadPdfDialog.this.saveLayer(file, placement, monitor); 474 progressRenderer.finish(); 475 } 476 }, 477 new ActionListener() { 478 479 public void actionPerformed(ActionEvent e) { 480 //sync part 481 LoadPdfDialog.this.setVisible(false); 482 } 483 }); 386 484 } 387 485 … … 389 487 private void showPressed() { 390 488 391 boolean ok = this.parsePlacement();392 if ( !ok){489 FilePlacement placement = preparePlacement(); 490 if (placement == null) { 393 491 return; 394 492 } 395 493 396 OsmBuilder builder = new OsmBuilder(this.placement);397 398 494 //zoom to new location 399 Main.map.mapView.zoomTo( builder.getWorldBounds(this.data));495 Main.map.mapView.zoomTo(placement.getWorldBounds(this.data)); 400 496 Main.map.repaint(); 401 497 } … … 511 607 } 512 608 513 private PathOptimizer loadPDF(File fileName) { 514 515 PathOptimizer data = new PathOptimizer(); 609 private PathOptimizer loadPDF(File fileName, ProgressMonitor monitor) { 610 611 monitor.beginTask(tr(""), 100); 612 monitor.setTicks(0); 613 monitor.setCustomText(tr("Preparing")); 614 615 double nodesTolerance = 0.0; 616 Color color = null; 617 618 if (this.mergeCloseNodesCheck.isSelected()) { 619 try { 620 nodesTolerance = Double.parseDouble(this.mergeCloseNodesTolerance.getText()); 621 } 622 catch (Exception e) { 623 JOptionPane 624 .showMessageDialog( 625 Main.parent, 626 tr("Tolerance is not a number")); 627 return null; 628 } 629 } 630 631 if (this.colorFilterCheck.isSelected()) { 632 try { 633 String colString = this.colorFilterColor.getText().replace("#", ""); 634 color = new Color(Integer.parseInt(colString, 16)); 635 } 636 catch (Exception e) { 637 JOptionPane 638 .showMessageDialog( 639 Main.parent, 640 tr("Could not parse color")); 641 return null; 642 } 643 } 644 645 646 monitor.setTicks(10); 647 monitor.setCustomText(tr("Parsing file")); 648 649 PathOptimizer data = new PathOptimizer(nodesTolerance, color); 516 650 517 651 try { 518 652 PdfBoxParser parser = new PdfBoxParser(data); 519 parser.parse(fileName );653 parser.parse(fileName, monitor.createSubTaskMonitor(80, false)); 520 654 521 655 } catch (FileNotFoundException e1) { … … 534 668 } 535 669 536 if (this.colorFilterCheck.isSelected()) { 537 try { 538 String colString = this.colorFilterColor.getText().replace("#", ""); 539 Color color = new Color(Integer.parseInt(colString, 16)); 540 data.filterByColor(color); 541 } 542 catch (Exception e) { 543 JOptionPane 544 .showMessageDialog( 545 Main.parent, 546 tr("Could not parse color")); 547 return null; 548 } 549 } 550 670 monitor.setTicks(80); 551 671 552 672 if (this.removeParallelSegmentsCheck.isSelected()) { 553 673 try { 554 674 double tolerance = Double.parseDouble(this.removeParallelSegmentsTolerance.getText()); 675 monitor.setCustomText(tr("Removing parallel segments")); 555 676 data.removeParallelLines(tolerance); 556 677 } … … 564 685 } 565 686 566 if (this.mergeCloseNodesCheck.isSelected()) { 687 monitor.setTicks(85); 688 monitor.setCustomText(tr("Joining adjacent segments")); 689 data.mergeSegments(); 690 691 if (this.removeSmallObjectsCheck.isSelected()) { 567 692 try { 568 double tolerance = Double.parseDouble(this.mergeCloseNodesTolerance.getText()); 569 data.mergeNodes(tolerance); 693 double tolerance = Double.parseDouble(this.removeSmallObjectsSize.getText()); 694 monitor.setTicks(90); 695 monitor.setCustomText(tr("Removing small objects")); 696 697 data.removeSmallObjects(tolerance); 570 698 } 571 699 catch (Exception e) { … … 578 706 } 579 707 580 data.mergeSegments(); 581 582 if (this.removeSmallObjectsCheck.isSelected()) { 708 if (this.removeLargeObjectsCheck.isSelected()) { 583 709 try { 584 double tolerance = Double.parseDouble(this.removeSmallObjectsSize.getText()); 585 data.removeSmallObjects(tolerance); 710 double tolerance = Double.parseDouble(this.removeLargeObjectsSize.getText()); 711 monitor.setTicks(90); 712 monitor.setCustomText(tr("Removing large objects")); 713 data.removeLargeObjects(tolerance); 586 714 } 587 715 catch (Exception e) { … … 594 722 } 595 723 596 597 if (this.removeLargeObjectsCheck.isSelected()) { 598 try { 599 double tolerance = Double.parseDouble(this.removeLargeObjectsSize.getText()); 600 data.removeLargeObjects(tolerance); 601 } 602 catch (Exception e) { 603 JOptionPane 604 .showMessageDialog( 605 Main.parent, 606 tr("Tolerance is not a number")); 607 return null; 608 } 609 } 610 724 monitor.setTicks(95); 725 monitor.setCustomText(tr("Finalizing layers")); 611 726 data.splitLayersByPathKind(); 612 727 data.finish(); 728 729 monitor.finishTask(); 613 730 return data; 614 731 } … … 616 733 617 734 618 private booleanparsePlacement() {735 private FilePlacement parsePlacement() { 619 736 Object selectedProjection = this.projectionCombo.getSelectedItem(); 620 737 … … 622 739 { 623 740 JOptionPane.showMessageDialog(Main.parent, tr("Please set a projection.")); 624 return false; 625 } 626 627 this.placement.projection = (Projection)this.projectionCombo.getSelectedItem(); 741 return null; 742 } 743 744 FilePlacement placement = new FilePlacement(); 745 746 placement.projection = (Projection)this.projectionCombo.getSelectedItem(); 628 747 629 748 try 630 749 { 631 this.placement.setPdfBounds(750 placement.setPdfBounds( 632 751 Double.parseDouble(this.minXField.getText()), 633 752 Double.parseDouble(this.minYField.getText()), 634 753 Double.parseDouble(this.maxXField.getText()), 635 754 Double.parseDouble(this.maxYField.getText())); 636 this.placement.setEastNorthBounds(755 placement.setEastNorthBounds( 637 756 Double.parseDouble(this.minEastField.getText()), 638 757 Double.parseDouble(this.minNorthField.getText()), … … 642 761 catch (Exception e) { 643 762 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse numbers. Please check.")); 644 return false; 645 } 646 647 return true; 648 } 649 650 private void setPlacement() { 763 return null; 764 } 765 766 return placement; 767 } 768 769 private void setPlacement(FilePlacement placement) { 770 771 if (placement == null) { 772 //use default values. 773 placement = new FilePlacement(); 774 } 651 775 652 776 this.projectionCombo.setSelectedItem(placement.projection); … … 662 786 663 787 664 private void loadPlacement() { 788 private FilePlacement loadPlacement() { 789 FilePlacement pl = null; 665 790 //load saved transformation 666 791 File propertiesFile = new File(fileName.getAbsoluteFile()+ ".placement"); … … 668 793 669 794 if (propertiesFile.exists()){ 795 pl = new FilePlacement(); 670 796 Properties p = new Properties(); 671 797 p.load(new FileInputStream(propertiesFile)); 672 this.placement.fromProperties(p);798 pl.fromProperties(p); 673 799 } 674 800 }catch (Exception e){ 801 pl = null; 675 802 e.printStackTrace(); 676 803 } 677 } 678 679 private void savePlacement(){ 804 805 return pl; 806 } 807 808 private void savePlacement(FilePlacement pl){ 680 809 //load saved transformation 681 810 File propertiesFile = new File(fileName.getAbsoluteFile()+ ".placement"); 682 811 try { 683 Properties p = this.placement.toProperties();812 Properties p = pl.toProperties(); 684 813 p.store(new FileOutputStream(propertiesFile), "PDF file placement on OSM"); 685 814 } catch (Exception e){ … … 689 818 690 819 691 private void makeLayer(String name, FilePlacement placement, OsmBuilder.Mode mode) { 820 private OsmDataLayer makeLayer(String name, FilePlacement placement, OsmBuilder.Mode mode, ProgressMonitor monitor) { 821 monitor.beginTask(tr("Building JOSM layer"), 100); 822 OsmBuilder builder = new OsmBuilder(placement); 823 DataSet data = builder.build(this.data.getLayers(), mode, monitor.createSubTaskMonitor(50, false)); 824 monitor.setTicks(50); 825 monitor.setCustomText(tr("Postprocessing layer")); 826 OsmDataLayer result = new OsmDataLayer(data, name, null); 827 result.onPostLoadFromFile(); 828 829 monitor.finishTask(); 830 return result; 831 } 832 833 private void placeLayer(OsmDataLayer _layer, FilePlacement placement) { 692 834 this.removeLayer(); 693 694 if (placement == null) { 695 return; 696 } 697 698 OsmBuilder builder = new OsmBuilder(placement); 699 700 DataSet data = builder.build(this.data.getLayers(), mode); 701 this.layer = new OsmDataLayer(data, name, null); 702 703 // Commit 704 this.layer.onPostLoadFromFile(); 835 this.layer = _layer; 705 836 Main.main.addLayer(this.layer); 706 Main.map.mapView.zoomTo(builder.getWorldBounds(this.data)); 707 708 this.okButton.setEnabled(true); 709 this.showButton.setEnabled(true); 837 Main.map.mapView.zoomTo(placement.getWorldBounds(this.data)); 710 838 } 711 839 … … 716 844 this.layer = null; 717 845 } 718 719 this.okButton.setEnabled(false); 720 this.showButton.setEnabled(false); 721 } 722 723 private void saveLayer(java.io.File file) { 724 OsmBuilder builder = new OsmBuilder(this.placement); 725 DataSet data = builder.build(this.data.getLayers(), OsmBuilder.Mode.Final); 846 } 847 848 private void saveLayer(java.io.File file, FilePlacement placement, ProgressMonitor monitor) { 849 monitor.beginTask(tr("Saving to file."), 1000); 850 851 OsmBuilder builder = new OsmBuilder(placement); 852 DataSet data = builder.build(this.data.getLayers(), OsmBuilder.Mode.Final, monitor.createSubTaskMonitor(500, false)); 726 853 OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file); 854 855 monitor.setCustomText(tr(" Writing to file")); 856 monitor.setTicks(500); 727 857 728 858 OsmExporter exporter = new OsmExporter(); … … 734 864 //TODO: 735 865 } 866 867 monitor.finishTask(); 736 868 } 737 869 -
applications/editors/josm/plugins/pdfimport/src/pdfimport/OsmBuilder.java
r24186 r24189 1 1 package pdfimport; 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 2 4 3 5 import java.awt.Color; … … 8 10 import java.util.Map; 9 11 10 import org.openstreetmap.josm.data.Bounds;11 import org.openstreetmap.josm.data.coor.LatLon;12 12 import org.openstreetmap.josm.data.osm.DataSet; 13 13 import org.openstreetmap.josm.data.osm.Node; … … 15 15 import org.openstreetmap.josm.data.osm.RelationMember; 16 16 import org.openstreetmap.josm.data.osm.Way; 17 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 17 18 18 19 public class OsmBuilder { … … 27 28 private Mode mode; 28 29 30 private ProgressMonitor monitor; 31 private int monitorPos; 32 private int monitorTotal; 29 33 30 34 public OsmBuilder(FilePlacement placement) … … 33 37 } 34 38 39 public DataSet build(List<LayerContents> data, Mode mode, ProgressMonitor monitor) { 35 40 36 public Bounds getWorldBounds(PathOptimizer data) { 37 LatLon min = placement.tranformCoords(new Point2D.Double(data.bounds.getMinX(), data.bounds.getMinY())); 38 LatLon max = placement.tranformCoords(new Point2D.Double(data.bounds.getMaxX(), data.bounds.getMaxY())); 39 return new Bounds(min, max); 40 } 41 42 public DataSet build(List<LayerContents> data, Mode mode) { 43 41 this.monitor = monitor; 42 this.monitorPos = 0; 44 43 this.mode = mode; 45 44 DataSet result = new DataSet(); 45 46 //count total items for progress monitor. 47 this.monitorTotal = 0; 48 for (LayerContents layer: data) { 49 this.monitorTotal += layer.paths.size(); 50 for(PdfMultiPath mp: layer.multiPaths){ 51 this.monitorTotal += mp.paths.size(); 52 } 53 } 54 55 monitor.beginTask(tr("Building JOSM layer."), this.monitorTotal); 56 46 57 47 58 for (LayerContents layer: data) { 48 59 this.addLayer(result, layer); 49 60 } 61 62 monitor.finishTask(); 50 63 return result; 51 64 } … … 108 121 109 122 private Way insertWay(PdfPath path, Map<Point2D, Node> point2Node, int multipathId, boolean multipolygon) { 123 124 monitor.setExtraText(tr(" "+this.monitorPos+"/"+this.monitorTotal)); 125 monitor.setTicks(this.monitorPos); 126 this.monitorPos ++; 110 127 111 128 List<Node> nodes = new ArrayList<Node>(path.points.size()); -
applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java
r24080 r24189 19 19 private List<LayerContents> layers; 20 20 public Rectangle2D bounds; 21 22 public PathOptimizer() 21 private final double pointsTolerance; 22 private final Color color; 23 24 public PathOptimizer(double _pointsTolerance, Color _color) 23 25 { 26 24 27 uniquePointMap = new HashMap<Point2D, Point2D>(); 25 28 uniquePoints = new ArrayList<Point2D>(); 26 29 layerMap = new HashMap<LayerInfo, LayerContents>(); 27 30 layers = new ArrayList<LayerContents>(); 31 pointsTolerance = _pointsTolerance; 32 color = _color; 28 33 } 29 34 … … 42 47 public void addPath(LayerInfo info, PdfPath path) 43 48 { 49 if (!isColorOK(info)){ 50 return; 51 } 52 53 if (path.points.size() > 10){ 54 int a = 10; 55 a++; 56 } 57 44 58 LayerContents layer = this.getLayer(info); 45 59 layer.paths.add(path); … … 48 62 public void addMultiPath(LayerInfo info, List<PdfPath> paths) { 49 63 64 if (!isColorOK(info)){ 65 return; 66 } 67 50 68 LayerContents layer = this.getLayer(info); 69 70 //optimize the paths 71 Set<Point2D> points = new HashSet<Point2D>(); 72 for(PdfPath path: paths) { 73 points.addAll(path.points); 74 } 75 LayerContents multipathLayer = new LayerContents(); 76 multipathLayer.paths = paths; 77 Map<Point2D, Point2D> pointMap = DuplicateNodesFinder.findDuplicateNodes(points, pointsTolerance); 78 this.fixPoints(multipathLayer,pointMap); 79 this.concatenatePaths(multipathLayer); 80 81 paths = multipathLayer.paths; 82 51 83 boolean goodMultiPath = true; 52 53 84 for(PdfPath path: paths) { 54 85 goodMultiPath &= path.isClosed(); … … 63 94 } 64 95 65 public void filterByColor(Color color) { 96 private boolean isColorOK(LayerInfo info) { 97 98 if (color == null) { 99 return true; 100 } 66 101 67 102 int rgb = color.getRGB() & 0xffffff; 68 69 List<LayerContents> newLayers = new ArrayList<LayerContents>(); 70 for(LayerContents l: this.layers) { 71 72 boolean good = false; 73 74 75 if (l.info.fill != null && (l.info.fill.getRGB() & 0xffffff) == rgb) { 76 good = true; 77 } 78 79 if (l.info.stroke != null && (l.info.stroke.getRGB() & 0xffffff) == rgb) { 80 good = true; 81 } 82 83 if (good) { 84 newLayers.add(l); 85 } 86 } 87 this.layers = newLayers; 103 boolean good = false; 104 105 if (info.fill != null && (info.fill.getRGB() & 0xffffff) == rgb) { 106 good = true; 107 } 108 109 if (info.stroke != null && (info.stroke.getRGB() & 0xffffff) == rgb) { 110 good = true; 111 } 112 113 return good; 88 114 } 89 115 … … 95 121 } 96 122 97 public void mergeNodes( double tolerance) {98 Map<Point2D, Point2D> pointMap = DuplicateNodesFinder.findDuplicateNodes(uniquePoints, tolerance);123 public void mergeNodes() { 124 Map<Point2D, Point2D> pointMap = DuplicateNodesFinder.findDuplicateNodes(uniquePoints, pointsTolerance); 99 125 100 126 for(LayerContents layer: this.layers) { … … 491 517 } 492 518 493 //construct path494 495 519 //remove from map 496 520 for (PdfPath path: pathChain) { 497 521 pathEndpoints.get(path.firstPoint()).remove(path); 498 522 pathEndpoints.get(path.lastPoint()).remove(path); 499 } 500 523 mergedPaths.add(path); 524 } 525 526 527 //construct path 501 528 PdfPath path = pathChain.get(0); 502 529 … … 507 534 throw new RuntimeException(); 508 535 } 509 510 mergedPaths.add(pathChain.get(pos));511 536 } 512 537 -
applications/editors/josm/plugins/pdfimport/src/pdfimport/pdfbox/GraphicsProcessor.java
r23991 r24189 1 1 package pdfimport.pdfbox; 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 2 4 3 5 import java.awt.BasicStroke; … … 11 13 import java.util.List; 12 14 15 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 16 13 17 import pdfimport.LayerInfo; 14 18 import pdfimport.PathOptimizer; … … 26 30 27 31 private final AffineTransform transform; 28 29 public GraphicsProcessor(PathOptimizer target, int rotation) 32 private final ProgressMonitor monitor; 33 34 public GraphicsProcessor(PathOptimizer target, int rotation, ProgressMonitor monitor) 30 35 { 31 36 this.target = target; … … 34 39 this.info.stroke = Color.BLACK; 35 40 this.info.fill = Color.BLACK; 41 this.monitor = monitor; 36 42 } 37 43 … … 48 54 if (paths.size() > 1) { 49 55 this.target.addMultiPath(this.info, paths); 56 this.parsePath(s, closed); 50 57 } 51 58 else if (paths.size() == 1) { 52 59 this.target.addPath(this.info, paths.get(0)); 53 60 } 61 62 this.monitor.setCustomText(tr(" {0} objects so far", pathNo)); 54 63 } 55 64 -
applications/editors/josm/plugins/pdfimport/src/pdfimport/pdfbox/PageDrawer.java
r23991 r24189 88 88 processStream( page, resources, page.getContents().getStream() ); 89 89 } 90 90 91 List annotations = page.getAnnotations(); 91 92 for( int i=0; i<annotations.size(); i++ ) -
applications/editors/josm/plugins/pdfimport/src/pdfimport/pdfbox/PdfBoxParser.java
r23991 r24189 1 1 package pdfimport.pdfbox; 2 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 3 4 … … 10 11 import org.apache.pdfbox.pdmodel.common.PDRectangle; 11 12 import org.apache.pdfbox.util.PDFStreamEngine; 13 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 12 14 13 15 import pdfimport.PathOptimizer; … … 21 23 22 24 @SuppressWarnings("unchecked") 23 public void parse(File file ) throws Exception25 public void parse(File file, ProgressMonitor monitor) throws Exception 24 26 { 27 monitor.beginTask(tr("Parsing PDF", 1)); 28 25 29 PDDocument document = PDDocument.load( file); 26 30 … … 43 47 } 44 48 45 GraphicsProcessor p = new GraphicsProcessor(target, rotation );49 GraphicsProcessor p = new GraphicsProcessor(target, rotation, monitor); 46 50 PageDrawer drawer = new PageDrawer(); 47 51 drawer.drawPage(p, page); 48 52 this.target.bounds = new Rectangle2D.Double(pageSize.getLowerLeftX(), pageSize.getLowerLeftY(), pageSize.getWidth(), pageSize.getHeight()); 53 54 monitor.finishTask(); 49 55 } 50 56 }
Note:
See TracChangeset
for help on using the changeset viewer.