Changeset 24056 in osm for applications


Ignore:
Timestamp:
2010-11-04T11:48:42+01:00 (14 years ago)
Author:
extropy
Message:

pdfimport: added options at import.

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  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import java.awt.Color;
    56import java.awt.Cursor;
    67import java.awt.GridBagConstraints;
     
    1213import java.awt.event.WindowEvent;
    1314import java.io.File;
     15import java.io.FileInputStream;
    1416import java.io.FileNotFoundException;
    1517import java.io.IOException;
    1618import java.util.Collection;
     19import java.util.Properties;
    1720
    1821import javax.swing.BorderFactory;
     
    4548        private String fileName;
    4649        private PathOptimizer data;
    47         private final OsmBuilder builder;
     50        private final FilePlacement placement;
    4851        private OsmDataLayer layer;
    4952
     
    7073        private JCheckBox mergeCloseNodesCheck;
    7174        private JTextField mergeCloseNodesTolerance;
     75        private JCheckBox removeSmallObjectsCheck;
     76        private JTextField removeSmallObjectsSize;
     77        private JTextField colorFilterColor;
     78        private JCheckBox colorFilterCheck;
    7279
    7380        public LoadPdfDialog() {
    7481
    75                 this.builder = new OsmBuilder();
     82                this.placement = new FilePlacement();
    7683
    7784                this.buildGUI();
     
    150157                this.cancelButton = new JButton(tr("Discard"));
    151158
    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);
    156163                this.getMinButton = new JButton(tr("Take X and Y from selected node"));
    157164
    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);
    162169                this.getMaxButton = new JButton(tr("Take X and Y from selected node"));
    163170
    164171                this.debugModeCheck = new JCheckBox(tr("Debug info"));
    165172                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");
    167180
    168181                JPanel configPanel = new JPanel(new GridBagLayout());
     182                configPanel.setBorder(BorderFactory.createTitledBorder(tr("Import settings")));
    169183                c.gridx = 0; c.gridy = 0; c.gridwidth = 1;
    170184                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;
    172188                configPanel.add(this.mergeCloseNodesTolerance, c);
    173189
    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;
    175203                configPanel.add(this.debugModeCheck, c);
    176204
     
    241269                panel.add(okCancelPanel, c);
    242270
    243                 this.setSize(400, 400);
     271                this.setSize(400, 500);
    244272                this.setContentPane(panel);
    245273        }
     
    316344                }
    317345
     346                OsmBuilder builder = new OsmBuilder(this.placement);
     347
    318348                //zoom to new location
    319349                Main.map.mapView.zoomTo(builder.getWorldBounds(this.data));
     
    356386
    357387                LatLon ll = ((Node)selected.iterator().next()).getCoor();
    358                 return this.builder.reverseTransform(ll);
     388                return this.placement.reverseTransform(ll);
    359389        }
    360390
     
    453483                }
    454484
     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
    455501                if (this.mergeCloseNodesCheck.isSelected()) {
    456502                        try {
     
    467513                }
    468514
    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
    470549                return data;
    471550        }
     551
     552
    472553
    473554        private boolean loadTransformation() {
     
    480561                }
    481562
    482                 this.builder.projection = (Projection)this.projectionCombo.getSelectedItem();
     563                this.placement.projection = (Projection)this.projectionCombo.getSelectedItem();
    483564
    484565                try
    485566                {
    486                         this.builder.setPdfBounds(
     567                        this.placement.setPdfBounds(
    487568                                        Double.parseDouble(this.minXField.getText()),
    488569                                        Double.parseDouble(this.minYField.getText()),
    489570                                        Double.parseDouble(this.maxXField.getText()),
    490571                                        Double.parseDouble(this.maxYField.getText()));
    491                         this.builder.setEastNorthBounds(
     572                        this.placement.setEastNorthBounds(
    492573                                        Double.parseDouble(this.minEastField.getText()),
    493574                                        Double.parseDouble(this.minNorthField.getText()),
     
    503584        }
    504585
     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
    505599        private void makeLayer(String name, OsmBuilder.Mode mode) {
    506600                this.removeLayer();
    507601
    508                 if (builder == null) {
     602                if (placement == null) {
    509603                        return;
    510604                }
     605
     606                OsmBuilder builder = new OsmBuilder(this.placement);
    511607
    512608                DataSet data = builder.build(this.data.getLayers(), mode);
     
    534630
    535631        private void saveLayer(java.io.File file) {
     632                OsmBuilder builder = new OsmBuilder(this.placement);
    536633                DataSet data = builder.build(this.data.getLayers(), OsmBuilder.Mode.Final);
    537634                OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file);
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/OsmBuilder.java

    r23991 r24056  
    99
    1010import org.openstreetmap.josm.data.Bounds;
    11 import org.openstreetmap.josm.data.coor.EastNorth;
    1211import org.openstreetmap.josm.data.coor.LatLon;
    1312import org.openstreetmap.josm.data.osm.DataSet;
     
    1615import org.openstreetmap.josm.data.osm.RelationMember;
    1716import org.openstreetmap.josm.data.osm.Way;
    18 import org.openstreetmap.josm.data.projection.Projection;
    1917
    2018public class OsmBuilder {
     
    2220        enum Mode {Draft, Final, Debug};
    2321
    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;
    3423
    3524        private String layerName;
     
    3928
    4029
    41         public OsmBuilder()
     30        public OsmBuilder(FilePlacement placement)
    4231        {
    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;
    5733        }
    5834
    5935
    6036        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());
    6339                return new Bounds(min, max);
    6440        }
     
    8662                for(Point2D pt: layer.points) {
    8763                        Node node = new Node();
    88                         node.setCoor(this.tranformCoords(pt.getX(), pt.getY()));
     64                        node.setCoor(this.placement.tranformCoords(pt.getX(), pt.getY()));
    8965
    9066                        target.addPrimitive(node);
     
    199175                return "#" + s;
    200176        }
    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         }
    223177}
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java

    r24054 r24056  
    11package pdfimport;
    22
     3import java.awt.Color;
    34import java.awt.geom.Point2D;
    45import java.awt.geom.Rectangle2D;
     
    1819        private List<LayerContents> layers;
    1920        public Rectangle2D bounds;
    20         private final double POINT_TOLERANCE = 1e-6;
    2121
    2222        public PathOptimizer()
     
    5151                layer.multiPaths.add(p);
    5252        }
     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
    53133
    54134        private LayerContents getLayer(LayerInfo info) {
     
    72152
    73153
    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 
    117154        private void finalizeLayer(LayerContents layer){
    118155                Set<Point2D> points = new HashSet<Point2D>();
     
    189226                return newPoints;
    190227        }
     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
    191273
    192274        /**
Note: See TracChangeset for help on using the changeset viewer.