GDAL for Java環境搭建

+

前言

  本文主要參考別人的一篇文章:windows下gdal的java開發環境搭建。爲什麼自己還要寫一篇,一方面擔心他把文章刪了,另一方面,裏面還是有其它坑。

  之前寫過一篇GeoTools環境搭建,今天來寫一篇GDAL的環境搭建。

                                (看到股票大跌,都沒心情寫了,摩根系真的噁心人)

背景

  最近有個需求是實現shp、gdb、geojson用GeoTools解析GIS數據能力有限,所以改用GDAL。

  關於這部分的描述引用一篇文章如下:

   Java處理GIS數據主要有兩種思路:直接使用JNI方式調用gdal/ogr庫;使用GeoTools的gt-ogr-jni插件,本質還是JNI方式調用gdal/ogr庫。

    使用GeoTools的gt-ogr-jni插件,可以藉助GeoTools庫的強大封裝,實現各種查詢分析功能,使用更便捷。但是,實際使用發現gt-ogr-jni插件並不完善,對於shapefile、geojson之類的數據格式支持較好,對於FileGDB數據沒有進行有效測試,內部會出錯。

    直接使用JNI方式調用gdal/ogr庫,需要對GIS數據集有更深入的理解,使用起來更爲靈活,與GeoTools的數據封裝轉換需要自行實現。實際上gdal/ogr庫已經封裝的非常完善,使用也很便捷,在GeoTools的gt-ogr-jni插件處理有問題時,推薦採用此種方式。

環境

  Windows 10 ×64

   IntelliJ IDE Ultimate 2021.3

   JDK 1.8.0

步驟

  前提是已經搭建好Java環境

  一.下載GDAL

  ①我的電腦是win64位,最新版是最後面那個:

  

 

  ②點進去之後下載第一個(下載速度會比較慢,即便fq也慢):

   ③下載完畢得到一個zip文件:

   ④解壓後得到:

 二.設置環境變量

網上好多是把gdal文件放到jdk中,這樣做感覺不太好,這也是我採用文章開頭那位博主搭建方法的原因。

①其實就是設置下GDAL的目錄,方便其它地方用:

 ②添加path環境變量

 

  ③創建PROJ_LIB變量,我們發現這裏有三個proj相關的文件夾,原作者選擇的是proj7,但是我選那個會報錯,選了proj9好了,所以這裏得自己嘗試

 

  ④設置完成後,要重啓電腦,這個很重要,不然設置的環境變量不生效。

 三.拷貝gdal.jar到Java工程項目中

  Java項目中新建lib文件夾,與src文件夾同級;拷貝gdal-3-7-2/bin/gdal/java/gdal.jar文件到剛纔新建的lib文件夾中

 

 

四.在pom.xml中添加依賴

我的完整文件如下,因爲是在geotools的基礎上搭建的環境,如果不需要geotools的,把geotools相關的刪掉即可

  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>https://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     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 20     <geotools.version>27-SNAPSHOT</geotools.version>
 21     <maven.deploy.skip>true</maven.deploy.skip>
 22   </properties>
 23 
 24   <dependencies>
 25     <dependency>
 26       <groupId>junit</groupId>
 27       <artifactId>junit</artifactId>
 28       <version>4.13.2</version>
 29       <scope>test</scope>
 30     </dependency>
 31 
 32     <dependency>
 33       <groupId>org.geotools</groupId>
 34       <artifactId>gt-shapefile</artifactId>
 35       <version>${geotools.version}</version>
 36     </dependency>
 37     <dependency>
 38       <groupId>org.geotools</groupId>
 39       <artifactId>gt-swing</artifactId>
 40       <version>${geotools.version}</version>
 41     </dependency>
 42     <dependency>
 43       <groupId>org.geotools.jdbc</groupId>
 44       <artifactId>gt-jdbc-postgis</artifactId>
 45       <version>${geotools.version}</version>
 46     </dependency>
 47     <dependency>
 48       <groupId>org.geotools</groupId>
 49       <artifactId>gt-geojson</artifactId>
 50       <version>${geotools.version}</version>
 51     </dependency>
 52     <dependency>
 53     <groupId>org.gdal</groupId>
 54     <artifactId>gdal</artifactId>
 55     <version>3.7.2</version>
 56     <scope>system</scope>
 57     <systemPath>${project.basedir}/lib/gdal.jar</systemPath>
 58     </dependency>
 59     <dependency>
 60       <groupId>org.json</groupId>
 61       <artifactId>json</artifactId>
 62       <version>20230618</version> <!-- 根據需要選擇版本 -->
 63     </dependency>
 64 
 65   </dependencies>
 66 
 67   <repositories>
 68     <repository>
 69       <id>osgeo</id>
 70       <name>OSGeo Release Repository</name>
 71       <url>https://repo.osgeo.org/repository/release/</url>
 72       <snapshots><enabled>false</enabled></snapshots>
 73       <releases><enabled>true</enabled></releases>
 74     </repository>
 75     <repository>
 76       <id>osgeo-snapshot</id>
 77       <name>OSGeo Snapshot Repository</name>
 78       <url>https://repo.osgeo.org/repository/snapshot/</url>
 79       <snapshots><enabled>true</enabled></snapshots>
 80       <releases><enabled>false</enabled></releases>
 81     </repository>
 82   </repositories>
 83 
 84   <build>
 85     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 86       <plugins>
 87         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
 88         <plugin>
 89           <artifactId>maven-clean-plugin</artifactId>
 90           <version>3.1.0</version>
 91         </plugin>
 92         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
 93         <plugin>
 94           <artifactId>maven-resources-plugin</artifactId>
 95           <version>3.0.2</version>
 96         </plugin>
 97         <plugin>
 98           <artifactId>maven-compiler-plugin</artifactId>
 99           <version>3.8.0</version>
100         </plugin>
101         <plugin>
102           <artifactId>maven-surefire-plugin</artifactId>
103           <version>2.22.1</version>
104         </plugin>
105         <plugin>
106           <artifactId>maven-jar-plugin</artifactId>
107           <version>3.0.2</version>
108         </plugin>
109         <plugin>
110           <artifactId>maven-install-plugin</artifactId>
111           <version>2.5.2</version>
112         </plugin>
113         <plugin>
114           <artifactId>maven-deploy-plugin</artifactId>
115           <version>2.8.2</version>
116         </plugin>
117         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
118         <plugin>
119           <artifactId>maven-site-plugin</artifactId>
120           <version>3.7.1</version>
121         </plugin>
122         <plugin>
123           <artifactId>maven-project-info-reports-plugin</artifactId>
124           <version>3.0.0</version>
125         </plugin>
126 
127       </plugins>
128     </pluginManagement>
129     <plugins>
130       <plugin>
131         <groupId>org.apache.maven.plugins</groupId>
132         <artifactId>maven-compiler-plugin</artifactId>
133         <configuration>
134           <source>8</source>
135           <target>8</target>
136         </configuration>
137       </plugin>
138     </plugins>
139   </build>
140 </project>
pom.xml

添加完成後,右鍵pom.xml,Maven→生成資源

 

 

五.示例代碼

  獲取geojson文件中的feature

 1     public  List<Feature> getGdbInf() {
 2 
 3 //        註冊所有驅動
 4         ogr.RegisterAll();
 5 //        配置GDAL_DATA路徑
 6 //        gdal.SetConfigOption("GDAL_DATA", "D:\\GDAL\\GDAL\\release-1911-x64-gdal-2-4-0-mapserver-7-2-1\\bin\\gdal-data");
 7 //        支持中文路徑
 8         gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
 9 //        屬性表字段支持中文
10         gdal.SetConfigOption("SHAPE_ENCODING", "");
11 
12 
13         String strDriverName ="GeoJSON" ;
14 
15         Driver oDriver = ogr.GetDriverByName(strDriverName);
16         if (oDriver == null) {
17             System.out.println(strDriverName + "驅動不可用!\n");
18             return null;
19         }
20 
21         DataSource dataSource = oDriver.Open(path,0);
22 //        獲取圖層
23         Layer layer = dataSource.GetLayer(0);
24 
25         String strlayerName = layer.GetName();
26         System.out.println("圖層名稱:" + strlayerName);
27 
28         SpatialReference spatialReference = layer.GetSpatialRef();
29         System.out.println("空間參考座標系:" + spatialReference.GetAttrValue("AUTHORITY", 0) + spatialReference.GetAttrValue("AUTHORITY", 1));
30 
31         double[] layerExtent = layer.GetExtent();
32         System.out.println("圖層範圍:minx:" + layerExtent[0] + ", maxx:" + layerExtent[1] + ", miny:" + layerExtent[2] + ", maxy:" + layerExtent[3]);
33         List<Feature> featureList = new ArrayList<>();
34 
35         // 遍歷所有要素
36        Feature feature;
37         while ((feature = layer.GetNextFeature()) != null) {
38             featureList.add(feature); // 將Feature對象添加到集合中
39 //            feature.delete(); // 釋放要素資源,加這句最後入庫會報錯
40         }
41 //
42         // 關閉GDB數據源
43         dataSource.delete();
44 
45 
46         return featureList;
47 
48     }
示例代碼

 

總結

  其實並不難,網上的資料有點亂。

  要注意的兩個地方在文中標紅了,一個是設置完環境變量要重啓機器,第二個是proj文件夾的設置,proj7和proj9都試一下,這個大概是版本問題,能解決問題就行,不想深入研究了。

 

後續我會把矢量文件入arcsde連接的postgresql數據庫整理出來,放到小專欄。

 

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