使用简单的json java创建geojson

thtygnil  于 2023-03-04  发布在  Java
关注(0)|答案(1)|浏览(213)

我想使用simple-json-1.1.1 jar创建下面的geojson。

{"type": "FeatureCollection",
"crs": {
    "type": "name",
    "properties": {
            "name": "ESPG:4326"
            }
    },
    "features":[

    {
        "type":"Feature",
        "geometry":{
                "type":"Point",
                "coordinates":[55,55]
                },
        "properties":{
                "desc":"something"}
                }
    ]
}

有什么办法吗?谢谢!!

dced5bon

dced5bon1#

创建上面提到的geojson的代码如下:

JSONObject featureCollection = new JSONObject();
featureCollection.put("type", "FeatureCollection");
JSONObject properties = new JSONObject();
properties.put("name", "ESPG:4326");
JSONObject crs = new JSONObject();
crs.put("type", "name");
crs.put("properties", properties);
featureCollection.put("crs", crs);

JSONArray features = new JSONArray();
JSONObject feature = new JSONObject();
feature.put("type", "Feature");
JSONObject geometry = new JSONObject();

JSONArray JSONArrayCoord = new JSONArray();

JSONArrayCoord.add(0, 55);
JSONArrayCoord.add(1, 55);
geometry.put("type", "Point");
geometry.put("coordinates", JSONArrayCoord);
feature.put("geometry", geometry);

features.add(feature);
featureCollection.put("features", features);

相关问题