使用Java从(geo)json中的数组中删除对象

使用Java从(geo)json中的数组中删除对象,java,json,polygon,geojson,Java,Json,Polygon,Geojson,我有一个geojson(一个用于地理参考数据的json),它有一个“features”数组,包含许多多边形。我想循环遍历“features”数组中的元素,并删除其中一些元素(多边形面积小于70.0)。这是我的geojson的结构: { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "DN": 0 }, "geometry": { "type": "Polyg

我有一个geojson(一个用于地理参考数据的json),它有一个“features”数组,包含许多多边形。我想循环遍历“features”数组中的元素,并删除其中一些元素(多边形面积小于70.0)。这是我的geojson的结构:

{
   "type": "FeatureCollection",

   "features": [
      { "type": "Feature", "properties": { "DN": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 5117.0, 0.0 ], [ 5117.0, 1.0 ], [ 5124.0, 1.0 ], [ 5117.0, 0.0 ] ] ] } },
      { "type": "Feature", "properties": { "DN": 2 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.0, 149.0 ], [ 0.0, 150.0 ], [ 61.0, 150.0 ], [ 0.0, 149.0 ] ] ] } }
   ]
}
在这里,我尝试循环“features”(特征)数组的元素,计算每个多边形的面积,如果其面积小于70.0,则将其删除:

public static void smallPolygonRemover() throws IOException, ParseException{
        // read geojson
        FileReader reader = new FileReader("source.geojson");
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
        JSONArray features = (JSONArray) jsonObject.get("features");

        for (int j = 0; j < features.size(); j++) {
              JSONObject firstFeature = (JSONObject) features.get(j);
              JSONObject geometry = (JSONObject) firstFeature.get("geometry");
              JSONArray coordinates = (JSONArray) geometry.get("coordinates");
              // area(coordinate) calculates the area of the polygon with given coordinates
              if(area(coordinates)<70.0){
                    features.remove(firstFeature);
              }
        }
        // write the edited geojson to a file
        FileWriter writer = new FileWriter("Removed.geojson");
        writer.write(jsonObject.toJSONString());
        writer.close();
}
public static void smallPolygonRemover()抛出IOException、ParseException{
//读取geojson
FileReader=newFileReader(“source.geojson”);
JSONParser JSONParser=新的JSONParser();
JSONObject JSONObject=(JSONObject)jsonParser.parse(reader);
JSONArray特性=(JSONArray)jsonObject.get(“特性”);
对于(int j=0;j如果(区域(坐标)循环和
remove()之间存在交互作用
您遗漏的内容:如果删除数组中的一个条目,则所有后续元素的索引都会更改。这意味着如果删除项目2,则项目3将变为项目2。然后,循环将索引设置为3,这意味着您永远不会选中项目3,而是继续使用旧的项目4

从循环中删除
j++
,改用此代码:

 if(area(coordinate)<70.0){
     features.remove(firstFeature);
     // removing the polygon moves j+1 to j, so we need to leave j alone
 } else {
     j++; // this polygon is OK, move to the next one
 }

if(区域(坐标))检查代码。将点放入变量
coordinates
(使用
s
)中,然后调用
area()
带有
坐标
。这是问题中的输入错误还是实际的代码错误?这是一个输入错误,但我的原始代码使用坐标。因此输入错误仅在这里。Thx。这似乎是朝着正确的方向,但仍然有一些错误。带有
System.out.println(区域(坐标));
我发现最大的多边形的面积为2932899.1。因此我将if块改为:
if(面积(坐标),代码在每次循环后都应该执行
j++
。因此,不可能用上面的代码获得
-1
索引(对于第一个元素,
j
将是循环末尾的
-1
,然后
j++
将其转换为
0
)。另一个解决方案是删除循环中的增量并使用
if(area…{…}else{j++;}
。如果没有其他帮助,您需要逐行调试代码,看看会发生什么。将j++从If块下的循环头中移动就成功了!我发现面积函数计算出的面积比实际值少了大约60%。。嗯,也许您应该编辑您的答案,以便我可以接受它(或者因为它在注释部分,所以可以吗?@Selphiron:我已经编辑了我的答案,但仍然不需要
else j++;
除非您在循环中执行其他操作。嗯,不,我没有else分支。无论if块如何,j++都在执行。我应该将它放在else分支中吗?