這算是第一次接觸開源工具包,說實話剛開始有點不知所措,中途遇到很多問題的時候也感覺頭皮發麻,不過很高興自己還是堅持下來了。
geotools就不做過多的介紹了,想總結一下如何根據開源內容做自己的項目。整個過程中技術相關的內容學到了不少,但要是都寫出來篇幅就太長了,所以這篇主要是寫一些自己總結的開發流程上的東西。
一、 對要參考的開源項目有一個宏觀的把握
geotools是一個 GIS 開源工具包,官網有入門教程和一系列的參考文檔,這些東西是非常關鍵的,要想把geotools里面的各個jar包的作用弄明白,就要仔細的閱讀這些文檔。geotools的 Quickstart 講的是如何打開一個.shp文件, eclipse版本、IDEA版本的都有。Document有Featur(要素)教程,Geometry(幾何)教程,Query(查詢)教程等,要注意的是最的新版本可能不支持舊版里的一些內容。
有篇博客講了geotools的體系,非常值得參考:http://blog.csdn.net/anglestar2012/article/details/42555819
由於我要做的與幾何內容聯系比較大,所以着重參考了JTS Topology Suite(拓撲套件)類庫,這里面的類主要是用來實現具體的幾何操作,主要用到了以下三個包:
org.locationtech.jts.operation.union
org.locationtech.jts.operation.buffer
org.locationtech.jts.algorithm (PointLocator)
附上JTS的文檔地址:http://locationtech.github.io/jts/javadoc/overview-summary.html
二、多看例子
一周多的時間,我幾乎翻遍了網上所有與geotools有關的博客,之后試着對里面的代碼進行理解和仿寫。這個階段獲取的信息量非常大,有很多重復的內容,關鍵是要從中提取出有用的信息點。
三、分析自己項目的具體需求
剛開始領導布置任務的時候是說要導入JSON格式的數據,所以又看了相關的一些知識,包括JSON格式的特點、使用范圍以及如何通過Java與javascript對它進行解析與生成。
三、下載、導入jar包,配置maven
這個項目對我來講非常重要的一點就是學會了maven工具的使用,這個構建工具非常強大,geotools就是默認使用maven來做的。maven重點在於對pom.xml文件的配置,導入jar包后就要添加依賴。
這里附一個講解地址:http://blog.csdn.net/u012152619/article/category/6239920
下面是我配置的pom文件:
<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>org.geotools</groupId> <artifactId>buffer</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>buffer</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <geotools.version>19-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-main</artifactId> <version>${geotools.version}</version> </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-api</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geometry</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-jts-wrapper</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</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>
四、具體實現代碼
以前一直在用eclipse,這次做完之后把所有內容都遷到了IDEA,熟悉新IDE也花費了不少時間。二者我覺得都很好,目前的水平也感覺不出來IDEA有網上說的那么神奇,從體驗來講只是UI上的差異,不過有一點很重要,就是IDEA的結構,它沒有eclipse中Workspace的概念,在IDEA中,Project是最頂級的結構單元,然后是Module,這類項目一般按照功能划分。
這里有相關的介紹:http://blog.csdn.net/qq_35246620/article/details/65448689
package org.geotools; import java.util.ArrayList; import java.util.List; import org.geotools.geometry.jts.JTSFactoryFinder; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.io.WKTReader; import com.vividsolutions.jts.algorithm.PointLocator; public class UnionPolygon { public static void main(String[] args) { //創建4個多邊形 Coordinate[] coordinates1 = new Coordinate[] { new Coordinate(0, 0), new Coordinate(0, 1), new Coordinate(1, 2), new Coordinate(2, 1), //該點在union后會被消去 new Coordinate(2, 0), new Coordinate(0, 0), }; Coordinate[] coordinates2 = new Coordinate[] { new Coordinate(1, 0), new Coordinate(1, 1), //該點在union后會被消去 new Coordinate(2, 2), new Coordinate(3, 1), new Coordinate(3, 0), new Coordinate(1, 0)}; Coordinate[] coordinates3 = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 0), new Coordinate(2, 0), new Coordinate(2, -1), new Coordinate(0, -1), new Coordinate(0, 0), }; Coordinate[] coordinates4 = new Coordinate[] { new Coordinate(1, 0), new Coordinate(2, 0), new Coordinate(3, 0), new Coordinate(3, -1), new Coordinate(1, -1), new Coordinate(1, 0)}; GeometryFactory gf=new GeometryFactory(); Geometry g1 = gf.createPolygon(coordinates1); Geometry g2 = gf.createPolygon(coordinates2); Geometry g3 = gf.createPolygon(coordinates3); Geometry g4 = gf.createPolygon(coordinates4); //兩個合並 Geometry union = g1.union(g2); Geometry union2 = union.union(g3); //多個合並,設置多邊形數組,再通過循環依次疊加各個多邊形 Geometry[] geos=new Geometry[] {g1,g2,g3,g4}; Geometry allunion = geos[0]; for(int i=1; i<geos.length; i++) { allunion=allunion.union(geos[i]); } System.out.println(union); System.out.println(union2); System.out.println(allunion); //緩沖區建立 Geometry g3buffer=g3.buffer(1); //對第三個多邊形加緩沖區 Geometry allunionbuffer=allunion.buffer(1); //對全部合並后的多邊形加緩沖區 System.out.println(g3buffer); System.out.println(allunionbuffer); //點是否在多邊形內判斷 Coordinate point1 = new Coordinate(1, 1); PointLocator a=new PointLocator(); boolean p1=a.intersects(point1, allunion); if(p1) System.out.println("point1:"+"該點在多邊形內"); else System.out.println("point1:"+"該點不在多邊形內"); Coordinate point2 = new Coordinate(5, 5); PointLocator b=new PointLocator(); boolean p2=b.intersects(point2, allunion); if(p2) System.out.println("point2:"+"該點在多邊形內"); else System.out.println("point2:"+"該點不在多邊形內"); } }
五、小總結
做項目的提升速度其實比純看書是快很多的,因為會碰到很多問題。我的方法是把一天遇到的與內容相關的問題點都記下來,然后再看它們分別屬於什么知識范疇,最后安排時間去查閱相關的知識,重要的(比如maven)就按照體系來學習,進行宏觀把握,其他的達到能看懂、能理解、會基礎操作的水平就行。還有就是多交流吧,能解決很多問題。
說到底,學習就是一個發現問題解決問題的過程,只不過這中間不同人選取的手段方法有一些差異而已。
附 shp轉geojson網址:http://mapshaper.org/
地圖數據:http://www.osgeo.cn/filter/2102
https://www.hcharts.cn/mapdata