WKT (the well-known text) is a standard way in which the OpenGIS statement defines a space object. When we get this object, we need to parse it and draw it on our map. This article mainly describes how to use Java to parse it.
Before parsing, you need to use a tool class. This tool class is uploaded in my resources. You can click here: JTs. Jar
Here is a simple example:
/* WKT is the WKT string you get. It can be a dot, a line, a multi-line, or a multi-faceted string */
Geometry GEO = new wktreader (). Read (WKT );
The following is my line resolution method in practical applications:
Linestring:
/*** Add a path * to input the WKT format * @ Param WKT */Public void addline (string WKT) {try {linestring = (linestring) New wktreader (). read (WKT); this. addline (linestring);} catch (parseexception e) {Throw new illegalargumentexception ("WKT resolution failed:" + WKT );}} /*** Add a path * to input the linestring object * @ Param linestring */Public void addline (final linestring) {arraylist <point> mpoints = new arraylist <point> (); coordinate [] coordinates = linestring. getcoordinates (); For (coordinate: coordinates) {system. out. println (coordinate);} paths. add (mpoints );}
Multilinestring
/*** Add path * to input WKT format ** @ Param WKT */Public void addmultiline (final string WKT) {try {This. addmultiline (multilinestring) New wktreader (). read (WKT);} catch (parseexception e) {Throw new illegalargumentexception ("WKT resolution failed:" + WKT );}} /*** add path * to input multilinestring format ** @ Param multilinestring */Public void addmultiline (final multilinestring) {arraylist <point> mpoints = new arraylist <point> (); int num = multilinestring. getnumgeometries (); // parse wktfor (INT I = 0; I <num; I ++) {linestring = (linestring) multilinestring. getgeometryn (I); coordinate [] coordinates = linestring. getcoordinates (); For (coordinate: coordinates) {mpoints. add (new point (INT) (coordinate. y * 1e6), (INT) (coordinate. x * 1e6);} paths. add (mpoints); mpoints = new arraylist <point> ();}}