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?
我想将java List对象转换为D3 GeoJSON。是否有任何java api可用于将java对象转换为GeoJSON对象。我想在d3中显示图形。任何人都可以帮我解决这个问题吗?
1 个解决方案
#1
14
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/):
GeoJSON非常简单;一般的JSON库应该就是您所需要的。以下是使用json.org代码(http://json.org/java/)构建Points列表的方法:
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());
This will output something like this:
这将输出如下内容:
{
"features": [
{
"geometry": {
"coordinates": [
-94.149,
36.33
],
"type": "Point"
}
}
],
"type": "featureCollection"
}
#1
14
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/):
GeoJSON非常简单;一般的JSON库应该就是您所需要的。以下是使用json.org代码(http://json.org/java/)构建Points列表的方法:
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());
This will output something like this:
这将输出如下内容:
{
"features": [
{
"geometry": {
"coordinates": [
-94.149,
36.33
],
"type": "Point"
}
}
],
"type": "featureCollection"
}