JDK environment variable configuration
Right-click Computer -> Properties -> Advanced System Settings -> Environment Variables -
> New System Variables:
Create two new system variables
JAVA_HOME,
CLASSPATH
Variable name: JAVA_HOME
Variable value:
C:\Program Files (x86)\Java\jdk1.5.0_22
Variable name:
CLASSPATH
Variable value (note that the preceding dot and semicolon cannot be less):
.;%JAVA_HOME%/lib
Variable name: JAVA_HOME
Add in front of Path (if there is no Path system variable, create a new one) "
%JAVA_HOME%/bin;
"
%JAVA_HOME%/bin;%SystemRoot%\system32;。。。。。。
Get GDAL Java Edition compiled files
There are two main ways to obtain GDAL Java compiled files
The first one: Download the GDAL source code and compile it with Visual Studio.
The second: Download the GDAL java library that has been compiled by others.
The second method is adopted here, to download 32-bit GDAl to get release-1600-dev.zip
,
unzip it, and extract the
5 files in the directory release-1600\bin\gdal\java, namely:
gdal.jar
gdalconstjni.dll
gdaljni.dll
ogrjni.dll
osrjni.dll
1 jar package, 4 dlls.
Create a new java project, put the jar package under the lib library of the project, and put the 4 dlls under the project root directory.
Run the demo, when
ogr.RegisterAll
() is executed to
register
all the drivers, the following two possible errors will be reported
Nativelibraryloadfailed.
java.lang.UnsatisfiedLinkError:noogrjniinjava.library.path
Exceptioninthread"main"java.lang.UnsatisfiedLinkError:RegisterAll
atorg.gdal.ogr.ogrJNI.RegisterAll(NativeMethod)
atorg.gdal.ogr.ogr.RegisterAll ( ogr.java:115 )
atcom.gdal.vector.ShapeFileWrite.main(ShapeFileWrite.java:20)
Reason: The
4 dlls are not placed under the project root directory.
or:
Nativelibraryloadfailed.
java.lang.UnsatisfiedLinkError:D:\InspurWorkspace\GDALTest01\ogrjni.dll:Can'tfinddependentlibraries
Exceptioninthread"main"java.lang.UnsatisfiedLinkError:RegisterAll
atorg.gdal.ogr.ogrJNI.RegisterAll(NativeMethod)
atorg.gdal.ogr.ogr.RegisterAll ( ogr.java:115 )
atcom.gdal.vector.ShapeFileWrite.main(ShapeFileWrite.java:20)
The reason is that
ogrjni.dll
lacks other dlls that depend on it. Using a dll dependency tool to check,
ogrjni.dll also depends on a called
gdal111.dll.
问题到这一步,如果你想直接把这个gdal111.dll拷贝到工程跟目录下面去的话,会发现这个gdal111.dll又依赖于其它dll,所以自然而然,你想到了把这个目录下面的所有dll全部拷贝到工程根目录下面去,这样程序能够执行了,但是你会发现,你每建一个工程都需要这样做。
配置GDAL Dll调用路径
上述问题转换为java如何调用c++编译后的dll库。目前自己找到3种方法
方法1、直接将dll文件放在java工程根目录下面。
GDAL Java demo编译成功。优点:思路简单、32为、64位dll可以很方便的切换。缺点:每次新建工程都需要拷贝这些dll,增加工程文件大小,操作繁琐。
方法2、在Eclipse里面编译java的时候设定其Java Build Path-》Source 选择工程名下面的 Native library location选择dll所在路径。
GDAL
Java demo编译失败,
方法3、设置系统环境变量指定dll路径,将其加入Path中
前两种方法是从别人那里看到的,第3种方法是自己想出来的,想法也很简单,在操作系统中运行一个程序(命令)时,如果没有指定绝对路径,它会先去程序本身所在的路径下面去寻找可执行文件,当找不到这些文件的时候,会去环境变量Path下面去寻找需要的东西。所以我们只需要将这个GDAL dll的路径加入到Path里面就行了,
右键计算机——》属性——》高级系统设置——》环境变量——》
新建系统变量:
新建两个系统变量GDAL32_DLL
变量名:
GDAL32_DLL
变量值:C:\GDAL\release-1600-win32-dev\release-1600\bin;C:\GDAL\release-1600-win32-dev\release-1600\bin\gdal\java
名字可以自己定义,指定GDAL DLL路径位置,和GDAL java版编译出路径。
在Path前面增加(如果没有Path系统变量,则新建)"
%GDAL32_DLL%;",即path变为
%GDAL32_DLL%;%JAVA_HOME%/。。。。。。
注意:将dll加入Path后,需要重启电脑,或者先注销后在进入windows系统,才会生效。
Java调用GDAL写入矢量文件 Demo
package com.gdal.vector; import org.gdal.gdal.gdal; import org.gdal.ogr.DataSource; import org.gdal.ogr.Feature; import org.gdal.ogr.FeatureDefn; import org.gdal.ogr.FieldDefn; import org.gdal.ogr.Geometry; import org.gdal.ogr.Layer; import org.gdal.ogr.ogr; public class ShapeFileWrite { public static void main(String[] args) { String strVectorFile ="D:\\test\\gdal\\testShap05.shp"; // 注册所有的驱动 ogr.RegisterAll(); // 为了支持中文路径,请添加下面这句代码 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","NO"); // 为了使属性表字段支持中文,请添加下面这句 gdal.SetConfigOption("SHAPE_ENCODING",""); //创建数据,这里以创建ESRI的shp文件为例 String strDriverName = "ESRI Shapefile"; org.gdal.ogr.Driver oDriver =ogr.GetDriverByName(strDriverName); if (oDriver == null) { System.out.println(strVectorFile+ " 驱动不可用!\n"); return; } // 创建数据源 DataSource oDS = oDriver.CreateDataSource(strVectorFile,null); if (oDS == null) { System.out.println("创建矢量文件【"+ strVectorFile +"】失败!\n" ); return; } // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定 Layer oLayer =oDS.CreateLayer("TestPolygon", null, ogr.wkbPolygon, null); if (oLayer == null) { System.out.println("图层创建失败!\n"); return; } // 下面创建属性表 // 先创建一个叫FieldID的整型属性 FieldDefn oFieldID = new FieldDefn("FieldID", ogr.OFTInteger); oLayer.CreateField(oFieldID, 1); // 再创建一个叫FeatureName的字符型属性,字符长度为50 FieldDefn oFieldName = new FieldDefn("FieldName", ogr.OFTString); oFieldName.SetWidth(100); oLayer.CreateField(oFieldName, 1); FeatureDefn oDefn =oLayer.GetLayerDefn(); // 创建三角形要素 Feature oFeatureTriangle = new Feature(oDefn); oFeatureTriangle.SetField(0, 0); oFeatureTriangle.SetField(1, "三角形"); Geometry geomTriangle =Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))"); oFeatureTriangle.SetGeometry(geomTriangle); oLayer.CreateFeature(oFeatureTriangle); // 创建矩形要素 Feature oFeatureRectangle = new Feature(oDefn); oFeatureRectangle.SetField(0, 1); oFeatureRectangle.SetField(1, "矩形"); Geometry geomRectangle =Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))"); oFeatureRectangle.SetGeometry(geomRectangle); oLayer.CreateFeature(oFeatureRectangle); // 创建五角形要素 Feature oFeaturePentagon = new Feature(oDefn); oFeaturePentagon.SetField(0, 2); oFeaturePentagon.SetField(1, "五角形"); Geometry geomPentagon =Geometry.CreateFromWkt("POLYGON ((70 0,85 0,90 15,80 30,65 15,70 0))"); oFeaturePentagon.SetGeometry(geomPentagon); oLayer.CreateFeature(oFeaturePentagon); //写入文件 oLayer.SyncToDisk(); oDS.SyncToDisk(); System.out.println("\n数据集创建完成!\n"); } }
执行结果
还有一点小问题,中文乱码,上述代码是按照
这篇博文说的写的,修复了下绘制五边形问题和将数据写入磁盘。
总结:
如果是64位java 调用GDAL环境,需要保证3者一致,即64位JDK+64位Eclipse+64位GDAL。64位GDAL需要
去下载release-1600-x64-dev.zip把64为的GDAL dll文件加入Path中。
写这篇博客是为了方便新人快速搭建java调用GDAL库,自己就在这个过程中遇到了很多问题,花了很多时间,上述仅仅是个人的一些理解,有不对的地方请指正。
附件:
编译后的GDAL下载url:
http://download.gisinternals.com/sdk.php
ShapeExplore(查看生成的shp文件)下载url:
http://www.sz1001.net/soft/41323.htm
DLL依赖查看工具:
http://www.xiazaiba.com/html/5730.html