Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
ShapeReader conversion does not always correctly identify holes #408
ShapeReader can give unexpected (at least to me...) results in some cases, I believe because the assumption within
Here is a short example, subtracting a small rectangle from a larger rectangle:
import org.locationtech.jts.awt.ShapeReader; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Area; import java.awt.image.BufferedImage; // Define large & small rectangles int bigSize = 100; int smallSize = 50; Rectangle rectBig = new Rectangle(5, 5, bigSize, bigSize); Rectangle rectSmall = new Rectangle(30, 30, smallSize, smallSize); // Subtract small from large Area area = new Area(rectBig); area.subtract(new Area(rectSmall)); // Print out areas from corresponding Geometry objects ShapeReader reader = new ShapeReader(new GeometryFactory()); Geometry geomBig = reader.read(rectBig.getPathIterator(null)); Geometry geomSmall = reader.read(rectSmall.getPathIterator(null)); Geometry geomCombined = reader.read(area.getPathIterator(null)); System.out.println("Area of big rectangle: " + geomBig.getArea()); System.out.println("Area of small rectangle: " + geomSmall.getArea()); System.out.println("Area of combined geometry: " + geomCombined.getArea()); System.out.println("Area of combined geometry (with buffer): " + geomCombined.buffer(0).getArea()); System.out.println("Combined geometry: " + geomCombined); // Create a binary image for an extra check BufferedImage img = new BufferedImage(bigSize+10, bigSize+10, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g2d = img.createGraphics(); g2d.fill(area); g2d.dispose(); int px = new int[img.getWidth()*img.getHeight()]; img.getRaster().getSamples(0, 0, img.getWidth(), img.getHeight(), 0, px); System.out.println("Area from raster: " + Arrays.stream(px).filter(i -> i > 0).count());
This prints the following:
I would expect the Geometry area to also be 7500 if the hole was correctly identified.
My aim is to switch easily between
Therefore I figured that converting closed shapes to
Any corrections / tips for how to do this conversion better would be much appreciated!