Ignore:
Timestamp:
2010-11-05T14:48:46+01:00 (14 years ago)
Author:
extropy
Message:

PdfImport: parallel segements removal.

Location:
applications/editors/josm/plugins/pdfimport/src/pdfimport
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/FilePlacement.java

    r24056 r24080  
    9191        }
    9292
     93        EastNorth en = new EastNorth(0, 0);
     94
    9395        public LatLon tranformCoords(double x, double y) {
    9496
    9597                if (this.projection == null){
    96                         return new LatLon(y/1000, x/1000);
     98                        //en.setLocation(x * 1024,y * 1024);
     99                        //return Main.proj.eastNorth2latlon( en);
     100                        return new LatLon(y / 1000, x / 1000);
    97101                }
    98102                else{
     103
    99104                        x = (x - this.minX) * (this.maxEast - this.minEast) / (this.maxX - this.minX)  + this.minEast;
    100105                        y = (y - this.minY) * (this.maxNorth - this.minNorth) /  (this.maxY - this.minY) + this.minNorth;
    101                         return this.projection.eastNorth2latlon(new EastNorth(x, y));
     106                        en.setLocation(x,y);
     107                        return this.projection.eastNorth2latlon(en);
    102108                }
    103109        }
     
    105111        public EastNorth reverseTransform(LatLon coor) {
    106112                if (this.projection == null){
     113                        //EastNorth result = this.projection.latlon2eastNorth(coor);
     114                        //result.setLocation(result.east() / 1024, result.north() / 1024);
     115                        //return result;
    107116                        return new EastNorth(coor.lon() * 1000, coor.lat() * 1000);
    108117                }
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java

    r24060 r24080  
    7878        private JTextField colorFilterColor;
    7979        private JCheckBox colorFilterCheck;
     80        private JCheckBox removeParallelSegmentsCheck;
     81        private JTextField removeParallelSegmentsTolerance;
     82        private JCheckBox removeLargeObjectsCheck;
     83        private JTextField removeLargeObjectsSize;
    8084
    8185        public LoadPdfDialog() {
     
    177181                this.removeSmallObjectsSize = new JTextField("1");
    178182
     183                this.removeLargeObjectsCheck = new JCheckBox(tr("Remove objects larger than"));
     184                this.removeLargeObjectsSize = new JTextField("10");
     185
     186
    179187                this.colorFilterCheck = new JCheckBox(tr("Only this color"));
    180188                this.colorFilterColor = new JTextField("#000000");
     189
     190                this.removeParallelSegmentsCheck = new JCheckBox(tr("Remove parallel lines"));
     191                this.removeParallelSegmentsTolerance = new JTextField("3");
     192
    181193
    182194                JPanel configPanel = new JPanel(new GridBagLayout());
     
    197209
    198210                c.gridx = 0; c.gridy = 2; c.gridwidth = 1;
     211                configPanel.add(this.removeLargeObjectsCheck, c);
     212                c.gridx = 1; c.gridy = 2; c.gridwidth = 1; c.anchor = c.NORTHEAST;
     213                configPanel.add(new JLabel("Tolerance :"), c);
     214                c.gridx = 2; c.gridy = 2; c.gridwidth = 1; c.anchor = c.NORTHWEST;
     215                configPanel.add(this.removeLargeObjectsSize, c);
     216
     217                c.gridx = 0; c.gridy = 3; c.gridwidth = 1;
     218                configPanel.add(this.removeParallelSegmentsCheck, c);
     219                c.gridx = 1; c.gridy = 3; c.gridwidth = 1; c.anchor = c.NORTHEAST;
     220                configPanel.add(new JLabel("Max distance :"), c);
     221                c.gridx = 2; c.gridy = 3; c.gridwidth = 1; c.anchor = c.NORTHWEST;
     222                configPanel.add(this.removeParallelSegmentsTolerance, c);
     223
     224                c.gridx = 0; c.gridy = 4; c.gridwidth = 1;
    199225                configPanel.add(this.colorFilterCheck, c);
    200                 c.gridx = 2; c.gridy = 2; c.gridwidth = 1;
     226                c.gridx = 2; c.gridy = 4; c.gridwidth = 1;
    201227                configPanel.add(this.colorFilterColor, c);
    202228
    203                 c.gridx = 0; c.gridy = 3; c.gridwidth = 2;
     229                c.gridx = 0; c.gridy = 5; c.gridwidth = 2;
    204230                configPanel.add(this.debugModeCheck, c);
    205231
     
    270296                panel.add(okCancelPanel, c);
    271297
    272                 this.setSize(400, 500);
     298                this.setSize(400, 520);
    273299                this.setContentPane(panel);
    274300        }
     
    396422
    397423                LatLon ll = ((Node)selected.iterator().next()).getCoor();
    398                 return this.placement.reverseTransform(ll);
     424                FilePlacement pl = new FilePlacement();
     425                return pl.reverseTransform(ll);
    399426        }
    400427
     
    509536
    510537
     538                if (this.removeParallelSegmentsCheck.isSelected()) {
     539                        try {
     540                                double tolerance = Double.parseDouble(this.removeParallelSegmentsTolerance.getText());
     541                                data.removeParallelLines(tolerance);
     542                        }
     543                        catch (Exception e) {
     544                                JOptionPane
     545                                .showMessageDialog(
     546                                                Main.parent,
     547                                                tr("Max distance is not a number"));
     548                                return null;
     549                        }
     550                }
     551
    511552                if (this.mergeCloseNodesCheck.isSelected()) {
    512553                        try {
     
    539580                }
    540581
     582
     583                if (this.removeLargeObjectsCheck.isSelected()) {
     584                        try {
     585                                double tolerance = Double.parseDouble(this.removeLargeObjectsSize.getText());
     586                                data.removeLargeObjects(tolerance);
     587                        }
     588                        catch (Exception e) {
     589                                JOptionPane
     590                                .showMessageDialog(
     591                                                Main.parent,
     592                                                tr("Tolerance is not a number"));
     593                                return null;
     594                        }
     595                }
    541596
    542597                data.splitLayersByPathKind();
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java

    r24069 r24080  
    4747
    4848        public void addMultiPath(LayerInfo info, List<PdfPath> paths) {
     49
    4950                LayerContents layer = this.getLayer(info);
    50                 PdfMultiPath p = new PdfMultiPath(paths);
    51                 layer.multiPaths.add(p);
     51                boolean goodMultiPath = true;
     52
     53                for(PdfPath path: paths) {
     54                        goodMultiPath &= path.isClosed();
     55                }
     56
     57                if (goodMultiPath) {
     58                        PdfMultiPath p = new PdfMultiPath(paths);
     59                        layer.multiPaths.add(p);
     60                } else {
     61                        layer.paths.addAll(paths);
     62                }
    5263        }
    5364
     
    7889
    7990
     91        public void removeParallelLines(double maxDistance){
     92                for(LayerContents layer: this.layers) {
     93                        this.removeParallelLines(layer, maxDistance);
     94                }
     95        }
    8096
    8197        public void mergeNodes(double tolerance) {
     
    96112        public void removeSmallObjects(double tolerance) {
    97113                for(LayerContents layer: this.layers) {
    98                         this.removeSmallObjects(layer, tolerance);
     114                        this.removeSmallObjects(layer, tolerance, Double.POSITIVE_INFINITY);
     115                }
     116        }
     117
     118
     119        public void removeLargeObjects(double tolerance) {
     120                for(LayerContents layer: this.layers) {
     121                        this.removeSmallObjects(layer, 0.0, tolerance);
    99122                }
    100123        }
     
    232255
    233256
    234         private void removeSmallObjects(LayerContents layer, double tolerance) {
     257        private void removeSmallObjects(LayerContents layer, double min, double max) {
    235258                List<PdfPath> newPaths = new ArrayList<PdfPath>(layer.paths.size());
    236259
    237260                for(PdfPath path: layer.paths) {
    238                         boolean good = getShapeSize(path) >= tolerance;
     261                        double size = getShapeSize(path);
     262                        boolean good = size >= min && size <= max;
    239263
    240264                        if (good) {
     
    250274                        boolean good = true;
    251275                        for(PdfPath path: mp.paths) {
    252                                 good &= getShapeSize(path) >= tolerance;
     276                                double size = getShapeSize(path);
     277                                good &= size >= min && size <= max;
    253278                        }
    254279
     
    274299        }
    275300
     301
     302
     303        /***
     304         * This method finds parralel lines with similar distance and removes them.
     305         * @param layer
     306         */
     307        private void removeParallelLines(LayerContents layer, double maxDistance) {
     308                double angleTolerance = 1.0 / 180.0 * Math.PI; // 1 degree
     309                int minSegments = 10;
     310
     311                //filter paths by direction
     312                List<ParallelSegmentsFinder> angles = new ArrayList<ParallelSegmentsFinder>();
     313
     314                for(PdfPath path: layer.paths) {
     315                        if (path.points.size() != 2){
     316                                continue;
     317                        }
     318
     319                        Point2D p1 = path.firstPoint();
     320                        Point2D p2 = path.lastPoint();
     321                        double angle = Math.atan2(p2.getX() - p1.getX(), p2.getY() - p1.getY());
     322                        //normalize between 0 and 180 degrees
     323                        while (angle < 0) angle += Math.PI;
     324                        while (angle > Math.PI) angle -= Math.PI;
     325                        boolean added = false;
     326
     327                        for(ParallelSegmentsFinder pa: angles) {
     328                                if (Math.abs(pa.angle - angle) < angleTolerance){
     329                                        pa.addPath(path, angle);
     330                                        added = true;
     331                                        break;
     332                                }
     333                        }
     334
     335                        if (!added) {
     336                                ParallelSegmentsFinder pa = new ParallelSegmentsFinder();
     337                                pa.addPath(path, angle);
     338                                angles.add(pa);
     339                        }
     340                }
     341
     342                Set<PdfPath> pathsToRemove = new HashSet<PdfPath>();
     343
     344                //process each direction
     345                for (ParallelSegmentsFinder pa: angles){
     346                        if (pa.paths.size() < minSegments){
     347                                continue;
     348                        }
     349
     350                        List<ParallelSegmentsFinder> parts = pa.splitByDistance(maxDistance);
     351
     352                        for(ParallelSegmentsFinder part: parts) {
     353                                if (part.paths.size() >= minSegments){
     354                                        pathsToRemove.addAll(part.paths);
     355                                }
     356                        }
     357                }
     358
     359                //generate new path list
     360                List<PdfPath> result = new ArrayList<PdfPath>(layer.paths.size() - pathsToRemove.size());
     361
     362                for(PdfPath path: layer.paths) {
     363                        if (!pathsToRemove.contains(path)) {
     364                                result.add(path);
     365                        }
     366                }
     367
     368                layer.paths = result;
     369        }
    276370
    277371
     
    354448                                                //remove excess segments from start of chain
    355449                                                while (lastPoint != firstPoint) {
    356                                                         PdfPath pathToRemove = pathChain.remove(0);                                                     
     450                                                        PdfPath pathToRemove = pathChain.remove(0);
    357451                                                        firstPoint = pathToRemove.getOtherEnd(firstPoint);
    358452                                                }
     
    639733
    640734
     735
    641736}
Note: See TracChangeset for help on using the changeset viewer.