Determining whether a polygon is a rectangle is a very useful function in GIS, how do you determine if a polygon is a rectangle?
To understand some concepts, what is a face, first look at the definition of the OGC standard.
My English level is limited. (Please leave a message if translation is better, if translation is accurate will be adopted) It roughly means that a Polygon (Polygon is followed by Polygon) is flat, consisting of an outer boundary and 0 or more inner boundaries, each of which is a hole in a Polygon.
This is still abstract, so the last picture is much more awake.
The three above are all polygons, but there are some special cases, which are not discussed in depth here and are a bit different in different GIS frameworks.
Well, we can tell if such a face is rectangular.
//First of all, I want to make a statement here that not all of the code is required here. If you want copy ken to run or not, this is an idea and a basic idea. I hope you can write your own code according to my ideas. public boolean isRectangle() { //First determine if there are five points on the outer ring if(outLine.getPointNum() != 5) return false;
//The number of inner rings can only be zero if(getNumInteriorRing() != 0) return false; //Take the outer winding of this side Envelope envelope = getEnvelope(); //Remove the width and height of the outer winding double envelopeHeight = envelope.getHeight(); double envelopeWidth = envelope.getWidth();
//Why does this loop four times because the definition of a polygon is closed, and closure requires that the beginning and end points be equal, which is the knowledge of GIS
//This for loop expels something.See Fig.1
for (int i = 0 ; i < 5; i ++) { Coordinate coordinate = outLine.getPoint(i); double x = coordinate.getX(); double y = coordinate.getY(); if (!(x == envelope.getMaxX() || x == envelope.getMinX())) { return false; } if(!(y == envelope.getMaxY() || y == envelope.getMinY())) { return false; } } //This is a special definition of this frame, with lines enclosed by rectangles. if (envelopeHeight == 0 || envelopeWidth == 0) { return true; } Coordinate upCoor = outLine.getPoint(0); Coordinate nextCoor = outLine.getPoint(1); //Because the computer calculates the multiplication faster than the square, it chooses to use the multiplication to do it. double doubleEnvelopeHeight = envelopeHeight * envelopeHeight; double doubleEnvelopWidth = envelopeWidth * envelopeWidth;
//Is there anything else besides Fig.1?
//See Figure 2 for (int i = 1; i < 4; i ++) {
//a^2 + b^2 No square calculation is done here, for efficiency double length = MathUtil.distanceTwoPointNoSquare(upCoor,nextCoor); if (length > doubleEnvelopeHeight && length > doubleEnvelopWidth) { return false; } upCoor = outLine.getPoint(i); nextCoor = outLine.getPoint(i+1); } return true; }
Fig.1
for (int i = 0 ; i < 5; i ++) { Coordinate coordinate = outLine.getPoint(i); double x = coordinate.getX(); double y = coordinate.getY(); if (!(x == envelope.getMaxX() || x == envelope.getMinX())) { return false; } if(!(y == envelope.getMaxY() || y == envelope.getMinY())) { return false; } }
This for selects the case where the drain point is not on the vertex of the bounding rectangle.
Figure 2
for (int i = 1; i < 4; i ++) { //a^2 + b^2 No square calculation is done here, for efficiency double length = MathUtil.distanceTwoPointNoSquare(upCoor,nextCoor); if (length > doubleEnvelopeHeight && length > doubleEnvelopWidth) { return false; } upCoor = outLine.getPoint(i); nextCoor = outLine.getPoint(i+1); }
In the case of Figure 2, each point is at the point of the outer rectangle. (In particular, some midpoints of the GIS framework cannot define duplicate points, such as the last two points in the last triangle are the same, so there should be no controversy.)
Author Young-Ken (Weibo)
Reviewer Cindy-Leee (Weibo)
Please note http://www.cnblogs.com/youngKen/p/4987049.html for upload
Reprinted at: https://www.cnblogs.com/youngKen/p/4987049.html