Tag Archives: wkt

Wkt to geojson (for multipole)

Wkt (well-known text) is a text markup language, which is used to represent vector geometric objects, spatial reference systems and the conversion between them. Its binary representation, that is, WKB (well-known binary), is better than transmitting and storing the same information in the database.

Geojson is a coding format for various geographic data structures, which can represent geometry, features or feature sets.

Point (6 10)

Linestring (44 4,11 44,33 25)

Polygon ((1 1,2 2,3 3,4 4,5,1,1), (11 11,2 13,34 43,34 42,52,11))

Multipoint (44 4,11 44,33 25)

Multilinestring ((3 4,10 50,20 25), (- 5 – 8, – 10 – 8, – 15 – 4))

Multi polygon (((1 1,5 1,5,1 5,1 1 1), (2 2,2,3,3,3 2,2)), ((6 3,9 2,9 4,6 3)))

Geometric collection (point (46), linear (46,7,10))

Corresponding to geojson

Point {“type”: “point”, “coordinates”: [6,10]}

Lines {“type”: “linestring”, “coordinates”: [[44, 4], [11, 44], [33, 25]]}

Face {“type”: “polygon”, “coordinates”: [[[1,1], [2,2], [3,3], [4,4], [5,5], [1,1]], [[11,11], [2,13], [34,43], [34,42], [52,52], [11,11]]}

Multipoint {“type”: “multipoint”, “coordinates”: [[44, 4], [11, 44], [33, 25]]}

Multilines {“type”: “multilinestring”, “coordinates”: [[[3, 4], [10, 50], [20, 25]], [[- 5, – 8], [- 10, – 8], [- 15, – 4]]}

Polyhedron {“type”: “multipolygon”, “coordinates”: [[[3, 4], [10, 50], [20, 25]], [[- 5, – 8], [- 10, – 8], [- 15, – 4]]}

Geometric set {“type”: “feature collection”, “features”: [{“type”: “feature”, “geometry”: {“type”: “point”, “coordinates”: [4,6]},}, {“type”: “feature”, “geometry”: {“type”: “linestring”, “coordinates”: [[[4,6], [7,10]]}]}

Convert wkt format data to geojson for Google Map

Data format:

wkt: MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3)))

geoJson: {“type”:”MultiPolygon”,”coordinates”:[[[[1,1],[5,1],[5,5],[1,5],[1,1]],[[2,2],[2,3],[3,3],[3,2],[2,2]]],[[[6,3],[9,2],[9,4],[6,3]]]]}

Import the JTS jar package and use geometry to parse multipolygon and other types of data. The code is as follows


 public static final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(),
     4326);
 public static final WKTReader wktReader = new WKTReader(geometryFactory);

 public static Geometry toGeometry(String wkt) throws ParseException {
   Geometry read = wktReader.read(wkt);
   return read;
 }

 public static JSONObject parsePolygon2Geojson(Geometry geom) throws ParseException {
   JSONObject jsonObject = new JSONObject();
   String type = geom.getGeometryType();
   jsonObject.put("type", geom.getGeometryType());
   if ("MultiPolygon".equalsIgnoreCase(type)) {
     JSONArray coordJson = new JSONArray();
     for (int i = 0; i < geom.getNumGeometries(); i++) {
       // polygon
       coordJson.add(parsePolygon(geom.getGeometryN(i)));
     }
     jsonObject.put("coordinates", coordJson);
   } else if ("Polygon".equalsIgnoreCase(type)) {
     jsonObject.put("coordinates", parsePolygon(geom));
   } else if ("MultiLineString".equalsIgnoreCase(type)) {
     JSONArray coordJson = new JSONArray();
     for (int i = 0; i < geom.getNumGeometries(); i++) {
       List<List<Double&>&> line = new ArrayList<&>();
       // polygon
       for (Coordinate coordinate : geom.getGeometryN(i).getCoordinates()) {
         line.add(getPoint(coordinate));
       }
       coordJson.add(line);
     }
     jsonObject.put("coordinates", coordJson);
   } else if ("Point".equalsIgnoreCase(type)) {
     jsonObject.put("coordinates", getPoint(geom.getCoordinate()));
   } else if ("MultiPoint".equalsIgnoreCase(type) || "LineString".equalsIgnoreCase(type)) {
     List<List<Double&>&> coordJson = new ArrayList<&>();
     for (Coordinate coordinate : geom.getCoordinates()) {
       coordJson.add(getPoint(coordinate));
     }
     jsonObject.put("coordinates", coordJson);
   } else {
     jsonObject.put("coordinates", new JSONArray());
   }
   return jsonObject;
 }

 private static JSONArray parsePolygon(Geometry geom) {
   // polygon
   JSONArray coordJson = new JSONArray();
   Polygon polygon = (Polygon) geom;
   LineString exteriorRing = polygon.getExteriorRing();
   exteriorRing.getCoordinateSequence();

   List<List<Double&>&> line = new ArrayList<&>();
   for (Coordinate exterior : exteriorRing.getCoordinates()) {
     line.add(getPoint(exterior));
   }
   coordJson.add(line);
   for (int j = 0; j < polygon.getNumInteriorRing(); j++) {
     List<List<Double&>&> lineIn = new ArrayList<&>();
     for (Coordinate internal : polygon.getInteriorRingN(j).getCoordinates()) {
       lineIn.add(getPoint(internal));
     }
     coordJson.add(lineIn);
   }
   return coordJson;
 }

 private static List<Double&> getPoint(Coordinate exterior) {
   List<Double&> point = new ArrayList();
   point.add(exterior.x);
   point.add(exterior.y);
//        li.add(coordinate.z);
   return point;
 }

According to the observation of the data relationship between wkt and geojson, we can find some rules. Through the way of processing string, we can support point, multi-point, line, multi line, face, multi face and other data. For the data with complex data volume, it takes about 10 ms, but the data format must be correct. Not recommended. MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4)) -&> {“type”:”MULTILINESTRING”,”coordinates”:[[[3,4],[10,50],[20,25]],[[-5,-8],[-10,-8],[-15,-4]]]}

  public static JSONObject getGj(String wkt) {
    if (wkt == null) {
      return null;
    }
    if (wkt.contains("(")) {
      int index = wkt.indexOf("(");
      String type = wkt.substring(0, index);
      String value = wkt.substring(index);
      String t = value.replace("(", "[").replace(")", "]");
//        Pattern.compile("-?(0-9){1,3}(.0-9)*+\\s+-?(0-9){1,2}(.0-9)*+");
//      Pattern compile = Pattern.compile("[-.0-9]+\\s+[-.0-9]+\\s+[-.0-9]+");
      Pattern compile = Pattern.compile("[-.0-9]+\\s+[-.0-9]+");
      Matcher matcher = compile.matcher(t);
      StringBuilder sb = new StringBuilder();
      sb.append("{\"type\":\"" + type + "\",\"coordinates\":");
      int end = 0;
      int start;
      while (matcher.find()) {
        String group = matcher.group();
        start = matcher.start();
        String[] split = group.split("\\s+");
        sb.append(t.substring(end, start)).append("[").append(split[0]).append(",").append
            (split[1]).append("]");
        end = matcher.end();
      }
      sb.append(t.substring(end));
      sb.append("}");
      return JSONObject.fromObject(sb.toString());
    }
    return null;
  }