Changeset 16311 in josm for trunk


Ignore:
Timestamp:
2020-04-15T22:40:32+02:00 (4 years ago)
Author:
simon04
Message:

fix #19041 - GeoJSONReader: reuse node on same at same position to

Avoids multiple nodes on top of each other. Avoid creating unconnected polygons.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/GeoJSONReader.java

    r15865 r16311  
    2424import org.openstreetmap.josm.data.coor.EastNorth;
    2525import org.openstreetmap.josm.data.coor.LatLon;
     26import org.openstreetmap.josm.data.osm.BBox;
    2627import org.openstreetmap.josm.data.osm.DataSet;
    2728import org.openstreetmap.josm.data.osm.Node;
     
    267268
    268269    private Node createNode(final LatLon latlon) {
     270        final List<Node> existingNodes = getDataSet().searchNodes(new BBox(latlon, latlon));
     271        if (!existingNodes.isEmpty()) {
     272            // reuse existing node, avoid multiple nodes on top of each other
     273            return existingNodes.get(0);
     274        }
    269275        final Node node = new Node(latlon);
    270276        getDataSet().addPrimitive(node);
     
    277283        }
    278284
    279         final List<LatLon> latlons = coordinates.stream().map(
    280                 coordinate -> getLatLon(coordinate.asJsonArray())).collect(Collectors.toList());
     285        final List<LatLon> latlons = coordinates.stream()
     286                .map(coordinate -> getLatLon(coordinate.asJsonArray()))
     287                .collect(Collectors.toList());
    281288
    282289        final int size = latlons.size();
     
    295302
    296303        final Way way = new Way();
    297         way.setNodes(latlons.stream().map(Node::new).collect(Collectors.toList()));
     304        getDataSet().addPrimitive(way);
     305        way.setNodes(latlons.stream().map(this::createNode).collect(Collectors.toList()));
    298306        if (doAutoclose) {
    299307            way.addNode(way.getNode(0));
    300308        }
    301 
    302         way.getNodes().stream().distinct().forEach(it -> getDataSet().addPrimitive(it));
    303         getDataSet().addPrimitive(way);
    304309
    305310        return Optional.of(way);
  • trunk/test/data/geo.json

    r15424 r16311  
    2222                    [
    2323                        102.0,
    24                         0.0
     24                        0.5
    2525                    ],
    2626                    [
  • trunk/test/unit/org/openstreetmap/josm/io/GeoJSONReaderTest.java

    r15865 r16311  
    4646                .doParseDataSet(in, null)
    4747                .getPrimitives(it -> true));
    48             assertEquals(21, primitives.size());
     48            assertEquals(20, primitives.size());
    4949
    5050            final Node node1 = new Node(new LatLon(0.5, 102.0));
     
    5656
    5757            final Way way1 = new Way();
    58             way1.addNode(new Node(new LatLon(0, 102)));
     58            way1.addNode(new Node(new LatLon(0.5, 102.0)));
    5959            way1.addNode(new Node(new LatLon(1, 103)));
    6060            way1.addNode(new Node(new LatLon(0, 104)));
     
    6666            assertEquals("valueB", foundWay1.get().get("propB"));
    6767            assertEquals("0.0", foundWay1.get().get("propB2"));
     68            assertEquals(foundNode1.get(), ((Way) foundWay1.get()).firstNode());
     69            assertEquals("valueA", ((Way) foundWay1.get()).firstNode().get("propA"));
    6870
    6971            final Way way2 = new Way();
Note: See TracChangeset for help on using the changeset viewer.