[Mobile GIS] How to parse spatial objects in WKT format?

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.