[GIS] Combining neighbor polygons (NOT touching) making one using JTS/GeoTools

geotoolsjts-topology-suite

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

Best Answer

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;
    }