码迷,mamicode.com
首页 > 编程语言 > 详细

Java 读取shape文件

时间:2019-12-28 18:54:00      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:pac   ast   win   download   led   char   dep   tool   jdb   

C# 的话建议使用ArcEngine进行开发,由于各版本不兼容,改为采用基于Java 的GeoTool进行读取

 

pom依赖如下

<properties>
<geotools.version>19.1</geotools.version>
</properties>

<repositories>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<id>geosolutions</id>
<name>geosolutions repository</name>
<url>http://maven.geo-solutions.it/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

<dependency>
<groupId>nl.pdok</groupId>
<artifactId>geoserver-manager</artifactId>
<version>1.7.0-pdok2</version>
</dependency>

 

<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>

 

 

 

package readshape;
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.simple.SimpleFeature;
import org.opengis.filter.Filter;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;


import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

public class Read {

public static void main(String[] args){
String path1 = "E:\\Test\\TestOpenLayers\\data\\wafangdian_0\\wafangdianshi_0.shp" ;
//读取shp
SimpleFeatureCollection colls1 = readShp(path1);
//拿到所有features
SimpleFeatureIterator iters = colls1.features();

//遍历打印
while(iters.hasNext()){


SimpleFeature sf = iters.next();
Object ss= sf.getDefaultGeometry();
System.out.println(ss);
System.err.println(sf.getDefaultGeometryProperty().getValue());
Geometry shape = (Geometry)ss;
System.err.println(shape.toText());
//打印出来的都是相同的内容 MULTIPOLYGON (((107.92820000000006 26.66963000000004, 107.92820000000006 26.69963000000007, 107.95820000000003 26.69963000000007, 107.95820000000003 26.66963000000004, 107.92820000000006 26.66963000000004)))
// 可以直接拼接sql写入到postgresql

/*INSERT INTO table( geom )
VALUES
(
st_geomfromtext(‘MULTIPOLYGON (((107.92820000000006 26.66963000000004, 107.92820000000006 26.69963000000007, 107.95820000000003 26.69963000000007, 107.95820000000003 26.66963000000004, 107.92820000000006 26.66963000000004)))‘, 4326)

)
*/

// class com.vividsolutions.jts.geom.MultiPolygon

if(ss instanceof Polygon){
Polygon polygon = (Polygon)ss;

Coordinate[] coordinates= polygon.getCoordinates();
}

else if(ss instanceof MultiPolygon){
MultiPolygon multiPolygon = (MultiPolygon)ss;
String mult=multiPolygon.toString();
Coordinate[] coordinates= multiPolygon.getCoordinates();
System.err.println(coordinates.length);

}

System.err.println(ss.getClass().toString());


System.out.println(sf.getID() + " , " + sf.getAttributes());
}
}

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 ;
}

}

Java 读取shape文件

标签:pac   ast   win   download   led   char   dep   tool   jdb   

原文地址:https://www.cnblogs.com/zt2710/p/12112528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!