企业建设网站流程,公司法人变更流程,犀牛云网站建设,网站多个用户怎样建设对于GIS开发者而言#xff0c;矢量数据是我们经常要用到的#xff0c;而shape数据是矢量数据中最常用的格式#xff0c;因此解析shape数据也是作为GIS软件开发人员必备的基础技能#xff0c;而GeoTools无疑是Java最好用来处理GIS数据的三方库#xff0c;下面例子是简单的g…对于GIS开发者而言矢量数据是我们经常要用到的而shape数据是矢量数据中最常用的格式因此解析shape数据也是作为GIS软件开发人员必备的基础技能而GeoTools无疑是Java最好用来处理GIS数据的三方库下面例子是简单的geotools读取shape的例子1、pom依赖包引入 dependenciesdependencygroupIdorg.geotools/groupIdartifactIdgt-shapefile/artifactIdversion25.2/version/dependency/dependencies2、解析函数
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]);//读取bboxReferencedEnvelope 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();//获取当前矢量数据有哪些属性字段值ListAttributeDescriptor 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();}}}
希望对您有帮助发财的小手点点赞~