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?
-
It's not clear to me what you're asking. A list is a generic data structure that could contain anything, GeoJSON is for geographical data. In general, you probably want to use GIS software such as QGIS to do such a conversion. – Lars Kotthoff Oct 11 '13 at 8:40
-
thank you for you reply.. I am create the application using java. and my data store in database and using those data i wont to display graph in d3 but d3 require data in JSON format so i wont to convert data in to JSON. So is there any api avaliable using that i convert data in json that accept by D3 – Milople Inc Oct 11 '13 at 10:38
-
You can use the GeoJSON POJOS for Jackson from Opendatalab, github.com/opendatalab-de/geojson-jackson – vzamanillo May 27 '14 at 15:33
-
RFC7946 1.4 requires the type to be "FeatureCollection" (case sensitive). – JayOThree Feb 16 '19 at 20:21
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());
This will output something like this:
{
"features": [
{
"geometry": {
"coordinates": [
-94.149,
36.33
],
"type": "Point"
}
}
],
"type": "featureCollection"
}
-
Maybe the geojson format referred to in the answer is a little outdated, but as of 2017, we would have to add
feature.put("type","Feature")
for this to work. Just added it as I stumbled upon the answer today. – Arpit Aug 7 '17 at 14:17