1.下载jar包(可以去我的gitee上看源码并下载也可以去geotools管网下载)
2.根目录下新建lib文件夹,将jar包放入
3.配置maven pom文件
<!-- shp解析-->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-shapefile-19.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.ejml</groupId>
<artifactId>ejml-ddense</artifactId>
<version>0.39</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/ejml-ddense-0.32.jar</systemPath>
</dependency>
<dependency>
<groupId>org.ejml</groupId>
<artifactId>ejml-core</artifactId>
<version>0.39</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/ejml-core-0.39.jar</systemPath>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-opengis-19.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-data-19.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-api-19.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-main-19.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-metadata</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-metadata-19.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-referencing-19.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gt-geojson-19.2.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.json.simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/json-simple-1.1.jar</systemPath>
</dependency>
<dependency>
<groupId>javax.measure</groupId>
<artifactId>jsr-275-1.0-beta</artifactId>
<version>2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jsr-275-1.0-beta-2.jar</systemPath>
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jts-1.13.jar</systemPath>
</dependency>
<!-- shp解析-->
3.编写工具类
public class ParsingShpFileUtils { public static void main(String[] args) throws Exception { System.out.println(ParsingShpFile("/Users/wuchao/Documents/bengzhan.shp")); } /** * 解析shp文件 * @param filePath * @return * @throws Exception */ public static Map ParsingShpFile(String filePath) throws Exception { File file = new File(filePath); if (!file.exists()){ throw new Exception("文件不存在!"); } if (!filePath.endsWith("shp")){ throw new Exception("只能指定后缀为shp的文件"); } Map map = new HashMap(); List<Map> list = new ArrayList(); //读取shp SimpleFeatureCollection colls1 = readShp(filePath); SimpleFeatureType schema = colls1.getSchema(); Name name = schema.getGeometryDescriptor().getType().getName(); ReferencedEnvelope bounds = colls1.getBounds(); //所有features SimpleFeatureIterator iters = colls1.features(); String s = name.toString(); if ("Point".equals(s)) { list = parsingPoint(iters); } else if ("MultiLineString".equals(s) || "MultiPolygon".equals(s)) { list = parsingLineOrPoly(iters); } map.put("data",list); map.put("maxX",bounds.getMaxX()); map.put("minX",bounds.getMinX()); map.put("maxY",bounds.getMaxY()); map.put("minY",bounds.getMinY()); map.put("shapeFile",name.toString()); return map; } /** * 解析点数据 * * @param iters * @return */ public static List<Map> parsingPoint(SimpleFeatureIterator iters) { List<Map> list = new ArrayList(); while (iters.hasNext()) { SimpleFeature sf = iters.next(); Map map = new HashMap(); Iterator<? extends Property> iterator = sf.getValue().iterator(); while (iterator.hasNext()) { Property property = iterator.next(); if (property.getValue() instanceof Point) { map.put("PointX", ((Point)(property.getValue())).getX()); map.put("PointY", ((Point)(property.getValue())).getY()); }else{ Name name = property.getName();//属性名称 Object value = property.getValue();//属性值 map.put(name,value); } } list.add(map); } return list; } /** * 解析线和面 * * @param iters * @return */ public static List<Map> parsingLineOrPoly(SimpleFeatureIterator iters) { List<Map> list = new ArrayList(); while (iters.hasNext()) { SimpleFeature sf = iters.next(); Map map = new HashMap(); Iterator<? extends Property> iterator = sf.getValue().iterator(); while (iterator.hasNext()) { Property property = iterator.next(); if (property.getValue() instanceof Geometry) { Geometry geometry = (Geometry) property.getValue(); Coordinate[] coordinates = geometry.getCoordinates(); List<Map> paths = new ArrayList<Map>(); for (Coordinate coordinate : coordinates) { Map path = new HashMap(); path.put("x",coordinate.x); path.put("y",coordinate.y); path.put("z",coordinate.z); paths.add(path); } map.put("path",paths); }else{ Name name = property.getName();//属性名称 Object value = property.getValue();//属性值 map.put(name,value); } } list.add(map); } return list; } public static SimpleFeatureCollection readShp(String path) { return readShp(path, null); } public static SimpleFeatureCollection readShp(String path, Filter filter) { SimpleFeatureSource featureSource = readStoreByShp(path); if (featureSource == null) return null; try { return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures(); } catch (IOException e) { e.printStackTrace(); } return null; } public static SimpleFeatureSource readStoreByShp(String path) { File file = new File(path); FileDataStore store; SimpleFeatureSource featureSource = null; try { store = FileDataStoreFinder.getDataStore(file); ((ShapefileDataStore) store).setCharset(Charset.forName("UTF-8")); featureSource = store.getFeatureSource(); } catch (IOException e) { e.printStackTrace(); } return featureSource; } }
4.gitee 地址:java-geotools: java语言使用geotools工具解决相关问题
目前更新的功能有:
- 解析shp 点线面文件 获取数据详情
- shp数据转成GeoJSON
- shp文件转esrijson,逻辑上是先将shp转成GeoJSON文件,再将GeoJSON转成esrijson
- 主要功能是将GeoJSON生成图片,同理shp也可以先转GeoJSON再生成图片。这个功能主要解决了geotools工具在处理数据时properties属性不一致问题(不过多描述)
- 2023-03-21新增根据shp文件直接生成图片代码