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);
A da.searchcursor should work for you.
for row in arcpy.da.SearchCursor("path2data", ["SHAPE@WKT"]):
print row[0]
POINT Z (-119.53753379999995 49.854383300000052 303.14500000000407)
doc here: http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001t000000
Note: SHAPE@JSON, SHAPE@WKB, and SHAPE@WKT tokens were made available at ArcGIS 10.1 Service Pack 1.
Or, if you're after the samples toolbox like you mentioned - its there, just deprecated. You can call into it with scripts still.
If you need the actual toolbox you can use, its here on the old Model and Script Gallery
EDIT (extra example)...Because you asked so nicely: :)
for row in arcpy.da.SearchCursor("GPX_Layer", ["SHAPE@WKT"],where_clause="TYPE = 'a'"):
print row[0]
It's just the same as a "select by attributes" type of query/expression. I have a field called "TYPE" and and a value 'a'.
Best Answer
You can use the com.vividsolutions.jts.io.oracle.ORAWriter constructor. It "translates a JTS Geometry into an Oracle STRUCT representing an MDSYS.GEOMETRY object.". But for this you need an OracleConnection. It's required by that constructor. Using this kind of implementation you don't have to worry how the several attributes from the SDO_GEOMETRY object are constructed.
Another approach is taken by the gt-jdbc-oracle extension from the GeoTools project: although they depended heavely on the JTS library, the ORAWriter isn't used because it needed to introduce a direct dependency on the OJDBC driver (oracle.jdbc.OracleConnection and others) classes.
I think the Oracle Spatial Java API (sdoapi.jar, sdoutl.jar) already have such facilities: read/write from/to WKT/JGeometry. But you will not be able to work with JTSGeometry..I think. This Java API has the same license as Oracle Locator.