Convert java.lang.String geometry to jts Geometry


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?

Best Solution

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);