如何用java读取并解析geojson文件


工具:json.simple、wowtools

json.simple用于读取json文件,wowtools自动适配地解析geojson格式。

1.引入依赖

代码如下(示例):

       <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.wowtools</groupId>
            <artifactId>giscat-vector-pojo</artifactId>
            <version>1.1.1-STABLE</version>
        </dependency>

2.读取并解析数据

比如现有geojson文件格式如下:
在这里插入图片描述
代码如下(示例):

 public static void main(String[] args) {
        //创建一个JSONParser对象
        JSONParser jsonParser = new JSONParser();
        //解析JSON文件的内容
        try {
            //先用json.simple读取geojson文件
            JSONObject obj = (JSONObject) jsonParser.parse(new FileReader("E:\\河南省乡镇点\\FieldPolygon.geojson"));
            //删除除features以外其它对象,方便下面所需的geojson字符串
            obj.remove("type");
            obj.remove("name");
            obj.remove("crs");
            //获取features对象json字符串
            String strGeoJson =obj.toString();
            GeometryFactory geometryFactory = new GeometryFactory();// jts GeometryFactory
            //获取要素集合
            FeatureCollection featureCollection = GeoJsonFeatureConverter.fromGeoJsonFeatureCollection(strGeoJson, geometryFactory);
            //遍历feature集合,取出其geometry或属性字段
            for (Feature feature : featureCollection.getFeatures()) {
                System.out.println(feature.getGeometry());//POLYGON (.....)
                System.out.println(feature.getProperties().get("name"));//"properties": { "id": 1, "name": "田块" }
            }
            System.out.println(obj.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

总结

如若想提取除“Feature”之外的对象的值时,可在执行remove之前将值提取出来即可。
如提取name:

 String obj2=(String) obj.get("name");

如提取crs中的name,需要将json对象转为map,再取值。

 JSONObject obj = (JSONObject) jsonParser.parse(new FileReader("E:\\河南省乡镇点\\FieldPolygon.geojson"));
 //根据结构一层层取值
 Map map = (Map) JSON.parse(obj.toString());
 Map map1 =(Map) JSON.parse (map.get("crs").toString());
 Map map2 =(Map) JSON.parse (map1.get("properties").toString());
 String name = (String) map2.get("name");
 System.out.println(name);

除上述方法外,还可以将json中各个对象封装成java类:参考https://www.cnblogs.com/dabenhou/p/14451224.html?ivk_sa=1024320u

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
GeoTools是一个开源的Java工具包,可用于处理地理空间数据。GeoTools提供了对GeoJSON格式的支持,可以使用GeoTools解析GeoJSON数据。 以下是一个简单的示例代码,展示了如何使用GeoTools解析GeoJSON数据: ```java File geojsonFile = new File("path/to/geojson/file"); // GeoJSON文件路径 // 创建GeoJSON文件读取GeoJSONDataStoreFactory dataStoreFactory = new GeoJSONDataStoreFactory(); Map<String, Object> params = new HashMap<>(); params.put(GeoJSONDataStoreFactory.URLP.key, geojsonFile.toURI().toURL()); GeoJSONDataStore dataStore = (GeoJSONDataStore) dataStoreFactory.createDataStore(params); // 获取GeoJSON数据源中的FeatureCollection SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]); SimpleFeatureCollection featureCollection = featureSource.getFeatures(); // 遍历FeatureCollection try (SimpleFeatureIterator featureIterator = featureCollection.features()) { while (featureIterator.hasNext()) { SimpleFeature feature = featureIterator.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); // 获取几何对象 // 对几何对象进行操作 // ... } } ``` 上述代码首先创建了一个GeoJSON文件读取器,然后使用该读取器获取GeoJSON数据源中的FeatureCollection。接着遍历FeatureCollection,对其中的每个Feature进行操作。 需要注意的是,使用GeoTools解析GeoJSON数据时,需要先将GeoJSON文件转换为GeoJSON数据源,然后再从数据源中获取FeatureCollection。GeoTools支持的数据源类型包括文件、URL、InputStream等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

定位算法工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值