天天看点

geo-tools系列(一) geo-tools读取shapefile文件

最近项目中接触到geo-tools,去官网学习了记录一下,学习笔记。

1、maven项目的pom文件中添加geo-tools依赖

<?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.summit</groupId>
	<artifactId>HydrologyGIS</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>HydrologyGIS</name>
	<description>测试gis模块</description>
	<packaging>jar</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<skipTests>true</skipTests>
		<geotools.version>23-SNAPSHOT</geotools.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- 开发工具热启动 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>

		<!-- Springboot 自定义配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

		<!-- swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
		</dependency>

		<!-- mybatis-plus -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.0.6</version>
		</dependency>


		<!-- 操作Oracle Spatial 所需jar -->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>ojdbc6</version>
			<scope>system</scope>
			<systemPath>${basedir}/src/main/resources/lib/ojdbc6.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>sdoapi</artifactId>
			<version>2</version>
			<scope>system</scope>
			<systemPath>${basedir}/src/main/resources/lib/sdoapi.jar</systemPath>
		</dependency>

		<!-- commons-codec 工具库 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>

		<!-- json解析 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.58</version>
		</dependency>

		<!--lombok 插件 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

		<!-- geo-tools相关的依赖 -->
		<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>

		<!-- geojson支持 -->
		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-geojson</artifactId>
			<version>${geotools.version}</version>
		</dependency>
		<!-- 绘图 -->
		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-render</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>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

           

拉取依赖的过程会比较慢……

2. 读取shapefile

/**  
 * All rights Reserved, Designed By www.summit.com.cn
 */
package com.summit.gis.dxs.geotools.tutorial;

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

import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

/**   
 * @ClassName:  QuickStart   
 * @Description: geotools 入门引导-- 读取shapefile文件
 * @author: zhangcj
 * @date:   2019年9月29日 下午5:11:51   
 */
public class QuickStart {

//	public static void showShape() throws IOException {
//		File file = JFileDataStoreChooser.showOpenFile("shp", null);
//		if(file == null) {
//			return ;
//		}
//		
//		Map<String,Object> params = new HashMap<>(4);
//		params.put("url", file.toURI().toURL());
//		params.put("create spatial index", false);
//        params.put("memory mapped buffer", false);
//        params.put("charset", "UTF-8");
//        DataStore store = DataStoreFinder.getDataStore(params);
//        SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
//        // 创建地图
//		MapContent map = new MapContent();
//		map.setTitle("QuickStart");
//		Style style = SLD.createSimpleStyle(featureSource.getSchema());
//		Layer layer = new FeatureLayer(featureSource, style);
//		map.addLayer(layer);
//		// 展示地图
//		JMapFrame.showMap(map);
//
//		
//	}
	
	
	
	public static void showShape() {
		File file = JFileDataStoreChooser.showOpenFile("shp", null);
		if(file == null) {
			return ;
		}
		
		try {
//			FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);
			ShapefileDataStore dataStore = (ShapefileDataStore) FileDataStoreFinder.getDataStore(file);
//			设置读取shapefile文件的编码格式
			dataStore.setCharset(Charset.forName("UTF-8"));
			SimpleFeatureSource featureSource = dataStore.getFeatureSource();
			
			// 创建地图
			MapContent map = new MapContent();
			map.setTitle("QuickStart");
//			Style style = SLD.createSimpleStyle(featureSource.getSchema());
			Style style = SLD.createLineStyle(new Color(0x1890ff), 2);
			Layer layer = new FeatureLayer(featureSource, style);
			map.addLayer(layer);
			// 展示地图
			JMapFrame.showMap(map);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		showShape();
	}
}

           

上述代码中对于DataStore 的获取分别使用了不同的方式,参考

3. 测试

geo-tools系列(一) geo-tools读取shapefile文件
geo-tools系列(一) geo-tools读取shapefile文件