图层文件包含.shp 用于存储地理形状和位置信息 .dbf 用于存储属性信息 .shx 用于索引文件 .prj 用于存储坐标系;
添加依赖:
<!--shape文件--> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>23.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>23.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>23.0</version> </dependency>
如果geotools依赖下载不了
<!--处理geotools依赖下载不了问题--> <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>
代码: package com.lw.common.utils.poi; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.opengis.feature.Property; import org.opengis.feature.simple.SimpleFeature; import org.locationtech.jts.geom.Point; import java.io.*; import java.nio.charset.Charset; import java.util.*; /** * 读取解析shape文件 * @author: cp * @date: 2021-02-20 15:26 */ public class ShapefileHelper { public static List<Map<String, Object>> read(String path) throws IOException { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); File file = getFile(path); if (file == null) { return list; } // String charset = getCharSet(path); String charset = "cp936"; FileDataStore store = FileDataStoreFinder.getDataStore(file); ((ShapefileDataStore)store).setCharset(Charset.forName(charset));//UTF-8乱码,采用GBK SimpleFeatureSource featureSource = store.getFeatureSource(); SimpleFeatureCollection collection = featureSource.getFeatures(); SimpleFeatureIterator features = collection.features(); while (features.hasNext()) { Map<String, Object> item = new HashMap<String, Object>(); SimpleFeature f = features.next(); Collection<Property> p = f.getProperties(); Iterator<Property> it = p.iterator(); while (it.hasNext()) { Property pro = it.next(); String field = pro.getName().toString(); field = field.equals("the_geom") ? "wkt" : field; String value = pro.getValue().toString(); item.put(field, value); if(pro.getValue() instanceof Point){//如果是坐标点,分别获取横纵坐标 Double pointX = ((Point) pro.getValue()).getX(); Double pointY = ((Point) pro.getValue()).getY(); item.put("pointX", pointX); item.put("pointY", pointY); } } list.add(item); } System.out.println("数据list"+list); return list; } private static File getFile(String path){ File file = new File(path); if (file == null) { System.out.println("找不到路径:" + path); } return file; } /** * 获取shapefile字符编码 * 如果存在.cpg文件,则从中读取,否则默认为UTF-8 * @param path * @return */ private static String getCharSet(String path){ String charset = "cp936"; int p = path.lastIndexOf("."); String cpg = path.substring(0,p) + ".cpg"; File file = getFile(cpg); if(file != null) { RandomAccessFile raf = null; try { raf = new RandomAccessFile(cpg, "r"); charset = raf.readLine(); raf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return charset; } public static void main(String[] args) { try { ShapefileHelper.read("C:\\Users\\Administrator\\Desktop\\rar\\dk\\DK.shp"); } catch (IOException e) { e.printStackTrace(); } } }