All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nlpcn.es4sql.spatial.WktToGeoJsonConverter Maven / Gradle / Ivy

There is a newer version: 7.8.0.1
Show newest version
package org.nlpcn.es4sql.spatial;


import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eliran on 4/8/2015.
 */
public class WktToGeoJsonConverter {
    public static String toGeoJson(String wkt){
        wkt = wkt.toLowerCase();
        int startOfCoordinates = wkt.indexOf("(");
        if(startOfCoordinates == -1)
            throw new IllegalArgumentException("not valid wkt");

        String wktType = wkt.substring(0, startOfCoordinates).trim();
        wkt = wkt.substring(startOfCoordinates);

        String type="";
        String coordinates="";
        switch (wktType){
            case("point"):
                type = "Point";
                coordinates = pointCoordinatesFromWkt(wkt);
                break;
            case("polygon"):
                type = "Polygon";
                coordinates = polygonCoordinatesFromWkt(wkt);
                break;
            case("linestring"):
                type = "LineString";
                coordinates = lineStringCoordinatesFromWkt(wkt);
                break;
            case("multipolygon"):
                type = "MultiPolygon";
                coordinates  = multiPolygonCoordinatesFromWkt(wkt);
                break;
            case("multipoint"):
                type = "MultiPoint";
                coordinates = multiPointCoordinatesFromWkt(wkt);
                break;
            case("multilinestring"):
                type = "MultiLineString";
                coordinates = multiLineStringCoordinatesFromWkt(wkt);
                break;
            default:
                throw new IllegalArgumentException("not supported wkt type");

        }

        return buildGeoJson(type,coordinates);
    }
    //input: ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))
    private static String multiLineStringCoordinatesFromWkt(String wkt) {
        wkt = removeBrackets(wkt,1);
        String lineStringsWithPipeSeparator = wkt.replaceAll("\\s*\\)\\s*,\\s*\\(",")|(");
        String[] lineStrings = lineStringsWithPipeSeparator.split("\\|");
        String[] coordinates = new String[lineStrings.length];
        for (int i=0;i 10 40, 40 30, 20 20
            wkt = wkt.replaceAll("\\(|\\)" ,"");
        }
        coordinates = getJsonArrayFromListOfPoints(wkt);
        return coordinates;
    }

    //input (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))
    private static String multiPolygonCoordinatesFromWkt(String wkt) {
        wkt = removeBrackets(wkt,1);
        String polygonsWithPipeSeparator = wkt.replaceAll("\\s*\\)\\s*\\)\\s*,\\s*\\(\\s*\\(\\s*","))|((");
        String[] polygons = polygonsWithPipeSeparator.split("\\|");
        String[] polygonsCoordinates = new String[polygons.length];
        for (int i=0;i coordinates = new ArrayList<>();
        for(String point : points){
            coordinates.add(extractCoordinateFromPoint(point));
        }

        String joinedCoordinates = Joiner.on(",").join(coordinates);
        return String.format("[%s]", joinedCoordinates);
    }

    private static String buildGeoJson(String type, String coordinates) {
        return String.format("{\"type\":\"%s\", \"coordinates\": %s}", type, coordinates);
    }
    //input : (30 10)
    public static String pointCoordinatesFromWkt(String wkt) {
        wkt = removeBrackets(wkt,1);
        return extractCoordinateFromPoint(wkt);
    }

    private static String extractCoordinateFromPoint(String point) {
        String pointPattern = "(\\s*)([0-9\\.-]+)(\\s*)([0-9\\.-]+)(\\s*)";
        return point.replaceAll(pointPattern,"[$2,$4]");
    }

    private static String removeBrackets(String wkt, int num) {
        String result= wkt;
        for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy