java解析geojson

需求:将geojson字符串转为java对象(jts),或将java对象转为geojson字符串

1、引入maven依赖(注意,此依赖由jdk11编译,所以如果你的版本是jdk8,需要自行下载源码编译,源码地址https://github.com/codingmiao/giscat)

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

2、geojson to FeatureCollection

        String strGeoJson = "{\"features\":[{\"geometry\":{\"coordinates\":[30,10],\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"id\":1}},{\"geometry\":{\"coordinates\":[[30,10],[10,30],[40,40]],\"type\":\"LineString\"},\"type\":\"Feature\",\"properties\":{\"name\":\"hello\"}}],\"type\":\"FeatureCollection\"}";
        GeometryFactory geometryFactory = new GeometryFactory();// jts GeometryFactory
        FeatureCollection featureCollection = GeoJsonFeatureConverter.fromGeoJsonFeatureCollection(strGeoJson, geometryFactory);
        for (Feature feature : featureCollection.getFeatures()) {
            System.out.println(feature.getGeometry());//POINT (30 10)
            System.out.println(feature.getProperties());//{name=hello ...}
        }

3、FeatureCollection to geojson

        GeoJsonObject.FeatureCollection geoJson = GeoJsonFeatureConverter.toGeoJson(featureCollection);
        System.out.println(geoJson.toGeoJsonString());

完整示例

package org.wowtools.giscat.vector.pojo.converter;

import org.locationtech.jts.geom.GeometryFactory;
import org.wowtools.giscat.vector.pojo.Feature;
import org.wowtools.giscat.vector.pojo.FeatureCollection;
import org.wowtools.giscat.vector.pojo.GeoJsonObject;

/**
 * @author liuyu
 * @date 2022/4/1
 */
public class Test1 {


    public static void main(String[] args) throws Exception {
        String strGeoJson = "{\"features\":[{\"geometry\":{\"coordinates\":[30,10],\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"id\":1}},{\"geometry\":{\"coordinates\":[[30,10],[10,30],[40,40]],\"type\":\"LineString\"},\"type\":\"Feature\",\"properties\":{\"name\":\"hello\"}}],\"type\":\"FeatureCollection\"}";
        GeometryFactory geometryFactory = new GeometryFactory();// jts GeometryFactory
        FeatureCollection featureCollection = GeoJsonFeatureConverter.fromGeoJsonFeatureCollection(strGeoJson, geometryFactory);
        for (Feature feature : featureCollection.getFeatures()) {
            System.out.println(feature.getGeometry());//POINT (30 10)
            System.out.println(feature.getProperties());//{name=hello ...}
        }


        GeoJsonObject.FeatureCollection geoJson = GeoJsonFeatureConverter.toGeoJson(featureCollection);
        System.out.println(geoJson.toGeoJsonString());
    }
}

参考

https://github.com/codingmiao/giscat/blob/main/giscat-vector/giscat-vector-pojo/src/test/java/org/wowtools/giscat/vector/pojo/converter/GeoJsonFeatureConverterTest.java

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
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等。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值