GDAL for Java环境搭建

发布时间 2023-10-13 14:28:47作者: Youse的二分口粮地

+

前言

  本文主要参考别人的一篇文章: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数据库整理出来,放到小专栏。