geotools快速入門
GeoTools
是一個(gè)開放源代碼(LGPL)Java代碼庫,它提供了符合標(biāo)準(zhǔn)的方法來處理地理空間數(shù)據(jù),例如實(shí)現(xiàn)地理信息系統(tǒng)(GIS)豆拨。
步驟
參照官網(wǎng):https://geotools.org/
我使用的是idea
其實(shí)我創(chuàng)建的時(shí)候并不是按照官網(wǎng)上的步驟來的,因?yàn)槲冶容^習(xí)慣從https://start.spring.io/
上面構(gòu)建一個(gè)新的maven項(xiàng)目,填寫命名下載壓縮包产还,然后在idea中打開。
pom中增加依賴包:
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>24-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>
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
測試一下
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2019, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
package org.geotools.tutorial.quickstart;
import java.io.File;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
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;
/**
* Prompts the user for a shapefile and displays the contents on the screen in a map frame.
*
* <p>This is the GeoTools Quickstart application used in documentationa and tutorials. *
*/
public class Quickstart {
/**
* GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
* contents on the screen in a map frame
*/
public static void main(String[] args) throws Exception {
// display a data store file chooser dialog for shapefiles
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
// Now display the map
JMapFrame.showMap(map);
}
}
運(yùn)行嘀趟,打開shp文件
連接PostgreSQL數(shù)據(jù)庫
在網(wǎng)上找到geotools連接數(shù)據(jù)庫的資料脐区,
參考:https://www.cnblogs.com/tuboshu/p/10752286.html
但是發(fā)現(xiàn)導(dǎo)入依賴失敗
最后看到這個(gè):http://www.reibang.com/p/e04f486b7954
但是還是失敗了,經(jīng)過多次嘗試她按,終于找到正確的依賴包牛隅。
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>24-SNAPSHOT</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- Provides support for PostGIS. Note the different groupId -->
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
接下來就是上代碼:
package com.geotools.geotoolsdemo;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
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.geotools.data.postgis.PostgisNGDataStoreFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @program: geotoolsdemo
* @description: 連接數(shù)據(jù)庫
* @author: zhudan
* @create: 2020/6/18 18:45
*/
public class PostGis {
/**
* @param dbtype: 數(shù)據(jù)庫類型,postgis or mysql
* @param host: ip地址
* @param port: 端口號(hào)
* @param database: 需要連接的數(shù)據(jù)庫
* @param userName: 用戶名
* @param password: 密碼
* @param tableName: a需要連接的表名
* @return: 返回為FeatureCollection類型
*/
private static SimpleFeatureCollection connAndgetCollection(String dbtype, String host, String port,
String database, String userName, String password, String tableName) {
Map<String, Object> params = new HashMap<String, Object>();
DataStore pgDatastore = null;
params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbtype); //需要連接何種數(shù)據(jù)庫酌泰,postgis or mysql
params.put(PostgisNGDataStoreFactory.HOST.key, host);//ip地址
params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port));//端口號(hào)
params.put(PostgisNGDataStoreFactory.DATABASE.key, database);//需要連接的數(shù)據(jù)庫
params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");//架構(gòu)
params.put(PostgisNGDataStoreFactory.USER.key, userName);//需要連接數(shù)據(jù)庫的名稱
params.put(PostgisNGDataStoreFactory.PASSWD.key, password);//數(shù)據(jù)庫的密碼
SimpleFeatureCollection fcollection = null;
try {
//獲取存儲(chǔ)空間
pgDatastore = DataStoreFinder.getDataStore(params);
//根據(jù)表名獲取source
SimpleFeatureSource fSource = pgDatastore.getFeatureSource(tableName);
if (pgDatastore != null) {
System.out.println("系統(tǒng)連接到位于:" + host + "的空間數(shù)據(jù)庫" + database
+ "成功媒佣!");
fcollection = fSource.getFeatures();
} else {
System.out.println("系統(tǒng)連接到位于:" + host + "的空間數(shù)據(jù)庫" + database
+ "失敗陵刹!請(qǐng)檢查相關(guān)參數(shù)");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("系統(tǒng)連接到位于:" + host + "的空間數(shù)據(jù)庫" + database
+ "失斈椤!請(qǐng)檢查相關(guān)參數(shù)");
}
return fcollection;
}
public static void main(String[] args) {
//調(diào)用方法
SimpleFeatureCollection featureColls = PostGis.connAndgetCollection("postgis", "localhost", "5432", "beijing", "postgres", "123456", "planet_osm_point");
SimpleFeatureIterator itertor = featureColls.features();
//循環(huán)讀取feature衰琐,itertor.hasNext()表示游標(biāo)下一個(gè)是否有數(shù)據(jù)也糊,有返回ture,否則為false
while (itertor.hasNext()) {
//獲取每一個(gè)要素
SimpleFeature feature = itertor.next();
System.out.println(feature.getAttribute("planet_osm_point"));
}
}
}
注:數(shù)據(jù)庫要導(dǎo)入空間數(shù)據(jù),參考這篇文章https://blog.csdn.net/TcCookEgg/article/details/102496421