如何将Java对象转换为GeoJSON(d3图形需要) [英] How to Convert Java Object in to GeoJSON (Required by d3 Graph)

查看:2217
本文介绍了如何将Java对象转换为GeoJSON(d3图形需要)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将 java列表对象转换为 D3 GeoJSON
有任何java api可用,帮助将java对象转换为GeoJSON对象。
我想在d3中显示图形。
任何人都可以帮我解决这个问题?

I want to convert java List object into D3 GeoJSON. Is there any java api available that help to convert java object to GeoJSON object. I want to display graph in d3. Can anyone help me to solve this problem?

推荐答案

GeoJSON非常简单;一个通用的JSON库应该是你需要的。以下是使用json.org代码( http://json.org/java/ )构建点列表的方法: / p>

GeoJSON is very simple; a general JSON library should be all you need. Here's how you could construct a list of Points using the json.org code (http://json.org/java/):

    JSONObject featureCollection = new JSONObject();
    try {
        featureCollection.put("type", "featureCollection");
        JSONArray featureList = new JSONArray();
        // iterate through your list
        for (ListElement obj : list) {
            // {"geometry": {"type": "Point", "coordinates": [-94.149, 36.33]}
            JSONObject point = new JSONObject();
            point.put("type", "Point");
            // construct a JSONArray from a string; can also use an array or list
            JSONArray coord = new JSONArray("["+obj.getLon()+","+obj.getLat()+"]");
            point.put("coordinates", coord);
            JSONObject feature = new JSONObject();
            feature.put("geometry", point);
            featureList.put(feature);
            featureCollection.put("features", featureList);
        }
    } catch (JSONException e) {
        Log.error("can't save json object: "+e.toString());
    }
    // output the result
    System.out.println("featureCollection="+featureCollection.toString());

这将输出如下:

{
"features": [
    {
        "geometry": {
            "coordinates": [
                -94.149, 
                36.33
            ], 
            "type": "Point"
        }
    }
], 
"type": "featureCollection"
}

这篇关于如何将Java对象转换为GeoJSON(d3图形需要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆