Reputation: 181
I have a geometry in java.lang.String format. I mean i take it directly from DB in as a java.lang.String which is stored in a variable. I want to convert it to jts Geometry type somehow. Is there any way to do it or what i'm trying to do is just a foolish approach?
Upvotes: 4
Views: 11189
Reputation: 2485
I assume that your geometry has the WKT (Well Known Text) format. If not, you can get it in the WKT format by using the ST_AsText
method (requires spatial DB, which I assume you are using).
A simple example of how to get a geometry from a WKT String:
String wktString = "LINESTRING (0 0, 0 10)";
WKTReader reader = new WKTReader();
Geometry geom = reader.read(wktString);
Upvotes: 10
Reputation: 3040
You will have to first convert from String to Coordinates before you can convert it to a Geometry.
If the values are comma separated you could split them and create an array of Coordinates
String[] split=stringgeometry.split(",");
Coordinate[] coordinates = new Coordinate[split.length/2];
index = 0;
for(int i=0;i<split.length;i+=2)
{
coordinates[index]=new Coordinate(split[i], split[i+1]);
index++;
}
After this you can create any Geometry that you want by using the GeometryFactory() class. For example to create a line string,
Geometry geometry = new GeometryFactory().createLineString(coordinates);
Is this what you want to do?
Upvotes: 2