GeoTools --- geoJson 转 SimpleFeatureCollection 并实现裁剪功能

geoJson 转 SimpleFeatureCollection 并实现裁剪功能

pom依赖

<properties>
	<geotools.version>25.0</geotools.version>
</properties>

<dependencies>
    <!--geojson操作相关工具-->
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-geojson</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-geometry</artifactId>
        <version>24.0</version>
    </dependency>
<dependencies>

<repositories>
  <repository>
    <id>osgeo</id>
    <name>OSGeo Release Repository</name>
    <url>https://repo.osgeo.org/repository/release/</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <releases>
      <enabled>true</enabled>
    </releases>
  </repository>
</repositories>

工具类:

package com.ruhr.sys.enshi.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.collection.ClippedFeatureCollection;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
 * @ClassName GeoJsonUtil
 * @Author: zsp
 * @Date 2022/4/1 10:41
 */
@Slf4j
public class GeoJsonUtils {

    /**
     * 切割等值线1
     * @param areaGeoJson 需要被裁剪的区块的geoJson
     * @param clipGeoJson 需要裁剪的范围 类型是 geoJson 比如某个乡镇的边界
     *                    场景:将某个市级的等值线(areaGeoJson)按某乡镇(clipGeoJson)的大小裁剪
     * @return 裁剪好的 geoJson字符串
     */
    public static String clipGeoJsonStrByGeoJsonStr(String areaGeoJson,
                                                    String clipGeoJson) throws Exception {

        SimpleFeatureCollection simpleFeatureCollection = geoJsonStr2FeatureCollection(areaGeoJson);
        SimpleFeatureCollection clipFeatureCollection = clipFeatureCollectionByGeoJsonStr(simpleFeatureCollection, clipGeoJson);
        return featureCollection2GeoJsonStr(clipFeatureCollection);
    }

    /**
     * 切割等值线2
     * @param featureCollection 需要被裁剪的区块的SimpleFeatureCollection
     * @param clipGeoJson 需要裁剪的范围 类型是 geoJson 比如某个乡镇的边界
     *                    场景:将某个市级的等值线(featureCollection)按某乡镇(clipGeoJson)的大小裁剪
     * @return 裁剪好的SimpleFeatureCollection
     */
    public static SimpleFeatureCollection clipFeatureCollectionByGeoJsonStr(SimpleFeatureCollection featureCollection,
                                                                            String clipGeoJson) throws IOException {
        GeometryJSON geometryJSON = new GeometryJSON();
        Geometry geometry;
        if (clipGeoJson.contains("MultiPolygon")) {// 多多边形
            geometry = geometryJSON.readMultiPolygon(clipGeoJson);
        } else if (clipGeoJson.contains("Polygon")) {
            geometry = geometryJSON.readMultiPolygon(clipGeoJson);
        } else {// 不符合规则 返回
            log.error("当前geoJson不符合规范!");
            return null;
        }
        // 调用 geoTools 的裁剪要素方法
        return new ClippedFeatureCollection(featureCollection, geometry, true);
    }


    /**
     * 将 SimpleFeatureCollection 对象转换成 geoJson 字符串
     * @param featureCollection SimpleFeatureCollection 或其子类
     * @return geoJson字符串
     */
    public static String featureCollection2GeoJsonStr(SimpleFeatureCollection featureCollection) throws IOException {
        FeatureJSON featureJSON = new FeatureJSON(new GeometryJSON(8));// 避免精度丢失
        return featureJSON.toString(featureCollection);
    }


    /**
     * geoJson 字符串 转 SimpleFeatureCollection
     * @param geoJson 字符串
     * @return SimpleFeatureCollection
     */
    public static SimpleFeatureCollection geoJsonStr2FeatureCollection(String geoJson) throws IOException {
        DefaultFeatureCollection simpleFeatures = new DefaultFeatureCollection();
        FeatureJSON featureJSON = new FeatureJSON();// 该工具提供了读写geoJson的方法
        SimpleFeature simpleFeature = featureJSON.readFeature(geoJson);
        SimpleFeatureCollection featureCollection =
                (SimpleFeatureCollection) featureJSON.readFeatureCollection(geoJson);
        if (featureCollection == null || featureCollection.isEmpty()) {
            simpleFeatures.add(simpleFeature);
            return simpleFeatures;
        }
        return featureCollection;
    }

    
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值