Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShapeReader conversion does not always correctly identify holes #408

Open
petebankhead opened this issue Apr 5, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@petebankhead
Copy link

commented Apr 5, 2019

ShapeReader can give unexpected (at least to me...) results in some cases, I believe because the assumption within isHole(Coordinate[] pts) does not always match the underlying Java AWT representation.

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:

Area of big rectangle: 10000.0
Area of small rectangle: 2500.0
Area of combined geometry: 12500.0
Area of combined geometry (with buffer): 10000.0
Combined geometry: MULTIPOLYGON (((80 30, 80 80, 30 80, 30 30, 80 30)), ((5 5, 5 105, 105 105, 105 5, 5 5)))
Area from raster: 7500

binary

I would expect the Geometry area to also be 7500 if the hole was correctly identified.

My aim is to switch easily betweenShape and Geometry representations. Given the diversity of possible Shape implementations, I am unclear on what assumptions can be made about the PathIterator in general - however the javadoc for java.awt.geom.Area states

  • The interiors of the individual stored sub-paths are all non-empty and non-overlapping [...]
  • The geometry of the path describing the outline of the Area resembles the path from which it was constructed only in that it describes the same enclosed 2-dimensional area, but may use entirely different types and ordering of the path segments to do so.

Therefore I figured that converting closed shapes to Area objects first would be the simplest/most reliable way to approach this, giving a PathIterator with more predictable behavior.

Any corrections / tips for how to do this conversion better would be much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.