geotools学习(一)IntelliJ快速入门
title: ‘geotools學習(一)IntelliJ快速入門’
date: 2021-04-29 14:08:52
tags: []
published: true
hideInList: false
feature:
isTop: false
本指南將幫助您設置IntelliJ IDE,以便使用GeoTools并遵循GeoTools教程的其余部分。
geotools學習(一)IntelliJ快速入門
預備知識
本指南假設如下:
1.您已經安裝了最新的JDK(在撰寫本文時為8)。如果沒有,Eclipse Quickstart提供了關于如何實現這一點的說明。
2.你已經安裝了IntelliJ。本文的目標是IntelliJ CE 2016;然而,至少早在13年前的版本應該可以正常工作。最終版本也應該工作良好。IntelliJ可以從JetBrains上下載,通常在普通的操作系統上可以開箱即用。
創建一個新項目
首先,我們將使用Maven快速啟動原型創建一個新項目。
1.從菜單中選擇File -> New Project。在新建項目對話框中選擇Maven項目,確保選擇了Create from prototype,并選擇org.apache.maven:maven-archetype-quickstart原型。按下一個。
image
2.下一步會詢問我們項目的基本標識信息:
GroupId: org.geotoolsArtifactId: tutorialVersion: 1.0-SNAPSHOTimage
3.點擊下一步。下面的屏幕應該可以保留默認設置。就我們的目的而言,IntelliJ的綁定Maven應該沒問題,除非版本小于3,在這種情況下,你應該考慮使用一個新的外部版本。
image
4.點擊下一步。給這個項目起個名字(這個名字只在IntelliJ內部使用),教程就能達到我們的目的。您可以根據需要更改項目位置,并希望保留更多的默認設置(推薦)
image
5.點擊完成,我們的新項目將被創建。IntelliJ將向我們展示我們新創建的Maven文件,并進行一個初始的Maven構建(在嘗試下一步之前先完成這個構建,它應該不會花很長時間)。IntelliJ還應該詢問您是否希望為Maven依賴項啟用自動導入。在本教程中,它將自動檢測我們對POM文件所做的更改并自動導入這些更改。
image
IntelliJ用一個簡單的Hello World創建了一個空的App.java !以及JUnit測試用例。您可以在Project Explorer中右鍵單擊App或AppTest并從上下文菜單中選擇run來運行它們。
向項目中添加jar包
pom.xml文件描述了項目的結構、配置、依賴關系和許多其他方面。我們將重點討論項目所需的依賴項。
下載jar Maven時,如果它下載的依賴項存在,那么Maven利用“本地存儲庫”來存儲副本。
Maven從internet上的公共存儲庫下載jar, GeoTools等項目在internet上發布其工作。
1.在項目的根目錄下打開pom.xml文件。您可以看到我們之前通過向導輸入的一些信息。
2.我們將向這個文件添加三樣東西。首先,在模塊轉換后的文件頂部,我們想要添加一個屬性元素來定義我們想要使用的地理工具的版本。雖然您可能希望嘗試不同的版本,但這本工作簿是為23-SNAPSHOT編寫的。
對于產品來說,23的穩定版本應該用于geotools.version:
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><geotools.version>23-SNAPSHOT</geotools.version></properties>3.添加一個geotools的gt-main和gt-swing的jar包依賴,用上面定義的geotools.version代替版本號
<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>4.最后,我們需要列出maven可以從其中下載GeoTools和其他所需jar的外部存儲庫。
<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>5.如果希望使用Java 8語言級別的特性(例如lambdas),需要告訴Maven使用1.8源代碼級別
<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>6.為了簡單 ,你可以在pom.xml 下載文件
你或許會發現剪切粘貼會比寫簡單的多
注意
1.如果Maven由于某些原因沒有自動下載依賴項(可能關閉了自動導入),您可以通過右鍵單擊項目并選擇Maven -> Reimport來手動下載依賴項。 2.如果您想為您的依賴項下載Javadoc,您可以再次轉到Maven上下文菜單并選擇download Documentation快速入門應用程序
現在我們的環境已經設置好了,我們可以創建一個簡單的快速啟動。本例將在屏幕上顯示一個shapefile。
1.讓我們在org.geotools.tutorial.quickstart包中創建一個名為Quickstart的類。IntelliJ可以一次性為我們創建包和類;右鍵點擊 org.geootools包在項目面板和上下文菜單中選擇新的-> Java類。
2.加入下面的代碼
/** 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 shapefilesFile 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 itMapContent map = new MapContent();map.setTitle("Quickstart");Style style = SLD.createSimpleStyle(featureSource.getSchema());Layer layer = new FeatureLayer(featureSource, style);map.addLayer(layer);// Now display the mapJMapFrame.showMap(map);} }1.我們需要下載一些示例數據來使用。http://www.naturalearthdata.com/項目是一個由北美制圖信息協會支持的偉大項目。到下面的鏈接下載一些文化載體。你可以使用頂部的“Download all 50m cultural themes”。
1:50m文化載體
請將上述數據解壓到您可以輕松找到的位置,如桌面。
1:50m Cultural Vectors
解壓到一個容易找到的地方
2.運行應用程序以打開文件選擇器。從示例數據集中選擇一個shapefile
3.應用程序將連接到您的shapefile,生成一個地圖,并顯示shapefile
代碼示例注意事項
1.shapefile不會加載到內存中——而是在每次需要時從磁盤讀取它,這種方法允許您處理比可用內存大的數據集。
2.我們在這里使用的是一種非常基本的顯示樣式,它只顯示功能概述。在下面的示例中,我們將看到如何指定更復雜的樣式
嘗試的東西
1.嘗試不同的樣本數據集2.您可以放大、縮小和顯示整個范圍,并使用select工具檢查樣本國家中的各個國家。shp文件3.下載你能找到的最大的shapefile,看看它能多快被渲染。您應該會發現,在第一次生成空間索引時需要一些時間。之后的表現應該很好,當放大時。4.一般來講,不會講shapefile加載到內存中,而是放在磁盤中創建控件索引,按需加載如果你想要求GeoTools在內存中緩存shapefile,請嘗試以下代碼:
/*** This method demonstrates using a memory-based cache to speed up the display (e.g. when* zooming in and out).** <p>There is just one line extra compared to the main method, where we create an instance of* CachingFeatureStore.*/public static void main(String[] args) throws Exception {// display a data store file chooser dialog for shapefilesFile file = JFileDataStoreChooser.showOpenFile("shp", null);if (file == null) {return;}FileDataStore store = FileDataStoreFinder.getDataStore(file);SimpleFeatureSource featureSource = store.getFeatureSource();SimpleFeatureSource cachedSource =DataUtilities.source(new SpatialIndexFeatureCollection(featureSource.getFeatures()));// Create a map content and add our shapefile to itMapContent map = new MapContent();map.setTitle("Using cached features");Style style = SLD.createSimpleStyle(featureSource.getSchema());Layer layer = new FeatureLayer(cachedSource, style);map.addLayer(layer);// Now display the mapJMapFrame.showMap(map);}這段代碼最初無法編譯,因為我們缺少一個導入。IntelliJ應該立即提示導入丟失的類。按Alt-Enter(在OS X上的^-Enter)打開一個對話框來導入缺少的類。
使用FileDataStoreFinder可以輕松地處理文件。另一種方法是使用連接參數映射。這種技術為我們提供了對如何使用shapefile的更多控制,并允許我們連接到數據庫和web功能服務器。
File file = JFileDataStoreChooser.showOpenFile("shp", null);Map<String, Object> params = new HashMap<>();params.put("url", file.toURI().toURL());params.put("create spatial index", false);params.put("memory mapped buffer", false);params.put("charset", "ISO-8859-1");DataStore store = DataStoreFinder.getDataStore(params);SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);作者:MrSwilder
鏈接:https://www.jianshu.com/p/b18cbee5af21
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
總結
以上是生活随笔為你收集整理的geotools学习(一)IntelliJ快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scala 样例类(case class
- 下一篇: GeoMesa-空间数据存储引擎入门学习