Shapefile屬於一種矢量圖形格式,它能夠保存幾何圖形的位置及相關屬性。但這種格式沒法存儲地理數據的拓撲信息。
其中,要組成一個Shapefile,有三個文件是必不可少的,它們分別是".shp", ".shx"與 ".dbf"文件
-
.shp— 圖形格式,用於保存元素的幾何實體。
-
.shx— 圖形索引格式。幾何體位置索引,記錄每一個幾何體在shp文件之中的位置,能夠加快向前或向后搜索一個幾何體的效率。
-
.dbf— 屬性數據格式,以dBase IV的數據表格式存儲每個幾何形狀的屬性數據。
下面將介紹如何通過Java讀取Shape文件中的內容信息
我們的pom文件
<?xml version="1.0" encoding="UTF-8"?> <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.herbert.geotool</groupId> <artifactId>geo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>19.2</version> <scope>system</scope> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-opengis</artifactId> <version>19.2</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-data</artifactId> <version>19.2</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-api</artifactId> <version>19.2</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>19.2</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-metadata</artifactId> <version>19.2</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-referencing</artifactId> <version>19.2</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>19.2</version> </dependency> <dependency> <groupId>org.json.simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>com.vividsolutions</groupId> <artifactId>jts</artifactId> <version>1.13</version> </dependency> </dependencies> </project>
具體Java代碼
package com.herbert.geotoool.util; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geojson.feature.FeatureJSON; import org.opengis.feature.simple.SimpleFeature; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.nio.charset.Charset; /** * @author :Herbert * @date :Created in 2019/12/26 17:01 * @description: * @modified By: * @version: $ */ public class ShapeModel { public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); String SHAPE_FILE = "F:\\MapData\\gisMap\\xian\\街道界線.shp"; // ShapeFile全路徑 // 使用GeoTools讀取ShapeFile文件 File shapeFile = new File(SHAPE_FILE); ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL()); //設置編碼 Charset charset = Charset.forName("GBK"); store.setCharset(charset); SimpleFeatureSource sfSource = store.getFeatureSource(); SimpleFeatureIterator sfIter = sfSource.getFeatures().features(); // 從ShapeFile文件中遍歷每一個Feature,然后將Feature轉為GeoJSON字符串 while (sfIter.hasNext()) { SimpleFeature feature = (SimpleFeature) sfIter.next(); // Feature轉GeoJSON FeatureJSON fjson = new FeatureJSON(); StringWriter writer = new StringWriter(); fjson.writeFeature(feature, writer); String sjson = writer.toString(); System.out.println("sjson===== >>>> " + sjson); } System.out.println("數據導入完成,共耗時"+(System.currentTimeMillis() - start)+"ms"); } }
讀取數據顯示:


