I have been using NetTopologySuite(a C# port of JTS) for a while. So I assume that most things stay the same as that of JTS. Hopefully Java code could be similar to the c# ones :)
My attempts answering some questions..
what is the canonical way to convert a String to a Point?
A direct casting from IGeometry to IPoint has worked as the below code worked in c#
static void Main(string[] args)
{
WKTReader wktReader = new WKTReader();
IGeometry ptGeometry = wktReader.Read("POINT(0 0)");
IPoint point = (IPoint)ptGeometry;
//printing the coordinates X,Y
Console.WriteLine(point.X.ToString() + "," + point.Y.ToString());
IGeometry lineGeometry = wktReader.Read("LINESTRING (30 10, 10 30, 40 40)");
ILineString lineString = (ILineString)lineGeometry;
//Printing Start and End Points of LineString
Console.WriteLine(lineString.StartPoint.ToString() + "," + lineString.EndPoint.ToString());
IGeometry polygonGeometry = wktReader.Read("POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))");
IPolygon polygon = (IPolygon)polygonGeometry;
Console.WriteLine(polygon.Centroid.ToString());
Console.ReadLine();
}
If so, how should one determine the specific type of Geometry? For
instance, if I want to validate that the user provided a Point rather
than a Polygon?
You could do something like below.
IGeometry ptGeometry = wktReader.Read("POINT(0 0)");
if (ptGeometry is IPoint)
{
//its a point
}
is it good practice to manage all spatial objects as abstract Geometry
instances?
I am not sure whether it is a good practice. But this is the way that it has worked for me. For example, If we are doing any spatial operations like intersect/contain, the parameters and the output of such functions are always IGeometry
. So it is a good reason to work with the abstract Geometries.
For Example, the method to get the intersection of two geometries(whether they are line,point or polygon) is as shown below.
IGeometry Intersection(IGeometry other);
Best Answer
OK, my original answer was wrong (see user30184's comment). Here's another:
The polygon is convex if each interior angle is 180 degrees or less. You can check this in O(n) time, iterating over the triples of points in the exterior ring and checking the sign of the determinant.
}
Original Answer:
You'd want to check whether the convex hull of the geometry is equal to the outer ring of the geometry (assuming it's contiguous), not the entire geometry. The Graham scan algorithm (used by JTS) is O(n log n), and the checking whether two rings are identical is O(n) in the number of points in the ring. That seems pretty fast to (non-geometer) me.