Java 读取shape文件

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

}

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

时间: 2025-01-16 15:42:21

Java 读取shape文件的相关文章

java读取大文件 超大文件的几种方法

计算机技术学习用书: 编程技术资料:http://myitbook.taobao.com/  电脑技术群:291644908    用技术改变人生,欢迎您的加入 java 读取一个巨大的文本文件既能保证内存不溢出又能保证性能 2010-09-25 11:18:50|  分类: 默认分类 |字号 订阅 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.Rando

java读取 500M 以上文件,java读取大文件

java 读取txt,java读取大文件 设置缓存大小BUFFER_SIZE ,Config.tempdatafile是文件地址 来源博客http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/ package com.yjf.util;import java.io.File;import java.io.RandomAccessFile;import java.nio.MappedByteBuffer;imp

Java编程:使用Java读取Excel文件内容

微软的ODBC驱动程序把工作表中的第一行作为列名(译者注:即字段名),工作表名作为数据库表名. 要通过JDBC访问工作表,我们还必须创建一个新的ODBC数据源,在Windows 2000系统上创建数据源的过程如下: 进入“控制面板” --> “管理工具” --> “数据源(ODBC)”,(译者注:打开后选择系统DSN),点击添加,在弹出窗口中选择“Driver do Microsoft Excel(*.xls)” 然后在数据源名处输入一个名字myexcel(译者注:相当于数据库名),然后点击“

Java读取Properties文件的六种方法

使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2.使用java.util.ResourceBundle类的getBundle()方法 示例: ResourceBundle rb

java读取TXT文件的方法

java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了.接下来需要解读成乙方可以理解的东西 既然你使用了FileInputStream().那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据 解读完成后要输出

转载:java基础学习总结——java读取properties文件总结

java基础学习总结--java读取properties文件总结 一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的

java读取XML文件的四种方式

java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> <NO>A1234</NO> <ADDR>河南省郑州市</ADDR> </VALUE> <VALUE> <NO>B1234</NO> <ADDR>河南省郑州市二七区&

java读取TXT文件的方法 (转)

转自:http://www.cnblogs.com/manongxiaojiang/archive/2012/10/13/2722068.html java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了.接下来需要解读成乙方可以理解的东西 既然你使用了F

Java读取.properties文件

例1: 创建一个config文件夹 config文件夹中有一个Properties.properties文件 内容为: capitalLetter=ABCDE smallLetter=abcde 注意:config文件夹与包含Test类的包为同一级 import java.io.IOException; import java.util.Properties; public class Test { public static void main(String[] args) { Propert