关于shp图层文件解析

图层文件包含.shp 用于存储地理形状和位置信息 .dbf 用于存储属性信息 .shx 用于索引文件 .prj 用于存储坐标系;

添加依赖:

<!--shape文件-->
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-shapefile</artifactId>
    <version>23.0</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-geojson</artifactId>
    <version>23.0</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-hsql</artifactId>
    <version>23.0</version>
</dependency>

如果geotools依赖下载不了

<!--处理geotools依赖下载不了问题-->
<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>
代码:
package com.lw.common.utils.poi;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.locationtech.jts.geom.Point;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;


/**
 * 读取解析shape文件
 * @author: cp
 * @date: 2021-02-20 15:26
 */
public class ShapefileHelper {


    public static List<Map<String, Object>> read(String path) throws IOException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        File file = getFile(path);
        if (file == null) {
            return list;
        }
//        String charset = getCharSet(path);
        String charset = "cp936";
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        ((ShapefileDataStore)store).setCharset(Charset.forName(charset));//UTF-8乱码,采用GBK
        SimpleFeatureSource featureSource = store.getFeatureSource();
        SimpleFeatureCollection collection = featureSource.getFeatures();
        SimpleFeatureIterator features = collection.features();
        while (features.hasNext()) {
            Map<String, Object> item = new HashMap<String, Object>();

            SimpleFeature f = features.next();
            Collection<Property> p = f.getProperties();
            Iterator<Property> it = p.iterator();
            while (it.hasNext()) {
                Property pro = it.next();
                String field = pro.getName().toString();
                field = field.equals("the_geom") ? "wkt" : field;
                String value = pro.getValue().toString();
                item.put(field, value);
                if(pro.getValue() instanceof Point){//如果是坐标点,分别获取横纵坐标
                    Double pointX = ((Point) pro.getValue()).getX();
                    Double pointY = ((Point) pro.getValue()).getY();
                    item.put("pointX", pointX);
                    item.put("pointY", pointY);
                }
            }
            list.add(item);
        }
        System.out.println("数据list"+list);
        return list;
    }

    private static File getFile(String path){
        File file = new File(path);
        if (file == null) {
            System.out.println("找不到路径:" + path);
        }
        return file;
    }

    /**
     * 获取shapefile字符编码
     * 如果存在.cpg文件,则从中读取,否则默认为UTF-8
     * @param path
     * @return
     */
    private static String getCharSet(String path){
        String charset = "cp936";

        int p = path.lastIndexOf(".");
        String cpg = path.substring(0,p) + ".cpg";
        File file = getFile(cpg);
        if(file != null) {
            RandomAccessFile raf = null;
            try {
                raf = new RandomAccessFile(cpg, "r");
                charset = raf.readLine();
                raf.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return charset;
    }


    public static void main(String[] args) {
        try {
            ShapefileHelper.read("C:\\Users\\Administrator\\Desktop\\rar\\dk\\DK.shp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Silenchen11

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

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

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

打赏作者

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

抵扣说明:

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

余额充值