.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()); }
打印贴图
实例的.java在附件。
相关推荐
geotools读取shp文件及shp文件操作工具类代码.pdf
NULL 博文链接:https://javaeye-hua.iteye.com/blog/1009362
1.java实现shapefile几何数据批量读取,并封装为通用的工具类,实现shp数据的通用批量读取 2.读取后的成果数据封装成hashMap的列表,每一个Map对象存储一条shp要素的字段属性及几何图形,通过shp字段名从Map中获取...
geotools对wms的实现支持,给出了geotools如何开发一个简单的wms实现,对理解geoserver源码有很大的帮助作用。
提供通过meteoInfo+geotools解析shp文件,提取出shp文件中的数据,javaj技术实现,获取边界线数据、中心点坐标、最大、最小经纬度值等
解决geotools读取中文dbf文件时的乱码问题.经测试,可以解读绝大多数中文dbf. 马翼超荣誉提供.
该项目包含了项目所需要的jar包以及测试案例。项目使用环境为eclipse jdk1.8 1.com.webadmin.util.SHPFileReader为测试...2.可读取shp文件坐标并返回list。 3.返回dbf数据,并解决乱码问题。 4.返回prj返回坐标系名称。
Geotools本身对中文的地图文件不支持.这个补丁可以解决正确从Shp文件中读出中文的问题.将这个jar包替换Geotools类库中的同名文件.这个补丁同样也能解决Geoserver读取中文地图的问题.
Java GIS工具包geotools可以对wkt,wkb格式数据进行操作包括判断相交包含等。以及读取shp文件等等操作
《【GIS系列】GeoTools+MybatisPlus+PostGIS实现Shp数据的读取及写入数据库》文章中JTS工具