windows环境下安装gdal
- 从官网下载gdal,并将相关dll文件拷贝至C:\Windows\System32目录下
- 在springboot项目的resources目录下新增lib目录,将gdal.jar拷贝lib目录
- 在pom.xml文件中引入外部依赖
<dependency>
<groupId>gdal</groupId>
<artifactId>gdal</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/gdal.jar</systemPath>
</dependency>
代码示例
package com.example.demo.gis;
import org.gdal.gdal.gdal;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Driver;
import org.gdal.ogr.Layer;
import org.gdal.ogr.ogr;
/**
* @author yangkun
* @createTime 2021年03月27日 20:34:00
*/
public class GdalTest {
public static void cadToShp(String cadPath,String shpPath){
// 注册所有的驱动
ogr.RegisterAll();
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
gdal.SetConfigOption("SHAPE_ENCODING","");
String strVectorFile = cadPath;
DataSource ds = ogr.Open(strVectorFile,0);
if (ds == null)
{
System.out.println("打开文件失败!" );
return;
}
System.out.println("打开文件成功!" );
ds.GetLayerCount();
Layer oLayer = ds.GetLayerByIndex(0);
oLayer.GetName();
if(oLayer == null){
System.out.println("获取失败");
return ;
}
oLayer.ResetReading();
oLayer.GetFeatureCount();
Driver dv = ogr.GetDriverByName("ESRI Shapefile");
String extfile = shpPath;
dv.CreateDataSource(extfile);
dv.CopyDataSource(ds, extfile);
}
public static void main(String[] args){
cadToShp("C:\\Users\\Administrator\\Desktop\\cad-test.dxf","C:\\Users\\Administrator\\Desktop\\2\\cad-test.shp");
}
}