Changeset 24056 in osm for applications
- Timestamp:
- 2010-11-04T11:48:42+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/pdfimport/src/pdfimport
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java
r24054 r24056 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.awt.Color; 5 6 import java.awt.Cursor; 6 7 import java.awt.GridBagConstraints; … … 12 13 import java.awt.event.WindowEvent; 13 14 import java.io.File; 15 import java.io.FileInputStream; 14 16 import java.io.FileNotFoundException; 15 17 import java.io.IOException; 16 18 import java.util.Collection; 19 import java.util.Properties; 17 20 18 21 import javax.swing.BorderFactory; … … 45 48 private String fileName; 46 49 private PathOptimizer data; 47 private final OsmBuilder builder;50 private final FilePlacement placement; 48 51 private OsmDataLayer layer; 49 52 … … 70 73 private JCheckBox mergeCloseNodesCheck; 71 74 private JTextField mergeCloseNodesTolerance; 75 private JCheckBox removeSmallObjectsCheck; 76 private JTextField removeSmallObjectsSize; 77 private JTextField colorFilterColor; 78 private JCheckBox colorFilterCheck; 72 79 73 80 public LoadPdfDialog() { 74 81 75 this. builder= newOsmBuilder();82 this.placement = new FilePlacement(); 76 83 77 84 this.buildGUI(); … … 150 157 this.cancelButton = new JButton(tr("Discard")); 151 158 152 this.minXField = new JTextField(""+this. builder.minX);153 this.minYField = new JTextField(""+this. builder.minY);154 this.minEastField = new JTextField(""+this. builder.minEast);155 this.minNorthField = new JTextField(""+this. builder.minNorth);159 this.minXField = new JTextField(""+this.placement.minX); 160 this.minYField = new JTextField(""+this.placement.minY); 161 this.minEastField = new JTextField(""+this.placement.minEast); 162 this.minNorthField = new JTextField(""+this.placement.minNorth); 156 163 this.getMinButton = new JButton(tr("Take X and Y from selected node")); 157 164 158 this.maxXField = new JTextField(""+this. builder.maxX);159 this.maxYField = new JTextField(""+this. builder.maxY);160 this.maxEastField = new JTextField(""+this. builder.maxEast);161 this.maxNorthField = new JTextField(""+this. builder.maxNorth);165 this.maxXField = new JTextField(""+this.placement.maxX); 166 this.maxYField = new JTextField(""+this.placement.maxY); 167 this.maxEastField = new JTextField(""+this.placement.maxEast); 168 this.maxNorthField = new JTextField(""+this.placement.maxNorth); 162 169 this.getMaxButton = new JButton(tr("Take X and Y from selected node")); 163 170 164 171 this.debugModeCheck = new JCheckBox(tr("Debug info")); 165 172 this.mergeCloseNodesCheck = new JCheckBox(tr("Merge close nodes")); 166 this.mergeCloseNodesTolerance = new JTextField(); 173 this.mergeCloseNodesTolerance = new JTextField("1e-6"); 174 175 this.removeSmallObjectsCheck = new JCheckBox(tr("Remove objects smaller than")); 176 this.removeSmallObjectsSize = new JTextField("1"); 177 178 this.colorFilterCheck = new JCheckBox(tr("Only this color")); 179 this.colorFilterColor = new JTextField("#000000"); 167 180 168 181 JPanel configPanel = new JPanel(new GridBagLayout()); 182 configPanel.setBorder(BorderFactory.createTitledBorder(tr("Import settings"))); 169 183 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; 170 184 configPanel.add(this.mergeCloseNodesCheck, c); 171 c.gridx = 1; c.gridy = 0; c.gridwidth = 1; 185 c.gridx = 1; c.gridy = 0; c.gridwidth = 1; c.anchor = c.NORTHEAST; 186 configPanel.add(new JLabel("Tolerance :"), c); 187 c.gridx = 2; c.gridy = 0; c.gridwidth = 1; c.anchor = c.NORTHWEST; 172 188 configPanel.add(this.mergeCloseNodesTolerance, c); 173 189 174 c.gridx = 0; c.gridy = 1; c.gridwidth = 2; 190 c.gridx = 0; c.gridy = 1; c.gridwidth = 1; 191 configPanel.add(this.removeSmallObjectsCheck, c); 192 c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.anchor = c.NORTHEAST; 193 configPanel.add(new JLabel("Tolerance :"), c); 194 c.gridx = 2; c.gridy = 1; c.gridwidth = 1; c.anchor = c.NORTHWEST; 195 configPanel.add(this.removeSmallObjectsSize, c); 196 197 c.gridx = 0; c.gridy = 2; c.gridwidth = 1; 198 configPanel.add(this.colorFilterCheck, c); 199 c.gridx = 2; c.gridy = 2; c.gridwidth = 1; 200 configPanel.add(this.colorFilterColor, c); 201 202 c.gridx = 0; c.gridy = 3; c.gridwidth = 2; 175 203 configPanel.add(this.debugModeCheck, c); 176 204 … … 241 269 panel.add(okCancelPanel, c); 242 270 243 this.setSize(400, 400);271 this.setSize(400, 500); 244 272 this.setContentPane(panel); 245 273 } … … 316 344 } 317 345 346 OsmBuilder builder = new OsmBuilder(this.placement); 347 318 348 //zoom to new location 319 349 Main.map.mapView.zoomTo(builder.getWorldBounds(this.data)); … … 356 386 357 387 LatLon ll = ((Node)selected.iterator().next()).getCoor(); 358 return this. builder.reverseTransform(ll);388 return this.placement.reverseTransform(ll); 359 389 } 360 390 … … 453 483 } 454 484 485 if (this.colorFilterCheck.isSelected()) { 486 try { 487 String colString = this.colorFilterColor.getText().replace("#", ""); 488 Color color = new Color(Integer.parseInt(colString, 16)); 489 data.filterByColor(color); 490 } 491 catch (Exception e) { 492 JOptionPane 493 .showMessageDialog( 494 Main.parent, 495 tr("Could not parse color")); 496 return null; 497 } 498 } 499 500 455 501 if (this.mergeCloseNodesCheck.isSelected()) { 456 502 try { … … 467 513 } 468 514 469 data.optimize(); 515 data.mergeSegments(); 516 517 if (this.removeSmallObjectsCheck.isSelected()) { 518 try { 519 double tolerance = Double.parseDouble(this.removeSmallObjectsSize.getText()); 520 data.removeSmallObjects(tolerance); 521 } 522 catch (Exception e) { 523 JOptionPane 524 .showMessageDialog( 525 Main.parent, 526 tr("Tolerance is not a number")); 527 return null; 528 } 529 } 530 531 532 data.splitLayersByPathKind(); 533 data.finish(); 534 535 //load saved transformation 536 File propertiesFile = new File(fileName.getAbsoluteFile()+ ".placement"); 537 try { 538 539 if (propertiesFile.exists()){ 540 Properties p = new Properties(); 541 p.load(new FileInputStream(propertiesFile)); 542 this.placement.fromProperties(p); 543 this.setTransformation(); 544 } 545 }catch (Exception e){ 546 e.printStackTrace(); 547 } 548 470 549 return data; 471 550 } 551 552 472 553 473 554 private boolean loadTransformation() { … … 480 561 } 481 562 482 this. builder.projection = (Projection)this.projectionCombo.getSelectedItem();563 this.placement.projection = (Projection)this.projectionCombo.getSelectedItem(); 483 564 484 565 try 485 566 { 486 this. builder.setPdfBounds(567 this.placement.setPdfBounds( 487 568 Double.parseDouble(this.minXField.getText()), 488 569 Double.parseDouble(this.minYField.getText()), 489 570 Double.parseDouble(this.maxXField.getText()), 490 571 Double.parseDouble(this.maxYField.getText())); 491 this. builder.setEastNorthBounds(572 this.placement.setEastNorthBounds( 492 573 Double.parseDouble(this.minEastField.getText()), 493 574 Double.parseDouble(this.minNorthField.getText()), … … 503 584 } 504 585 586 private void setTransformation() { 587 588 this.projectionCombo.setSelectedItem(placement.projection); 589 this.minXField.setText(Double.toString(placement.minX)); 590 this.maxXField.setText(Double.toString(placement.maxX)); 591 this.minYField.setText(Double.toString(placement.minY)); 592 this.maxYField.setText(Double.toString(placement.maxY)); 593 this.minEastField.setText(Double.toString(placement.minEast)); 594 this.maxEastField.setText(Double.toString(placement.maxEast)); 595 this.minNorthField.setText(Double.toString(placement.minNorth)); 596 this.maxNorthField.setText(Double.toString(placement.maxNorth)); 597 } 598 505 599 private void makeLayer(String name, OsmBuilder.Mode mode) { 506 600 this.removeLayer(); 507 601 508 if ( builder== null) {602 if (placement == null) { 509 603 return; 510 604 } 605 606 OsmBuilder builder = new OsmBuilder(this.placement); 511 607 512 608 DataSet data = builder.build(this.data.getLayers(), mode); … … 534 630 535 631 private void saveLayer(java.io.File file) { 632 OsmBuilder builder = new OsmBuilder(this.placement); 536 633 DataSet data = builder.build(this.data.getLayers(), OsmBuilder.Mode.Final); 537 634 OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file); -
applications/editors/josm/plugins/pdfimport/src/pdfimport/OsmBuilder.java
r23991 r24056 9 9 10 10 import org.openstreetmap.josm.data.Bounds; 11 import org.openstreetmap.josm.data.coor.EastNorth;12 11 import org.openstreetmap.josm.data.coor.LatLon; 13 12 import org.openstreetmap.josm.data.osm.DataSet; … … 16 15 import org.openstreetmap.josm.data.osm.RelationMember; 17 16 import org.openstreetmap.josm.data.osm.Way; 18 import org.openstreetmap.josm.data.projection.Projection;19 17 20 18 public class OsmBuilder { … … 22 20 enum Mode {Draft, Final, Debug}; 23 21 24 public Projection projection = null; 25 public double minX = 0; 26 public double maxX = 1; 27 public double minY = 0; 28 public double maxY = 1; 29 30 public double minEast = 0; 31 public double maxEast = 10000; 32 public double minNorth = 0; 33 public double maxNorth = 10000; 22 private final FilePlacement placement; 34 23 35 24 private String layerName; … … 39 28 40 29 41 public OsmBuilder() 30 public OsmBuilder(FilePlacement placement) 42 31 { 43 } 44 45 public void setPdfBounds(double minX, double minY, double maxX, double maxY){ 46 this.minX = minX; 47 this.minY = minY; 48 this.maxX = maxX; 49 this.maxY = maxY; 50 } 51 52 public void setEastNorthBounds(double minEast, double minNorth, double maxEast, double maxNorth) { 53 this.minEast = minEast; 54 this.maxEast = maxEast; 55 this.minNorth = minNorth; 56 this.maxNorth = maxNorth; 32 this.placement = placement; 57 33 } 58 34 59 35 60 36 public Bounds getWorldBounds(PathOptimizer data) { 61 LatLon min = tranformCoords(data.bounds.getMinX(), data.bounds.getMinY()); 62 LatLon max = tranformCoords(data.bounds.getMaxX(), data.bounds.getMaxY()); 37 LatLon min = placement.tranformCoords(data.bounds.getMinX(), data.bounds.getMinY()); 38 LatLon max = placement.tranformCoords(data.bounds.getMaxX(), data.bounds.getMaxY()); 63 39 return new Bounds(min, max); 64 40 } … … 86 62 for(Point2D pt: layer.points) { 87 63 Node node = new Node(); 88 node.setCoor(this.tranformCoords(pt.getX(), pt.getY())); 64 node.setCoor(this.placement.tranformCoords(pt.getX(), pt.getY())); 89 65 90 66 target.addPrimitive(node); … … 199 175 return "#" + s; 200 176 } 201 202 203 private LatLon tranformCoords(double x, double y) {204 205 if (this.projection == null){206 return new LatLon(y/1000, x/1000);207 }208 else{209 x = (x - this.minX) * (this.maxEast - this.minEast) / (this.maxX - this.minX) + this.minEast;210 y = (y - this.minY) * (this.maxNorth - this.minNorth) / (this.maxY - this.minY) + this.minNorth;211 return this.projection.eastNorth2latlon(new EastNorth(x, y));212 }213 }214 215 public EastNorth reverseTransform(LatLon coor) {216 if (this.projection == null){217 return new EastNorth(coor.lon() * 1000, coor.lat() * 1000);218 }219 else{220 return null;221 }222 }223 177 } -
applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java
r24054 r24056 1 1 package pdfimport; 2 2 3 import java.awt.Color; 3 4 import java.awt.geom.Point2D; 4 5 import java.awt.geom.Rectangle2D; … … 18 19 private List<LayerContents> layers; 19 20 public Rectangle2D bounds; 20 private final double POINT_TOLERANCE = 1e-6;21 21 22 22 public PathOptimizer() … … 51 51 layer.multiPaths.add(p); 52 52 } 53 54 public void filterByColor(Color color) { 55 List<LayerContents> newLayers = new ArrayList<LayerContents>(); 56 for(LayerContents l: this.layers) { 57 58 boolean good = false; 59 60 if (l.info.fill != null && l.info.fill.equals(color)) { 61 good = true; 62 } 63 64 if (l.info.stroke != null && l.info.stroke.equals(color)) { 65 good = true; 66 } 67 68 if (good) { 69 newLayers.add(l); 70 } 71 } 72 this.layers = newLayers; 73 } 74 75 76 77 public void mergeNodes(double tolerance) { 78 Map<Point2D, Point2D> pointMap = DuplicateNodesFinder.findDuplicateNodes(uniquePoints, tolerance); 79 80 for(LayerContents layer: this.layers) { 81 this.fixPoints(layer, pointMap); 82 } 83 } 84 85 public void mergeSegments() { 86 for(LayerContents layer: this.layers) { 87 this.concatenatePaths(layer); 88 } 89 } 90 91 92 public void removeSmallObjects(double tolerance) { 93 for(LayerContents layer: this.layers) { 94 this.removeSmallObjects(layer, tolerance); 95 } 96 } 97 98 public void splitLayersBySimilarShapes(double tolerance) { 99 List<LayerContents> newLayers = new ArrayList<LayerContents>(); 100 for(LayerContents l: this.layers) { 101 List<LayerContents> splitResult = splitBySimilarGroups(l); 102 103 for(LayerContents ll: splitResult) { 104 newLayers.add(ll); 105 } 106 } 107 this.layers = newLayers; 108 } 109 110 public void splitLayersByPathKind() { 111 List<LayerContents> newLayers = new ArrayList<LayerContents>(); 112 for(LayerContents l: this.layers) { 113 List<LayerContents> splitResult = splitBySegmentKind(l); 114 115 for(LayerContents ll: splitResult) { 116 newLayers.add(ll); 117 } 118 } 119 120 this.layers = newLayers; 121 } 122 123 124 public void finish() { 125 int nr = 0; 126 for(LayerContents layer: this.layers) { 127 layer.info.nr = nr; 128 nr++; 129 finalizeLayer(layer); 130 } 131 } 132 53 133 54 134 private LayerContents getLayer(LayerInfo info) { … … 72 152 73 153 74 public void mergeNodes(double tolerance) {75 Map<Point2D, Point2D> pointMap = DuplicateNodesFinder.findDuplicateNodes(uniquePoints, POINT_TOLERANCE);76 77 for(LayerContents layer: this.layers) {78 this.fixPoints(layer, pointMap);79 this.concatenatePaths(layer);80 }81 }82 83 public void optimize()84 {85 List<LayerContents> newLayers = new ArrayList<LayerContents>();86 /*87 for(LayerContents l: this.layers) {88 List<LayerContents> splitResult = splitBySimilarGroups(l);89 90 for(LayerContents ll: splitResult) {91 newLayers.add(ll);92 }93 }94 this.layers = newLayers;95 */96 97 98 newLayers = new ArrayList<LayerContents>();99 for(LayerContents l: this.layers) {100 List<LayerContents> splitResult = splitBySegmentKind(l);101 102 for(LayerContents ll: splitResult) {103 newLayers.add(ll);104 }105 }106 107 this.layers = newLayers;108 int nr = 0;109 for(LayerContents layer: this.layers) {110 layer.info.nr = nr;111 nr++;112 finalizeLayer(layer);113 }114 }115 116 117 154 private void finalizeLayer(LayerContents layer){ 118 155 Set<Point2D> points = new HashSet<Point2D>(); … … 189 226 return newPoints; 190 227 } 228 229 230 private void removeSmallObjects(LayerContents layer, double tolerance) { 231 List<PdfPath> newPaths = new ArrayList<PdfPath>(layer.paths.size()); 232 233 for(PdfPath path: layer.paths) { 234 boolean good = getShapeSize(path) >= tolerance; 235 236 if (good) { 237 newPaths.add(path); 238 } 239 } 240 241 layer.paths = newPaths; 242 243 List<PdfMultiPath> newMPaths = new ArrayList<PdfMultiPath>(layer.multiPaths.size()); 244 245 for (PdfMultiPath mp: layer.multiPaths){ 246 boolean good = true; 247 for(PdfPath path: mp.paths) { 248 good &= getShapeSize(path) >= tolerance; 249 } 250 251 if (good) { 252 newMPaths.add(mp); 253 } 254 } 255 256 layer.multiPaths = newMPaths; 257 } 258 259 260 private double getShapeSize(PdfPath path) { 261 262 Rectangle2D bounds = new Rectangle2D.Double(); 263 bounds.setRect(path.points.get(0).getX(), path.points.get(0).getY(), 0,0); 264 265 for(Point2D n: path.points) { 266 bounds.add(n); 267 } 268 269 return Math.max(bounds.getWidth(), bounds.getHeight()); 270 } 271 272 191 273 192 274 /**
Note:
See TracChangeset
for help on using the changeset viewer.