geotools 读取shape文件


前言

对于GIS开发者而言,矢量数据是我们经常要用到的,而shape数据是矢量数据中最常用的格式,因此解析shape数据也是作为GIS软件开发人员必备的基础技能,而GeoTools无疑是Java最好用来处理GIS数据的三方库,下面例子是简单的geotools读取shape的例子

1、pom依赖包引入

 <dependencies>
     <dependency>
         <groupId>org.geotools</groupId>
        <artifactId>gt-shapefile</artifactId>
        <version>25.2</version>
     </dependency>
  </dependencies>

2、解析函数

package com.gis;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 * shape文件读取
 * @author ly
 *
 */
public class ShapeFileReaderTest {

    private static final String FILE_PATH = "E:\\data\\shape\\****.shp";
    
    public static void main(String[] args) {
        File file = new File(FILE_PATH);
        readShapeFile(file);
    }
    
    /**
     * 
     * @param shpFile  传递的是shape文件中的.shp文件
     */
    private static void readShapeFile(File shpFile) {
        /**
         * 直接使用shapefileDatastore,如果不知道,也可以使用工厂模式(见下个方法)
         * 建议,如果确定是shape文件,就直使用shapefileDatastore
         */
        try {
            ShapefileDataStore shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
            //这个typeNamae不传递,默认是文件名称
            FeatureSource featuresource = shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
            //读取bbox
            ReferencedEnvelope bbox = featuresource.getBounds();
            //读取投影
            CoordinateReferenceSystem crs = featuresource.getSchema().getCoordinateReferenceSystem();
            //特征总数
            int count = featuresource.getCount(Query.ALL);
            //获取当前数据的geometry类型(点、线、面)
            GeometryType geometryType = featuresource.getSchema().getGeometryDescriptor().getType();
            //读取要素
            SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featuresource.getFeatures();
            //获取当前矢量数据有哪些属性字段值
            List<AttributeDescriptor> attributes = simpleFeatureCollection.getSchema().getAttributeDescriptors();
            //
            SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
            //
            while(simpleFeatureIterator.hasNext()) {
                SimpleFeature simpleFeature = simpleFeatureIterator.next();
                attributes.stream().forEach((a) -> {
                    //依次读取这个shape中每一个属性值,当然这个属性值,可以处理其它业务
                    System.out.println(simpleFeature.getAttribute(a.getLocalName()));
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
}

希望对您有帮助,发财的小手点点赞~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gis分享者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值