java shp 转化 jeojson

各个GPT大神们。嘎嘎CV 嘎嘎报错,网上十篇里面九篇是GPT ,复制嘎嘎报错。上代码!

maven 依赖

<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>27.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.geotools/gt-shapefile -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>27.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.geotools/gt-geojsondatastore -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojsondatastore</artifactId>
            <version>26.5.01</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>

java 代码  工具类

package com.xadc.jmrfb3des.controller.wuhan;

import java.io.*;
import com.xadc.jmrfb3des.exception.CustomException;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geojson.feature.FeatureJSON;
public class GeojsonUtil {

    public static String transformShpToGeoJson(String shpPath) throws IOException {
        String geojson = null;
        ShapefileDataStore store = new ShapefileDataStore(new File(shpPath).toURI().toURL());
        SimpleFeatureSource source = store.getFeatureSource();
        SimpleFeatureCollection featureCollection = source.getFeatures();
        FeatureJSON fjson = new FeatureJSON();
        try (StringWriter writer = new StringWriter()) {
            fjson.writeFeatureCollection(featureCollection, writer);
            geojson = writer.toString();
        }catch (Exception e){
            throw new CustomException("shp -> 转化错误");
        }
        return geojson;
    }
}

具体逻辑代码,

@Override
    public String upload(MultipartFile file) throws Exception {
        //获取拷贝后的文件位置
        String filePath = this.copyFile(file, uploadUrl + this.getFileNameWithoutExtension(file) + this.generateUUID());
        //解压文件  读取 .shp 这个文件
        String shpPath = this.unzipAndFindShapeFile(filePath, uploadUrl + ShiroConstant.SHP + this.generateUUID());
        return GeojsonUtil.transformShpToGeoJson(shpPath);
    }

说明一下:这里的copyFile 就是一个拷贝文件的操作,file 是个压缩包,拷贝到临时目录下,然后解压,这都是基础操作,然后直接调用工具类方法,返回的就是转好之后的json,解压文件这里封装了一下,代码如下:

 private  String unzipAndFindShapeFile(String zipFilePath, String destFolderPath) throws Exception {
        
        File destFolder = new File(destFolderPath);
        if (!destFolder.exists()) {
            destFolder.mkdirs();
        }
        String shapeFilePath = null;
        int shpFileCount = 0;
       
        try (ZipFile zipFile = new ZipFile(zipFilePath)) {
            
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                
                File destFile = new File(destFolderPath, entry.getName());
                if (!entry.isDirectory()) {
                    try (InputStream in = zipFile.getInputStream(entry);
                         OutputStream out = Files.newOutputStream(destFile.toPath())) {
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        while ((bytesRead = in.read(buffer)) != -1) {
                            out.write(buffer, 0, bytesRead);
                        }
                    }
                   
                    if (destFile.getName().toLowerCase().endsWith(ShiroConstant.SHP)) {
                        shapeFilePath = destFile.getAbsolutePath();
                        shpFileCount++;
                    }
                }
            }
        }
       
        if (shpFileCount == 1) {
            return shapeFilePath;
        } else if (shpFileCount == 0) {
            throw new CustomException("上传的文件有误,请核实文件后重新上传");
        } else {
            throw new CustomException("在zip文件中找到多个.shp文件");
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LIUUID

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

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

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

打赏作者

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

抵扣说明:

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

余额充值