Java使用Geotools读取shape矢量数据

【Java使用Geotools读取shape矢量数据】作为GIS开发者而言,矢量数据是我们经常要用到的,而shape数据是矢量数据中最常用的格式,因此解析shape数据也是作为GIS软件开发人员必备的基础技能,而GeoTools无疑是Java最好用来处理GIS数据的三方库,当然这只是GeoTools的冰山一角,后面我也会慢慢的去分享GeoTools的更多用法。
今天就简单来梳理下shape数据的解析获取,环境是maven工程,首先是maven依赖:
Java使用Geotools读取shape矢量数据
文章图片
Java使用Geotools读取shape矢量数据
文章图片

4.0.0 org.dudu.liuyang gis 0.0.1-SNAPSHOT central aliyun maven https://maven.aliyun.com/repository/central false true osgeo-releases OSGeo Nexus Release Repository https://repo.osgeo.org/repository/release/ false true osgeo-snapshots OSGeo Nexus Snapshot Repository https://repo.osgeo.org/repository/snapshot/ false true geosolutions geosolutions repository https://maven.geo-solutions.it/ false true org.geotools gt-shapefile 25.2 org.springframework.boot spring-boot-maven-plugin 2.2.8.RELEASE

View Code 下面就是简单的代码操作,其实如果只是简单的读取,就很简单,下面是主要代码。
Java使用Geotools读取shape矢量数据
文章图片
Java使用Geotools读取shape矢量数据
文章图片
package 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 = "F:\\data\\vector\\shape\\line\\xian_sheng.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 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(); }}}

View Code 当然GeoTools对于shape数据的操作非常多,除了简单的读取,还有高级的过滤查询,当然这个就需要借助ECSQL(其实在我看来就是sql语句的条件查询,只是用一种标准把它定义了出来),还有就是对矢量数据的增删改查,这些我都会在后期逐个分享。

    推荐阅读