干货!使用Geotools解析shap数据实例
【摘要】 本文将重点介绍基于Java和Geotools来解析shap文件,并打印解析内容的过程。
本文将重点介绍基于Java和Geotools来解析shap文件,并打印解析内容的过程。
环境:jdk 1.8
maven 3.3
第一步、pom.xml 设置
<project xmlns="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文件
package com.yelang; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.junit.Test; import org.opengis.feature.Property; import org.opengis.feature.simple.SimpleFeature; public class ReadShapTest { @Test public void readShap() throws IOException { long startTime=System.currentTimeMillis(); //加载文件 String filePath = "D:/cesium code/数据/矢量数据/0319点.shp"; File file = new File(filePath); if (!file.exists()) { System.out.println("文件不存在"); return; } //map记录shapefile key-value数据 List<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); //通过store获取featurecollection FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); SimpleFeatureCollection simpleFeatureCollection=featureSource.getFeatures(); SimpleFeatureIterator itertor = simpleFeatureCollection.features(); //遍历featurecollection while (itertor.hasNext()){ Map<String,Object> data = new HashMap<String, Object>(); SimpleFeature feature = itertor.next(); Collection<Property> p = feature.getProperties(); Iterator<Property> it = p.iterator(); //遍历feature的properties while(it.hasNext()) { Property pro = it.next(); if(null != pro && null != pro.getValue()) { String field = pro.getName().toString(); String value = pro.getValue().toString(); field = field.equals("the_geom")?"wkt":field; byte[]bytes=value.getBytes("iso8859-1"); value=new String(bytes, "gbk"); System.out.println(new String(field.getBytes("iso8859-1"),"gbk") + "==" + value); data.put(field, value); } } list.add(data); } long endTime=System.currentTimeMillis(); System.out.println("当前程序耗时:"+(endTime-startTime)+"ms"); } }
第三步、查看输出结果
第四步、使用GUI工具查看结果
代码如下:
package com.yelang; import java.io.File; import java.io.IOException; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.map.FeatureLayer; import org.geotools.map.Layer; import org.geotools.map.MapContent; import org.geotools.styling.SLD; import org.geotools.styling.Style; import org.geotools.swing.JMapFrame; import org.geotools.swing.data.JFileDataStoreChooser; public class ReadShapGui { public static void main(String[] args) throws IOException { // display a data store file chooser dialog for shapefiles File file = JFileDataStoreChooser.showOpenFile("shp", null); if (file == null) { return; } FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); // Create a map content and add our shapefile to it MapContent map = new MapContent(); map.setTitle("Quickstart"); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(featureSource, style); map.addLayer(layer); // Now display the map JMapFrame.showMap(map); } }
通过以上几步,可掌握通过geotools读取数据,并进行展示的方法。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)