0

I'm using simpleJSON in Java and I want to create a geojson. I would like to loop through a list that for each element of the list that has longtitude coords an Array would be created for the longtitude and latitude coords. The format of the geojson should be as follows:

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

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

My problem is that when I'm looping through the list and use the .put for a JSONArray object every time that it changes the previous values instead of just adding the new ones so I end up with something that looks like this:

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

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

Here is the code that I'm using:

public String toJson(ArrayList<String> array){
  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();

  JSONAray JSONArrayCoord = new JSONArray();
  for(int i=0; i<array.length; i++){
    eachElement = array[i];
    if(eachElement.getLongtitude()!=null){
      JSONArrayCoord.add(0, eachElement.getLongtitude());
      JSONArrayCoord.add(1, eachElement.getLatitude());
      geometry.put("type", "Point");
      geometry.put("coordinates", JSONArrayCoord);
      feature.put("geometry", geometry);

      features.add(feature);
      featureCollection.put("features", features);
    }
  }
  return featureCollection.toString();
}

Any ideas?Thanks!!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

The problem was I was initializing a JSONObject called feature, JSONArrayCoord and geometry outside of the loop. Here is the corrected code for the for loop:

for(int i=0; i<array.length; i++){
    eachElement = array[i];
    if(eachElement.getLongtitude()!=null){
      JSONObject geometry = new JSONObject();
      JSONArray JSONArrayCoord = new JSONArray();
      JSONObject newFeature = new JSONObject();

      JSONArrayCoord.add(0, eachElement.getLongtitude());
      JSONArrayCoord.add(1, eachElement.getLatitude());

      geometry.put("type", "Point");
      geometry.put("coordinates", JSONArrayCoord);
      feature.put("geometry", geometry);
      features.add(newFeature);
      featureCollection.put("features", features);
    }
  }