.shp檔案是目前大家使用頻率較高而且通用的一種格式。此篇主要說明geotools對shp檔案的讀取說明。
1、maven依賴,包含了postgis支援、epsg、和swing的支援,後續的文章基本都基于此
<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.jjxliu.geotools</groupId>
<artifactId>geotools_t1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>geotools_t1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>20-SNAPSHOT</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</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>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-jdbc</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>http://repo.boundlessgeo.com/main</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、直接上java代碼說明shp檔案讀取
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) {
// TODO Auto-generated catch block
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return featureSource ;
}
3、測試一段讀取的代碼,列印所有讀取到的simplefeature
String path1 = "G:/work/china_map/shp/BOUNT_poly.shp" ;
//讀取shp
SimpleFeatureCollection colls1 = readShp(path1);
//拿到所有features
SimpleFeatureIterator iters = colls1.features();
//周遊列印
while(iters.hasNext()){
SimpleFeature sf = iters.next();
System.out.println(sf.getID() + " , " + sf.getAttributes());
}
列印貼圖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauETZ4QmMhNDOjFGNi1SMiZGOtY2N5MTL0czYj1CMiRGO5kTZ48CX3ITN58CX5ITMw8CX05WZth2YhRHdh9CXkF2bsBXdvwVbvNmLllXZ0lmLywGZvw1LcpDc0RHaiojIsJye.jpg)
執行個體的.java在附件。