4

This question is similar to:

but need to do this programmatically with JTS.

This question is not for overlapping polygons or touching polygons but for adjacent polygons that is the difference when they don't touch but next to each other. the answer to this question Remove intersect/touch from polygons, to making one with JTS/GeoTools , worked for intersecting..but not for adjacent.

I am trying to implement this using JTS/GeoTools.. i found the .dissolve(..) thinking that would do the trick but it is not working.

The original GeoJson( //loadAdjacentGeoJson() )

enter image description here https://gist.github.com/boundaries-io/ef7c423f3e174de0f185cb86a1e873e3

Updated Solution:

 for (GeoJsonPolygon geoJsonPolygon : geoJsonPolygonList) {
                Coordinate[] coordinates = toCoordinateArray(geoJsonPolygon);
                SimpleFeatureBuilder simpleFeatureBuilder = new SimpleFeatureBuilder(simpleFeatureType);
                simpleFeatureBuilder.add(inflate(geometryFactory.createPolygon(coordinates)));  //add inflated polygons

                SimpleFeature simpleFeature = simpleFeatureBuilder.buildFeature(Integer.toString(index));
                simpleFeature.getUserData().put("zipcode", postal.getZipCode());
                index = index + 1;
                geometryList.add((Geometry)simpleFeature.getDefaultGeometry());
            }

        Object obj = geometryFactory.buildGeometry(geometryList);

        GeometryCollection geometryCollection = (GeometryCollection) obj;

        Geometry union = geometryCollection.union();  //perform union

        LOGGER.info(new GeoJsonWriter().write(geometryCollection)); //shows inflated polygons with overlap

        LOGGER.info(new GeoJsonWriter().write( deflate( union ))); //deflate back.

inflated polygons so union will work.: enter image description here https://gist.github.com/boundaries-io/80614499880bb4dabb57205cc529ca7c

inlate/deflate methods:

private Geometry deflate(Geometry geom) {
        BufferParameters bufferParameters = new BufferParameters();
        bufferParameters.setEndCapStyle(BufferParameters.CAP_ROUND);
        bufferParameters.setJoinStyle(BufferParameters.JOIN_MITRE);
        Geometry buffered = BufferOp.bufferOp(geom,-.0001, bufferParameters);
        buffered.setUserData(geom.getUserData());
        return buffered;
    }

    private Geometry inflate(Geometry geom) {
        BufferParameters bufferParameters = new BufferParameters();
        bufferParameters.setEndCapStyle(BufferParameters.CAP_ROUND);
        bufferParameters.setJoinStyle(BufferParameters.JOIN_MITRE);
        Geometry buffered = BufferOp.bufferOp(geom,.0001, bufferParameters);
        buffered.setUserData(geom.getUserData());
        return buffered;
    }

results: enter image description here

  • the code from the linked question will work for this problem too – Ian Turton Jan 25 '18 at 9:09
  • 1
    the solution is the same, the only difference is how you select the polygons to use for the union – Ian Turton Jan 25 '18 at 11:42
  • 1
    adjacent implies they touch so may be you could clarify the question – Ian Turton Jan 25 '18 at 11:59
  • 3
    So if they don't have any point in common, what output do you expect? A single geometry that "fills the gap", or a multi-part polygon (that would still be displayed as two geometries)? – JGH Jan 25 '18 at 12:30
  • 1
    You could expand, union, and shrink back to get about the original outer border. Or study the code of the OpenJUMP topology plugin (included in OpenJUMP Plus version) that tries to make the almost common boundaries to match. This migh be good starting point sourceforge.net/p/jump-pilot/code/HEAD/tree/plug-ins/… – user30184 Jan 25 '18 at 14:31
1

I was able to complete this by using JTS com.vividsolutions.jts.operation.buffer.BufferOp inflating, then performing a union, then deflating back.

private Geometry deflate(Geometry geom) {
        BufferParameters bufferParameters = new BufferParameters();
        bufferParameters.setEndCapStyle(BufferParameters.CAP_ROUND);
        bufferParameters.setJoinStyle(BufferParameters.JOIN_MITRE);
        Geometry buffered = BufferOp.bufferOp(geom, -.0001, bufferParameters);
        buffered.setUserData(geom.getUserData());
        return buffered;
    }

    private Geometry inflate(Geometry geom) {
        BufferParameters bufferParameters = new BufferParameters();
        bufferParameters.setEndCapStyle(BufferParameters.CAP_ROUND);
        bufferParameters.setJoinStyle(BufferParameters.JOIN_MITRE);
        Geometry buffered = BufferOp.bufferOp(geom, .0001, bufferParameters);
        buffered.setUserData(geom.getUserData());
        return buffered;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.