geometery parsing geojson

Solutions on MaxInterview for geometery parsing geojson by the best coders in the world

showing results for - "geometery parsing geojson"
María Paula
23 Jan 2021
1import org.apache.commons.lang3.ArrayUtils;
2import com.google.gson.Gson;
3
4public class Scratch {
5    public static void main(String[] args) throws Exception {
6        String json = "{" + 
7                "   \"geometry\": {" + 
8                "       \"type\": \"Polygon\"," + 
9                "       \"coordinates\": [" + 
10                "           [" + 
11                "               [-69.899139," + 
12                "                   12.452005" + 
13                "               ]," + 
14                "               [-69.895676," + 
15                "                   12.423015" + 
16                "               ]" + 
17                "           ]" + 
18                "       ]" + 
19                "   }" + 
20                "}";
21
22        Geometry g = new Gson().fromJson(json, Geometry.class);
23        System.out.println(g);
24        // Geometry [geometry=GeometryData [type=Polygon, coordinates={{{-69.899139,12.452005},{-69.895676,12.423015}}}]]
25    }
26}
27class Geometry {
28    GeometryData geometry;
29
30    @Override
31    public String toString() {
32        return "Geometry [geometry=" + geometry + "]";
33    }
34}
35class GeometryData {
36    String type;
37    double[][][] coordinates;
38
39    @Override
40    public String toString() {
41        return "GeometryData [type=" + type + ", coordinates=" + ArrayUtils.toString(coordinates) + "]";
42    }
43}