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

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值