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. 測試

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章