SHP 文件转geojson

依赖

 <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-api</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geometry</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-jts-wrapper</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-opengis</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-data</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-referencing</artifactId>
            <version>20.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>

如果依赖下载不下来,可以配置maven

 <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>
        <repository>
            <id>osgeo-snapshot</id>
            <name>OSGeo Snapshot Repository</name>
            <url>https://repo.osgeo.org/repository/snapshot/</url>
            <snapshots><enabled>true</enabled></snapshots>
            <releases><enabled>false</enabled></releases>
        </repository>
        <!--GeoServer-->
        <repository>
            <id>GeoSolutions</id>
            <url>http://maven.geo-solutions.it/</url>
        </repository>
    </repositories>

工具类

旧版

mport org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.geojson.feature.FeatureJSON;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.xadc.jmrfb3des.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource;


public static String transformShpToGeoJsons(String shpPath) throws IOException {
        String geojson = null;
        ShapefileDataStore store = new ShapefileDataStore(new File(shpPath).toURI().toURL());
        SimpleFeatureSource source = store.getFeatureSource();
        store.setCharset(Charset.forName("GBK"));
        SimpleFeatureCollection featureCollection = source.getFeatures();
        FeatureJSON json = new FeatureJSON();
        try (StringWriter writer = new StringWriter()) {
            json.writeFeatureCollection(featureCollection, writer);
            geojson = writer.toString();
        }catch (Exception e){
            log.info("shp -> 格式转换错误 {} " , e.getMessage());
            throw new CustomException("shp -> 格式转换错误");
        }
        return geojson;
    }


    public static void main(String[] args) throws IOException {

    }

    public static String transformShpToGeoJson(String shpPath) throws IOException {
        String geojson = null;
        ShapefileDataStore store = new ShapefileDataStore(new File(shpPath).toURI().toURL());
        store.setCharset(StandardCharsets.UTF_8);
        SimpleFeatureSource featureSource = store.getFeatureSource();
        SimpleFeatureCollection featureCollection = featureSource.getFeatures();
        FeatureJSON featureJSON = new FeatureJSON();
        try (StringWriter writer = new StringWriter()) {
            featureJSON.writeFeatureCollection(featureCollection, writer);
            geojson = writer.toString();
        } catch (IOException e) {
            log.info("shp -> 格式转换错误 {} " , e.getMessage());
            throw new CustomException("shp -> 格式转换错误");
        }
        return geojson;
    }

 看着没啥问题,但是在实际的过程中,转换过程中会报错 Null啥啥啥methodError 好像是这样的一个问题

跟新后

public static String shp2Geojson(String shpPath) {
        JSONObject geojsonObject = new JSONObject();
        geojsonObject.put("type", "FeatureCollection");
        String string = null;
        try {
            JSONArray array = new JSONArray();
            String fileName = readShpContent(shpPath, array);
            geojsonObject.put("features", array);
            geojsonObject.put("name", fileName);
            String crs = getCoordinateSystemWKT(shpPath);
            //GEOGCS表示这个是地址坐标系,PROJCS则表示是平面投影坐标系
            JSONObject crsJson = new JSONObject();
            JSONObject proJson = new JSONObject();
            crsJson.put("type", "name");
            if (crs.startsWith("PROJCS")) {
                proJson.put("name", "urn:ogc:def:crs:EPSG::3857");
                crsJson.put("properties", proJson);
            } else {
                proJson.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");
                crsJson.put("properties", proJson);
            }
            geojsonObject.put("crs", crsJson);
            long startTime = System.currentTimeMillis();
             // 写入文件
            OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
            string = JSON.toJSONString(geojsonObject).toString();
   return string;
}

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LIUUID

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

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

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

打赏作者

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

抵扣说明:

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

余额充值