Changeset 23746 in osm for applications/editors
- Timestamp:
- 2010-10-22T15:51:17+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/pdfimport/src/pdfimport
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java
r23703 r23746 19 19 import java.awt.event.WindowEvent; 20 20 import java.io.FileNotFoundException; 21 import java.io.IOException; 21 22 import java.util.Collection; 22 23 … … 41 42 import org.openstreetmap.josm.data.projection.Projection; 42 43 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 44 import org.openstreetmap.josm.io.OsmExporter; 43 45 44 46 public class LoadPdfDialog extends JFrame { … … 67 69 private JButton loadFileButton; 68 70 private JButton showButton; 71 private JButton saveButton; 69 72 70 73 public LoadPdfDialog() { … … 88 91 public void actionPerformed(ActionEvent e) { 89 92 okPressed(); 93 } 94 }); 95 96 this.saveButton.addActionListener(new ActionListener() { 97 public void actionPerformed(ActionEvent e) { 98 savePressed(); 90 99 } 91 100 }); … … 137 146 this.loadFileButton = new JButton(tr("Load file...")); 138 147 this.okButton = new JButton(tr("Place")); 148 this.saveButton = new JButton(tr("Save")); 139 149 this.showButton = new JButton(tr("Show target")); 140 150 this.cancelButton = new JButton(tr("Discard")); … … 206 216 okCancelPanel.add(this.showButton); 207 217 okCancelPanel.add(this.okButton); 208 218 okCancelPanel.add(this.saveButton); 209 219 210 220 JPanel panel = new JPanel(new BorderLayout()); … … 261 271 } 262 272 273 private void savePressed() { 274 boolean ok = this.loadTransformation(); 275 if (!ok){ 276 return; 277 } 278 279 java.io.File file = this.chooseSaveFile(); 280 281 if (file == null){ 282 return; 283 } 284 285 //rebuild layer with latest projection 286 this.removeLayer(); 287 this.saveLayer(file); 288 this.setVisible(false); 289 } 290 291 263 292 private void showPressed() { 264 293 … … 325 354 public String getDescription() { 326 355 return tr("PDF files"); 356 } 357 }); 358 int result = fc.showOpenDialog(Main.parent); 359 360 if (result != JFileChooser.APPROVE_OPTION) { 361 return null; 362 } 363 else 364 { 365 return fc.getSelectedFile(); 366 } 367 } 368 369 private java.io.File chooseSaveFile() { 370 //get file name 371 JFileChooser fc = new JFileChooser(); 372 fc.setAcceptAllFileFilterUsed(true); 373 fc.setMultiSelectionEnabled(false); 374 fc.setFileFilter(new FileFilter(){ 375 @Override 376 public boolean accept(java.io.File pathname) { 377 return pathname.isDirectory() || pathname.getName().endsWith(".osm"); 378 } 379 @Override 380 public String getDescription() { 381 return tr("OSM files"); 327 382 } 328 383 }); … … 449 504 this.showButton.setEnabled(false); 450 505 } 506 507 private void saveLayer(java.io.File file) { 508 DataSet data = builder.build(this.data, true); 509 OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file); 510 511 OsmExporter exporter = new OsmExporter(); 512 513 try { 514 exporter.exportData(file, layer); 515 } 516 catch(IOException e){ 517 //TODO: 518 } 519 } 520 451 521 } -
applications/editors/josm/plugins/pdfimport/src/pdfimport/OsmBuilder.java
r23702 r23746 83 83 84 84 for (PdfPath path: layer.paths){ 85 Way w = this.insertWay(path, point2Node, -1, full); 85 Way w = this.insertWay(path, point2Node, -1, full, false); 86 86 target.addPrimitive(w); 87 87 path2Way.put(path, w); … … 91 91 for (PdfMultiPath mpath: layer.multiPaths) { 92 92 for (PdfPath path: mpath.paths){ 93 Way w = this.insertWay(path, point2Node, pathId, full); 93 Way w = this.insertWay(path, point2Node, pathId, full, true); 94 94 target.addPrimitive(w); 95 95 path2Way.put(path, w); … … 118 118 } 119 119 120 private Way insertWay(PdfPath path, Map<Point2D, Node> point2Node, int multipathId, boolean full) { 120 private Way insertWay(PdfPath path, Map<Point2D, Node> point2Node, int multipathId, boolean full, boolean multipolygon) { 121 121 122 122 List<Node> nodes = new ArrayList<Node>(path.points.size()); … … 149 149 keys.put("PDF_multipath", ""+ multipathId); 150 150 } 151 else if (path.layer.info.fill) { 151 else if (path.layer.info.fill && !multipolygon) { 152 152 keys.put("area", "yes"); 153 153 } -
applications/editors/josm/plugins/pdfimport/src/pdfimport/PDFStreamProcessor.java
r23702 r23746 3 3 import it.stefanochizzolini.clown.documents.Document; 4 4 import it.stefanochizzolini.clown.documents.contents.ContentScanner; 5 import it.stefanochizzolini.clown.documents.contents.Contents;6 5 import it.stefanochizzolini.clown.documents.contents.ContentScanner.GraphicsState; 7 6 import it.stefanochizzolini.clown.documents.contents.colorSpaces.ColorSpace; … … 19 18 import it.stefanochizzolini.clown.documents.contents.objects.FillStrokeEvenOdd; 20 19 import it.stefanochizzolini.clown.documents.contents.objects.GenericOperation; 21 import it.stefanochizzolini.clown.documents.contents.objects.LocalGraphicsState;22 import it.stefanochizzolini.clown.documents.contents.objects.ModifyCTM;23 20 import it.stefanochizzolini.clown.documents.contents.objects.Path; 24 import it.stefanochizzolini.clown.documents.contents.objects.SetLineCap;25 import it.stefanochizzolini.clown.documents.contents.objects.SetLineWidth;26 21 import it.stefanochizzolini.clown.documents.contents.objects.Stroke; 27 22 import it.stefanochizzolini.clown.documents.contents.objects.Text; … … 38 33 import java.util.Map; 39 34 40 import javax.management.RuntimeErrorException;41 42 35 public class PDFStreamProcessor { 43 36 44 private LayerInfo info; 37 private final LayerInfo info; 45 38 public Rectangle2D bounds; 46 39 int pathNo = 0; … … 53 46 54 47 public PDFStreamProcessor(Document doc) { 55 48 56 49 this.rgbSpace = new DeviceRGBColorSpace(doc); 57 50 this.graySpace = new DeviceGrayColorSpace(doc); 58 51 59 52 this.info = new LayerInfo(); 60 53 } 61 54 62 55 public void finish() { 63 56 this.rgbSpace = null; 64 57 this.graySpace = null; 65 this.state = null; 58 this.state = null; 66 59 this.optimizer.optimize(); 67 60 } 68 61 69 62 public List<LayerContents> getResult() { 70 63 return this.optimizer.getLayers(); … … 75 68 return; 76 69 77 78 ContentObject object = level.getCurrent(); 70 while(level.moveNext()) { 71 ContentObject object = level.getCurrent(); 79 72 if(object instanceof ContainerObject) { 80 73 // Scan the inner level! 81 74 process(level.getChildLevel()); 82 } 75 } 83 76 else { 84 77 addObject(level); 85 78 } 86 87 } 88 79 } 80 } 81 89 82 public void addObject(ContentScanner level){ 90 83 91 84 ContentObject obj = level.getCurrent(); 92 85 93 86 if (obj instanceof Path) 94 87 { … … 98 91 else if (obj instanceof Text){ 99 92 //maybe something here 100 } 93 } 101 94 else if (obj instanceof EndPathNoOp){ 102 95 //nothing here 103 96 this.info.divider ++; 104 } 97 } 105 98 else if (obj instanceof GenericOperation) { 106 99 this.state = level.getState(); … … 110 103 else { 111 104 int a = 10; 112 a++; 105 a++; 113 106 } 114 107 } … … 117 110 String op = go.getOperator(); 118 111 boolean parsed = true; 119 120 if (op.equals("RG")) { 112 //FIXME - currently mapping ICC colors (SCN) to device RGB. 113 114 if (op.equals("RG") || op.equals("SCN")) { 121 115 this.state.strokeColorSpace = this.rgbSpace; 122 116 this.state.strokeColor = this.rgbSpace.getColor(go.getOperands().toArray(new PdfDirectObject[3])); … … 126 120 this.state.strokeColor = this.graySpace.getColor(go.getOperands().toArray(new PdfDirectObject[3])); 127 121 } 128 else if (op.equals("rg")) { 122 else if (op.equals("rg") || op.equals("scn")) { 129 123 this.state.fillColorSpace = this.rgbSpace; 130 124 this.state.fillColor = this.rgbSpace.getColor(go.getOperands().toArray(new PdfDirectObject[3])); … … 138 132 //nothing here 139 133 int a = 10; 140 a++;141 134 a++; 142 } 143 135 a++; 136 } 137 144 138 if (parsed && setDivider) { 145 139 this.info.divider ++; … … 148 142 149 143 private void parsePath(Path path) { 150 144 151 145 List<PdfPath> paths = this.getPathNodes(path); 152 146 this.updateInfoFromState(); 153 147 154 148 for (PdfPath p: paths){ 155 149 p.nr = pathNo; 156 150 } 157 158 pathNo ++; 159 160 if (paths.size() > 1) { 151 152 pathNo ++; 153 154 if (paths.size() > 1) { 161 155 this.optimizer.addMultiPath(this.info, paths); 162 156 } … … 165 159 } 166 160 } 167 161 168 162 private List<PdfPath> getPathNodes(Path path) { 169 List<PdfPath> result = new ArrayList<PdfPath>(2); 163 List<PdfPath> result = new ArrayList<PdfPath>(2); 170 164 List<Point2D> points = new ArrayList<Point2D>(2); 171 165 this.info.fill = false; 172 166 this.info.stroke = true; 173 167 174 168 for (ContentObject obj:path.getObjects()) { 175 169 Point2D point = null; 176 170 177 171 if (obj instanceof BeginSubpath) { 178 172 if (points.size() >= 2) { … … 180 174 points = new ArrayList<Point2D>(2); 181 175 } 182 176 183 177 BeginSubpath b = (BeginSubpath)obj; 184 178 point = b.getPoint(); … … 188 182 } 189 183 else if (obj instanceof DrawCurve) { 190 191 DrawCurve c = (DrawCurve) obj; 192 point = c.getPoint(); 184 185 DrawCurve c = (DrawCurve) obj; 186 point = c.getPoint(); 193 187 } 194 188 else if (obj instanceof Stroke) { … … 223 217 result.add(new PdfPath(points)); 224 218 points = new ArrayList<Point2D>(2); 225 } 226 219 } 220 227 221 DrawRectangle r = (DrawRectangle) obj; 228 222 229 223 points.add(this.parsePoint(new Point2D.Double(r.getX(), r.getY()))); 230 224 points.add(this.parsePoint(new Point2D.Double(r.getX()+r.getWidth(), r.getY()))); … … 232 226 points.add(this.parsePoint(new Point2D.Double(r.getX(), r.getY()+r.getHeight()))); 233 227 points.add(points.get(0)); 234 result.add(new PdfPath(points)); 228 result.add(new PdfPath(points)); 235 229 points = new ArrayList<Point2D>(2); 236 230 } 237 else { 231 else { 238 232 int a = 10; 239 233 a++; 240 234 } 241 235 242 236 //add point 243 237 if (point != null) 244 { 245 boolean sameAsPrevPoint = (points.size() > 0) &&points.get(points.size() - 1).equals(point); 238 { 239 boolean sameAsPrevPoint = (points.size() > 0) &&points.get(points.size() - 1).equals(point); 246 240 if (!sameAsPrevPoint) { 247 241 points.add(this.parsePoint(point)); 248 242 } 249 } 250 } 251 243 } 244 } 245 252 246 if (points.size() >= 2) 253 247 { 254 result.add(new PdfPath(points)); 255 } 256 248 result.add(new PdfPath(points)); 249 } 250 257 251 return result; 258 } 252 } 259 253 260 254 private Point2D parsePoint(Point2D point) { … … 264 258 265 259 266 260 267 261 private void updateInfoFromState() { 268 262 this.info.color = getColor(this.state.strokeColor); 269 263 this.info.fillColor = getColor(this.state.fillColor); 270 this.info.width = this.state.lineWidth; 271 } 272 264 this.info.width = this.state.lineWidth; 265 } 266 273 267 private Color getColor( 274 268 it.stefanochizzolini.clown.documents.contents.colorSpaces.Color col) { 269 if (col == null) { 270 return Color.BLACK; 271 } 272 275 273 ColorSpace space = col.getColorSpace(); 276 274 277 275 if (space instanceof DeviceRGBColorSpace) { 278 276 return new Color( … … 289 287 else { 290 288 throw new RuntimeException("Unexpected colour space: "+space.toString()); 291 } 289 } 292 290 } 293 291 294 292 private Color addColor(GenericOperation go) { 295 List<PdfDirectObject> operands = go.getOperands(); 293 List<PdfDirectObject> operands = go.getOperands(); 296 294 PdfDirectObject o1 = operands.get(0); 297 295 PdfDirectObject o2 = operands.get(1); … … 300 298 return c; 301 299 } 302 303 300 301 304 302 private Color addGrayColor(GenericOperation go) { 305 List<PdfDirectObject> operands = go.getOperands(); 303 List<PdfDirectObject> operands = go.getOperands(); 306 304 PdfDirectObject o1 = operands.get(0); 307 305 Color c =new Color(parseFloat(o1), parseFloat(o1), parseFloat(o1)); 308 306 return c; 309 307 } 310 311 308 309 312 310 private float parseFloat(PdfDirectObject obj) { 313 311 if (obj instanceof PdfReal) { … … 320 318 return 0.0f; 321 319 } 322 } 320 } 323 321 324 322 } -
applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java
r23702 r23746 10 10 11 11 public class PathOptimizer { 12 12 13 13 public Map<Point2D, Point2D> uniquePointMap; 14 private Map<LayerInfo, LayerContents> layerMap; 14 private final Map<LayerInfo, LayerContents> layerMap; 15 15 private List<LayerContents> layers; 16 16 17 17 public PathOptimizer() 18 18 { … … 30 30 this.uniquePointMap.put(point, point); 31 31 return point; 32 } 33 } 34 32 } 33 } 34 35 35 public void addPath(LayerInfo info, PdfPath path) 36 36 { … … 38 38 layer.paths.add(path); 39 39 } 40 40 41 41 public void addMultiPath(LayerInfo info, List<PdfPath> paths) { 42 42 LayerContents layer = this.getLayer(info); 43 43 PdfMultiPath p = new PdfMultiPath(paths); 44 layer.multiPaths.add(p); 45 } 46 44 layer.multiPaths.add(p); 45 } 46 47 47 private LayerContents getLayer(LayerInfo info) { 48 48 LayerContents layer; 49 49 50 50 if (this.layerMap.containsKey(info)) 51 51 { … … 63 63 return layer; 64 64 } 65 65 66 66 public void optimize() 67 67 { 68 68 for(LayerContents layer: this.layers) { 69 this. optimizeLayer(layer);70 } 71 69 this.concatenataPaths(layer); 70 } 71 72 72 List<LayerContents> newLayers = new ArrayList<LayerContents>(); 73 73 int nr = 0; 74 74 75 75 for(LayerContents l: this.layers) { 76 76 List<LayerContents> splitResult = splitBySegmentKind(l); 77 77 78 78 for(LayerContents ll: splitResult) { 79 79 ll.info.nr = nr; … … 82 82 } 83 83 } 84 84 85 85 this.layers = newLayers; 86 86 87 87 for(LayerContents layer: this.layers) { 88 88 finalizeLayer(layer); … … 93 93 Set<Point2D> points = new HashSet<Point2D>(); 94 94 layer.points = new ArrayList<Point2D>(); 95 95 96 96 for(PdfPath pp: layer.paths){ 97 97 pp.layer = layer; 98 98 99 99 for(Point2D point: pp.points){ 100 100 if (!points.contains(point)) { … … 102 102 points.add(point); 103 103 } 104 } 105 } 106 104 } 105 } 106 107 107 for (PdfMultiPath multipath: layer.multiPaths) { 108 108 multipath.layer = layer; 109 109 for(PdfPath pp: multipath.paths){ 110 110 pp.layer = layer; 111 111 112 112 for(Point2D point: pp.points){ 113 113 if (!points.contains(point)) { … … 119 119 } 120 120 } 121 121 122 122 /** 123 * This method merges together paths with common end nodes. 123 * This method merges together paths with common end nodes. 124 124 * @param layer the layer to process. 125 125 */ 126 private void optimizeLayer(LayerContents layer) {126 private void concatenataPaths(LayerContents layer) { 127 127 Map<Point2D, PdfPath> pathEndpoints = new HashMap<Point2D, PdfPath>(); 128 Set<PdfPath> mergedPaths = new HashSet<PdfPath>(); 129 130 /* 131 //sort paths, longest first 132 List<PdfPath> sortedPaths = new ArrayList<PdfPath>(); 133 134 for(PdfPath path: layer.paths) { 135 path.calculateLength(); 136 sortedPaths.add(path); 137 } 138 139 Collections.sort(sortedPaths, new Comparator<PdfPath>(){ 140 public int compare(PdfPath o1, PdfPath o2) { 141 142 if (o1.length > o2.length) { 143 return -1; 144 } else if (o1.length < o2.length) { 145 return 1; 146 } else { 147 return 0; 148 } 149 } 150 151 }); 152 */ 153 128 Set<PdfPath> mergedPaths = new HashSet<PdfPath>(); 129 154 130 for(PdfPath pp: layer.paths){ 155 131 156 132 PdfPath path = pp; 157 133 boolean changed = true; 158 134 159 135 while (changed && !path.isClosed()) { 160 136 changed = false; 161 162 if (pathEndpoints.containsKey(path.firstPoint())) {137 138 if (pathEndpoints.containsKey(path.firstPoint())) { 163 139 PdfPath p1 = pathEndpoints.get(path.firstPoint()); 164 140 pathEndpoints.remove(p1.firstPoint()); 165 141 pathEndpoints.remove(p1.lastPoint()); 166 142 167 143 List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points); 168 path.points = newNodes; 144 path.points = newNodes; 169 145 mergedPaths.add(p1); 170 146 changed = true; 171 147 } 172 173 if (pathEndpoints.containsKey(path.lastPoint())) {148 149 if (pathEndpoints.containsKey(path.lastPoint())) { 174 150 PdfPath p1 = pathEndpoints.get(path.lastPoint()); 175 151 pathEndpoints.remove(p1.firstPoint()); 176 152 pathEndpoints.remove(p1.lastPoint()); 177 153 178 154 List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points); 179 path.points = newNodes; 155 path.points = newNodes; 180 156 mergedPaths.add(p1); 181 157 changed = true; 182 } 183 } 184 158 } 159 } 160 185 161 if (!path.isClosed()){ 186 162 pathEndpoints.put(path.firstPoint(), path); … … 188 164 } 189 165 } 190 166 191 167 List<PdfPath> resultPaths = new ArrayList<PdfPath>(); 192 168 193 169 for(PdfPath path: layer.paths) { 194 170 if (!mergedPaths.contains(path)){ … … 196 172 } 197 173 } 198 174 199 175 layer.paths = resultPaths; 200 176 } 201 177 202 178 private List<LayerContents> splitBySegmentKind(LayerContents layer) 203 179 { … … 205 181 List<PdfPath> multiSegmentPaths = new ArrayList<PdfPath>(); 206 182 List<PdfPath> closedPaths = new ArrayList<PdfPath>(); 207 183 208 184 for(PdfPath path: layer.paths) { 209 185 if (path.points.size() <= 3) { … … 217 193 } 218 194 } 219 195 220 196 List<LayerContents> layers = new ArrayList<LayerContents>(); 221 197 222 198 if (multiSegmentPaths.size() > 0) { 223 199 LayerContents l = new LayerContents(); 224 200 l.paths = multiSegmentPaths; 225 201 l.info = layer.info.copy(); 226 202 227 203 layers.add(l); 228 204 } 229 205 230 206 if (singleSegmentPaths.size() > 0) { 231 207 LayerContents l = new LayerContents(); … … 242 218 layers.add(l); 243 219 } 244 220 245 221 return layers; 246 222 } 247 248 223 224 249 225 250 226 private List<Point2D> tryMergeNodeLists(List<Point2D> nodes1, List<Point2D> nodes2) { 251 227 252 228 boolean nodes1Closed = (nodes1.get(0) == nodes1.get(nodes1.size() - 1)); 253 229 boolean nodes2Closed = (nodes2.get(0) == nodes2.get(nodes2.size() - 1)); 254 230 255 231 if (nodes1Closed || nodes2Closed) { 256 232 return null; 257 233 } 258 234 259 235 if (nodes1.get(nodes1.size() - 1) == nodes2.get(0)) { 260 236 nodes1.remove(nodes1.size() -1); … … 267 243 nodes1.add(nodes2.get(pos)); 268 244 } 269 270 return nodes1; 271 } 245 246 return nodes1; 247 } 272 248 else if (nodes1.get(0) == nodes2.get(nodes2.size() - 1)) { 273 249 nodes1.remove(0); 274 250 nodes1.addAll(0, nodes2); 275 251 return nodes1; 276 } 252 } 277 253 else if (nodes1.get(0) == nodes2.get(0)) { 278 nodes1.remove(0); 254 nodes1.remove(0); 279 255 for (int pos = 0; pos < nodes2.size(); pos ++) { 280 256 nodes1.add(0, nodes2.get(pos)); 281 257 } 282 258 283 259 return nodes1; 284 260 } else {
Note:
See TracChangeset
for help on using the changeset viewer.