GIS:shp文件解析相关

1. 今天解析shp文件的时候,如果是点或者线就没问题,但是面就会解析报一下错误:

java.lang.NoSuchMethodError: org.locationtech.jts.geom.Polygon.getExteriorRing()Lorg/locationtech/jts/geom/LineString;

然后一通好找,发现是依赖包的版本问题,用这两个版本,就好了:geotools: 21.0          JTS: compile group: 'org.locationtech.jts', name: 'jts-core', version: '1.16.1'

参考来源:https://stackoverflow.com/questions/65442075/error-loading-polygon-geometries-using-shape-file-data-store-with-geotools-24-0

2. wkt和Geometry互转

wkt转Geometry:


import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;


WKTReader wktReader = new WKTReader();
Geometry geometry = wktReader.read(wkt);
geometry.setSRID(4326);

Geometry转wkt:

String wkt = geometry != null ? geometry.toText() : "";

3. shp解析核心部分代码

 @ApiOperation(value = "导入shp文件", notes = "导入shp文件")
    @RequestMapping(value = "shp", method = {RequestMethod.GET, RequestMethod.POST})
    public Result shp(MultipartFile[] file, int tag) {
        try {
            File ff = null;
            if (ff != null) {
                Map<String, Object> columnsMap = new HashMap<>();
                List<Columns> columns = new ArrayList<>();
                ShapeLoader loader = new ShapeLoader(ff);
                int i = 0;
                if (loader != null) {
                    FeatureCollection collection = loader.getFeatureCollection();
                    try (FeatureIterator<SimpleFeature> features = collection.features()) {
                        while (features.hasNext()) {
                            i++;
                            Feature feature = features.next();
                            GeoData geoData = new GeoData();
                            // 获取geometry列的属性名称
                            String geometryColumnName = feature.getDefaultGeometryProperty().getName().toString();
                            Geometry geometry = (Geometry) ((SimpleFeature) feature).getDefaultGeometry();
                            Geometry targetGeometry = loader.transform(geometry);
                            geoData.setGeometry(targetGeometry);
                            geoData.setWkt(geoData.getWkt());
                            geoDataService.saveData(geoData);
                            PropertyValues prop = new PropertyValues();
                            // 获取所有属性列表
                            Collection<Property> properties = feature.getProperties();//(
                            if (properties != null) {
                                Map<String, Object> map = new HashMap<>();
                                properties.forEach(property -> {
                                    String name = property.getName().toString(); // 获取属性名称
                                    if (!StringUtils.equalsIgnoreCase(geometryColumnName, name)) {
                                        String type = property.getDescriptor().getType().getBinding().getSimpleName();
                                        columnsMap.put(name, type);
                                        System.out.println(name + "===" + property.getDescriptor().getType().getBinding().getSimpleName() + "===" + property.getValue());
                                        Object value = property.getValue();
                                        if("Double".equals(type)){
                                            value  =BeanUtil.isEmpty(value)?"0":new BigDecimal(BeanUtil.nullToEmpty(value)).toPlainString();
                                        }
                                        map.put(name, value);
                                    }
                                });
                                prop.setValues(JSONObject.toJSONString(map));
                                prop.setGeometryType(geometry.getGeometryType());
                                prop.setSource("shp");
                            }
                        }
                    }
                }
                logger.info("导入数据完成,共计{}条", i);
                return new Result().setSuccess("导入成功");
            } else {
                logger.warn("未找到shp后缀文件");
                return new Result().setError("请上传完整的文件。包含[shp,shx,bdf,prj]");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result().setSuccess("导入成功!");
    }
Shape文件加载工具:
package com.trgis.thematicdata.util;

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.type.FeatureType;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

/**
 * Shape文件加载工具
 */
public class ShapeLoader {

    private File shapefile;                 // 保存需要解析的shapefile文件
    private FeatureSource featureSource;    // 读取出来的FeatureSource
    private CoordinateReferenceSystem crs;  // 获取shape文件的坐标系统
    private MathTransform mathTransform;    // 获取坐标系转换算法数据

    public ShapeLoader(File shapefile) throws Exception {
        this.shapefile = shapefile;
        this.featureSource = loadFeatureSource(shapefile);
    }

    /**
     * 计算从shapefile转到目标坐标系统的数学参数
     * @param targetCRS
     * @return
     */
    public MathTransform calculateMathTransform(CoordinateReferenceSystem targetCRS) throws FactoryException {
        try {
            FeatureType schema = this.featureSource.getSchema();
            System.out.println("this.featureSource.getSchema() = " + schema);
            this.crs = this.featureSource.getSchema().getCoordinateReferenceSystem();
            this.mathTransform = CRS.findMathTransform(this.crs,targetCRS,true);
            return this.mathTransform;
        } catch (FactoryException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public Geometry transform(Geometry geometry) throws TransformException, FactoryException {
        if(this.mathTransform == null) {
            // 默认转到WGS84坐标系
            calculateMathTransform(CRS.decode(DefaultGISConfig.EPSG4326,true));
        }
        Geometry geom = JTS.transform(geometry,this.mathTransform);
        geom.setSRID(DefaultGISConfig.SRID);
        return geom;
    }

    /**
     * 读取shapefile获取FeatureSource
     * @param shapefile
     */
    private FeatureSource loadFeatureSource(File shapefile) {
        if(shapefile != null) {
            if(this.shapefile == null) this.shapefile = shapefile;
            ShapefileDataStore dataStore = null;
            try {
                dataStore = new ShapefileDataStore(shapefile.toURI().toURL());
                if(dataStore != null) {
                    this.featureSource = dataStore.getFeatureSource();
                    dataStore.setCharset(Charset.forName("UTF-8"));
                    if(this.featureSource != null){
                        // 获取坐标系统
                        this.crs = featureSource.getSchema().getCoordinateReferenceSystem();
                    }
                } else {
                    throw new IOException("加载shapefile时出错");
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return this.featureSource;
    }

    public FeatureSource getFeatureSource() {
        return featureSource;
    }

    public FeatureCollection getFeatureCollection() throws IOException {
        if(this.getFeatureSource() != null) {
            return getFeatureSource().getFeatures();
        }
        return null;
    }
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
SHP文件是存储地理空间数据的一种常见的GIS矢量数据格式。通常情况下,SHP文件是由多个文件组成的集合,包括.shp、.shx和.dbf文件。其中,.shp文件包含了空间几何对象的几何信息,.shx文件shp文件的索引文件,用于快速访问.shp文件中的几何对象,.dbf文件则存储了与几何对象相关的属性数据。 要对SHP文件进行解析,首先需要了解其文件结构。可以使用第三方库或软件进行解析,如GDAL、ArcGIS、QGIS等。以GDAL为例,可以使用GDAL库中的Open函数读取.shp文件,并获取其中的几何对象和属性数据。通过对shp文件的读取,可以了解其坐标系信息、几何对象类型(如点、线、面等)、坐标点的位置等。 对于压缩的SHP文件,通常是将多个SHP文件进行压缩打包而成的。解析时需要先解压缩文件,即还原出原始的.shp、.shx和.dbf文件。然后再按照上述方法对解压缩后的文件进行解析。 在解析SHP文件时,还可以进行一些其他的操作,比如对空间几何对象进行空间分析、属性查询等。通过解析SHP文件,可以获取到地理数据中的几何信息和属性信息,进而进行地理空间分析和可视化等操作。 总之,解析SHP文件是指对其文件结构进行解析,提取出其中的几何对象和属性数据。通过了解SHP文件文件结构和使用相关的库或软件,可以对SHP文件进行解析和进一步的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值