文将重点介绍基于Java和Geotools来解析shap文件,并打印解析内容的过程。
环境:jdk 1.8
maven 3.3
第一步、pom.xml 设置
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.yelang</groupId><artifactId>geotools-demo1</artifactId><version>0.0.1-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><geotools.version>24.0</geotools.version></properties><repositories><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></repositories><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>test</scope></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId><version>${geotools.version}</version></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-swing</artifactId><version>${geotools.version}</version></dependency></dependencies></project>
提示:geotools依赖包在中央仓库中不存在,如果不设置私有仓库,jar会无法下载。解决办法有两种:第一种是在pom中设置私有仓库地址;第二种是在maven的setttings.xml中设置私服镜像。需要注意的是,镜像的设置mirrorOf一定不要设置*,否则无法下载依赖。推荐如下
<mirror><id>nexus-aliyun</id><mirrorOf>central</mirrorOf><name>Nexus aliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public</url></mirror>
第二步、解析shap文件
packagecom.yelang; importjava.io.File; importjava.io.IOException; importjava.util.ArrayList; importjava.util.Collection; importjava.util.HashMap; importjava.util.Iterator; importjava.util.List; importjava.util.Map; importorg.geotools.data.FileDataStore; importorg.geotools.data.FileDataStoreFinder; importorg.geotools.data.simple.SimpleFeatureCollection; importorg.geotools.data.simple.SimpleFeatureIterator; importorg.geotools.data.simple.SimpleFeatureSource; importorg.junit.Test; importorg.opengis.feature.Property; importorg.opengis.feature.simple.SimpleFeature; publicclassReadShapTest { publicvoidreadShap() throwsIOException { longstartTime=System.currentTimeMillis(); //加载文件StringfilePath="D:/cesium code/数据/矢量数据/0319点.shp"; Filefile=newFile(filePath); if (!file.exists()) { System.out.println("文件不存在"); return; } //map记录shapefile key-value数据List<Map<String,Object>>list=newArrayList<Map<String, Object>>(); //通过store获取featurecollectionFileDataStorestore=FileDataStoreFinder.getDataStore(file); SimpleFeatureSourcefeatureSource=store.getFeatureSource(); SimpleFeatureCollectionsimpleFeatureCollection=featureSource.getFeatures(); SimpleFeatureIteratoritertor=simpleFeatureCollection.features(); //遍历featurecollectionwhile (itertor.hasNext()){ Map<String,Object>data=newHashMap<String, Object>(); SimpleFeaturefeature=itertor.next(); Collection<Property>p=feature.getProperties(); Iterator<Property>it=p.iterator(); //遍历feature的propertieswhile(it.hasNext()) { Propertypro=it.next(); if(null!=pro&&null!=pro.getValue()) { Stringfield=pro.getName().toString(); Stringvalue=pro.getValue().toString(); field=field.equals("the_geom")?"wkt":field; byte[]bytes=value.getBytes("iso8859-1"); value=newString(bytes, "gbk"); System.out.println(newString(field.getBytes("iso8859-1"),"gbk") +"=="+value); data.put(field, value); } } list.add(data); } longendTime=System.currentTimeMillis(); System.out.println("当前程序耗时:"+(endTime-startTime)+"ms"); } }
第三步、查看输出结果
第四步、使用GUI工具查看结果
packagecom.yelang; importjava.io.File; importjava.io.IOException; importorg.geotools.data.FileDataStore; importorg.geotools.data.FileDataStoreFinder; importorg.geotools.data.simple.SimpleFeatureSource; importorg.geotools.map.FeatureLayer; importorg.geotools.map.Layer; importorg.geotools.map.MapContent; importorg.geotools.styling.SLD; importorg.geotools.styling.Style; importorg.geotools.swing.JMapFrame; importorg.geotools.swing.data.JFileDataStoreChooser; publicclassReadShapGui { publicstaticvoidmain(String[] args) throwsIOException { // display a data store file chooser dialog for shapefilesFilefile=JFileDataStoreChooser.showOpenFile("shp", null); if (file==null) { return; } FileDataStorestore=FileDataStoreFinder.getDataStore(file); SimpleFeatureSourcefeatureSource=store.getFeatureSource(); // Create a map content and add our shapefile to itMapContentmap=newMapContent(); map.setTitle("Quickstart"); Stylestyle=SLD.createSimpleStyle(featureSource.getSchema()); Layerlayer=newFeatureLayer(featureSource, style); map.addLayer(layer); // Now display the mapJMapFrame.showMap(map); } }
通过以上几步,可掌握通过geotools读取数据,并进行展示的方法。