前言
? 早就聽聞大名鼎鼎的GeoTools,因?yàn)樽约翰桓鉐ava,所以之前沒用過,
背景
? 最近有個(gè)需求,一個(gè)白模系統(tǒng),具體是數(shù)據(jù)是用SDE導(dǎo)入到postgresql中,然后用arcgis server發(fā)布了矢量,最后用 arcgis api? for js 4.x拉伸,得到有高度的白模。以前的數(shù)據(jù)都是通過sde導(dǎo)入的,現(xiàn)在的需求是要通過前端,用戶自己去更新矢量數(shù)據(jù)。本系列只涉及讀取shp數(shù)據(jù)并插入到SDE連接的PostgreSQL已有表中。
正常來說客戶的數(shù)據(jù)量不大,可以用前端來做的,前端也有庫解析shp文件,然后利用FeatureLayer.applyEdits() 實(shí)現(xiàn)跟數(shù)據(jù)庫的操作,但是我還是想嘗試下GeoTools,是個(gè)學(xué)習(xí)的機(jī)會(huì),另外就是覺得前端不適合處理數(shù)據(jù),
環(huán)境
Windows 10
? IntelliJ IDE Ultimate 2021.3
PostgreSql 9.4
PostGIS Bundle 2.2 for PostgreSQL ×64 9.4
ArcGIS 10.4.1
ArcGIS Server 10.4.1
ArcGIS API for JavaScript 4.24
geotools 27.x
?步驟
一.找到GeoTools官網(wǎng)
會(huì)看到給出了官網(wǎng)列出幾種環(huán)境的搭建方式,我們選擇在IntelliJ IDE搭建環(huán)境:
?
? ? ? ? ? ? ? ?
二.安裝jdk
jdk版本有很多,目前已經(jīng)到Java20,但是感覺主流還是Java8,至少搭建GeoTools環(huán)境還是推薦用jdk1.8,尤其新手不要自找麻煩。下表展示了GeoTools與Java版本的對應(yīng)關(guān)系(表格來源《GeoTools 地理信息系統(tǒng)開發(fā)》表2-1):
?
? ? ? ? ? ?
? ? 另外注意設(shè)置環(huán)境變量
三.新建工程
1.順序依次為:新建工程,選擇Maven,單擊"Create from archetype",選擇“org.apache.maven.archetypes:maven-archetype-quickstart”
? ? ? ? ??
? 2.單擊Next,填寫信息:
? ? ? ? ?
?? 3.保持默認(rèn),單擊Finish:
? ? ? ? ??
? 4.創(chuàng)建后的工程為(紅可以看到色框在轉(zhuǎn)圈像是下載包):
? 5.完全結(jié)束后是這樣子:
? 6.運(yùn)行下App文件,會(huì)打印處我們熟悉的“Hello World”:
?四.將Jar包添加到工程
首先官方文檔有個(gè)提示,啟用離線模式。原文是“如果您按照本教程進(jìn)行操作,則可能已經(jīng)提供了預(yù)加載的 Maven 存儲(chǔ)庫。我們可以使用離線模式來確保 Maven 不會(huì)嘗試下載任何依賴項(xiàng)”, 讓設(shè)置里面把"Work offline”打個(gè)勾。但是我們根據(jù)官方教程操作,發(fā)現(xiàn)沒有預(yù)加載庫,所以這個(gè)選項(xiàng)務(wù)必不要打勾,否則依賴會(huì)下載失??!我們需要在線下載依賴。
?
?
?
?
1.打開項(xiàng)目根目錄下的 pom.xml 文件。您可以看到我們之前通過向?qū)л斎氲囊恍┬畔?。主要涉及到GeoTools的版本、依賴,存儲(chǔ)庫。不過為了加快速度,直接復(fù)制一份過去得了:


1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.geotools</groupId> 8 <artifactId>tutorial</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <name>tutorial</name> 12 <!-- FIXME change it to the project's website --> 13 <url>http://www.example.com</url> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <maven.compiler.source>1.7</maven.compiler.source> 18 <maven.compiler.target>1.7</maven.compiler.target> 19 <geotools.version>28-SNAPSHOT</geotools.version> 20 <maven.deploy.skip>true</maven.deploy.skip> 21 </properties> 22 23 <dependencies> 24 <dependency> 25 <groupId>junit</groupId> 26 <artifactId>junit</artifactId> 27 <version>4.11</version> 28 <scope>test</scope> 29 </dependency> 30 <dependency> 31 <groupId>org.geotools</groupId> 32 <artifactId>gt-shapefile</artifactId> 33 <version>${geotools.version}</version> 34 </dependency> 35 <dependency> 36 <groupId>org.geotools</groupId> 37 <artifactId>gt-swing</artifactId> 38 <version>${geotools.version}</version> 39 </dependency> 40 <dependency> 41 <groupId>org.geotools.jdbc</groupId> 42 <artifactId>gt-jdbc-postgis</artifactId> 43 <version>${geotools.version}</version> 44 </dependency> 45 </dependencies> 46 47 <repositories> 48 <repository> 49 <id>osgeo</id> 50 <name>OSGeo Release Repository</name> 51 <url>https://repo.osgeo.org/repository/release/</url> 52 <snapshots><enabled>false</enabled></snapshots> 53 <releases><enabled>true</enabled></releases> 54 </repository> 55 <repository> 56 <id>osgeo-snapshot</id> 57 <name>OSGeo Snapshot Repository</name> 58 <url>https://repo.osgeo.org/repository/snapshot/</url> 59 <snapshots><enabled>true</enabled></snapshots> 60 <releases><enabled>false</enabled></releases> 61 </repository> 62 </repositories> 63 64 65 <build> 66 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 67 <plugins> 68 <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> 69 <plugin> 70 <artifactId>maven-clean-plugin</artifactId> 71 <version>3.1.0</version> 72 </plugin> 73 <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> 74 <plugin> 75 <artifactId>maven-resources-plugin</artifactId> 76 <version>3.0.2</version> 77 </plugin> 78 <plugin> 79 <artifactId>maven-compiler-plugin</artifactId> 80 <version>3.8.0</version> 81 </plugin> 82 <plugin> 83 <artifactId>maven-surefire-plugin</artifactId> 84 <version>2.22.1</version> 85 </plugin> 86 <plugin> 87 <artifactId>maven-jar-plugin</artifactId> 88 <version>3.0.2</version> 89 </plugin> 90 <plugin> 91 <artifactId>maven-install-plugin</artifactId> 92 <version>2.5.2</version> 93 </plugin> 94 <plugin> 95 <artifactId>maven-deploy-plugin</artifactId> 96 <version>2.8.2</version> 97 </plugin> 98 <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> 99 <plugin> 100 <artifactId>maven-site-plugin</artifactId> 101 <version>3.7.1</version> 102 </plugin> 103 <plugin> 104 <artifactId>maven-project-info-reports-plugin</artifactId> 105 <version>3.0.0</version> 106 </plugin> 107 </plugins> 108 </pluginManagement> 109 </build> 110 </project>
復(fù)制完畢后發(fā)現(xiàn)會(huì)有報(bào)錯(cuò)現(xiàn)象:
? 2.右鍵項(xiàng)目,選擇Maven,選擇"Reload project":
? 3.我們發(fā)現(xiàn)可以了:
? 4.可以到C盤相關(guān)文件夾里面看到相關(guān)的文件都下載完成(其它兩個(gè)27和30版本是之前踩坑留下的,為了寫博客重新用了28版本):
? 搭建環(huán)境部分到此結(jié)束,到官網(wǎng)后面有個(gè)“Quickstart Application”根據(jù)1~6步進(jìn)行操作,會(huì)顯示出地圖的。
?
后續(xù)?
2023.10.11更新
最近要操作GeoJson, 所以pom.xml需要引入依賴,
<dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>${geotools.version}</version> </dependency>
代碼中需要添加
import org.geotools.geojson.geom.GeometryJSON;
?
?
如果要查看歷史版本的geotools文檔,需要到官方文檔右側(cè)的blog頁面右側(cè)找到相應(yīng)版本,然后在左側(cè)找到userguide下載,解壓后打開index.html即可,另外github倉庫也有更新日志。
關(guān)于版本選擇,為何我用了27.x,因?yàn)槟壳癑ava主流還是Java8,或者說公司項(xiàng)目用的Java8:
?
參考資料
有些資料可能沒參考,只是覺得不錯(cuò),所以收藏一下
1.《GeoTools 地理信息系統(tǒng)開發(fā)》?王項(xiàng) 劉鈞文 王新寧 孫運(yùn)娟
2.? Getting started with geotools.org using IntelliJ IDEA 2020(油管視頻)
3.Geotools簡介以及quickstsrt加載shp文件并顯示
4.geoTools18.4開發(fā)環(huán)境快速搭建,使用java可視化讀取shapefile文件_idea配置geotools_GIS開發(fā)者的博客-CSDN博客(不使用Maven)
5.GeoTools, the Java GIS toolkit Files?(geotools包離線下載)
6.?Introduction to GeoTools (概要,及一些常用的用法)
7.IDEA運(yùn)行時(shí)報(bào)錯(cuò)“類文件具有錯(cuò)誤的版本 55.0, 應(yīng)為 52.0”的解決方法文章來源:http://www.zghlxwxcb.cn/news/detail-435857.html
8.GeoTools應(yīng)用:提取Shape文件屬性列頭信息(1)(本地導(dǎo)入依賴)文章來源地址http://www.zghlxwxcb.cn/news/detail-435857.html
到了這里,關(guān)于shp數(shù)據(jù)插入sde連接的PostgreSQL庫(一)----基于 IntelliJ IDE的GeoTools快速搭建環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!