Sunday, 24 December 2017

c# - Determining if collection of coordinates (polygon) is ellipse?


I'm attempting to determine a geometry type based on a collection of coordinates and have come across a situation where I'd like to differentiate between what is a "polygon" and what is an "ellipse". This question is specific to ESRI's ArcGIS Runtime for WPF 10.1.1 SDK, but I imagine this is generic enough to have bearing in any GIS. I'll use ESRI's API for the examples in this sample.


Given the following code sample to generate an ellipse's point collection:


double slice = 2 * Math.PI / 360;
double radiusX = 50;
double radiusY = 20;

ESRI.ArcGIS.Client.GeometryMapPoint center = new ESRI.ArcGIS.Client.Geometry.MapPoint(0,0);
ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection = new ESRI.ArcGIS.Client.Geometry.PointCollection();
for (int angle = 0; angle <= 360; angle += 6)
{
double rad = slice * angle;
double px = center.X + radiusX * Math.Cos(rad);
double py = center.Y + radiusY * Math.Sin(rad);
pointCollection.Add(new ESRI.ArcGIS.Client.Geometry.MapPoint(px, py));
}


And then, given this sample to generate a polygon's point collection (obviously the polygon could be much more complex than this):


ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection = new ESRI.ArcGIS.Client.Geometry.PointCollection();
pointCollection.Add(new MapPoint(0, 0));
pointCollection.Add(new MapPoint(0, 10));
pointCollection.Add(new MapPoint(10, 10));
pointCollection.Add(new MapPoint(10, 0));
//Close the polygon
pointCollection.Add(pointCollection[0]);

Is there an effective, efficient, and generic way to determine which of these two shapes is an ellipse and which is not? The impetus behind this is that the ESRI WPF API does not differentiate between Polygons and Ellipses.





Perhaps I can make this question more clear, what I would like to discern is whether the given points constitute what could be considered a 2-Dimensional ellipse (perhaps already making this too subjective). The ellipse could have any number of radial points comprising it, and I would like to ideally determine if it meets some test of "roundness" or curvature. The x-radius and y-radius of the sample ellipse could also be varied. I've edited my sample to include this.



Answer



I had a project where I needed to classify geometries as circles, ellipses, or irregular polygons. I found that after locating the center of the figure, I could easily classify two coordinates as "closest" and "farthest" point to that center, which then would allow me to derive a possible orientation of the ellipse, and its semi-major and semi-minor axis. Then I just calculated the distance from that center to each of the vertices, and what the hypothetical distance at that angle would be if the figure were an ellipse. If the sum of the deltas between actual and hypothetical, divided by the number of vertices was relatively small, then I could classify the shape as an ellipse, and if semi-major was roughly equal to semi-minor, then it was a circle, otherwise it was a generic polygon.


There were some minor flourishes in the orientation determination (using the two closest and two farthest points), and possibly a square root of sum of squares in the delta determination (I don't have access to the code anymore) but it seemed reliable enough over the hundreds of shapes I had to test against. I had a further complication that the distances all had to be calculated on a WGS84 spheroid, but it even handled high latitude geometries correctly. It's possibly not the most efficient solution, but it wasn't too bad [O(n)], and it was effective.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...