1.下载gdal压缩包并解压
2.在项目中引用gdal.jar
3.引用dll文件
将dll文件拷贝到项目的根目录下
4.将shpfile文件转出geojson文件
public static void main(String
[] args
) {
ogr
.RegisterAll();
gdal
.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
gdal
.SetConfigOption("SHAPE_ENCODING","");
String strVectorFile
= "C:\\Users\\10469\\Desktop\\乡镇\\乡镇.shp";
DataSource ds
= ogr
.Open(strVectorFile
,0);
if (ds
== null
)
{
System
.out
.println("打开文件失败!" );
return;
}
System
.out
.println("打开文件成功!" );
Driver dv
= ogr
.GetDriverByName("GeoJSON");
if (dv
== null
)
{
System
.out
.println("打开驱动失败!" );
return;
}
System
.out
.println("打开驱动成功!" );
dv
.CopyDataSource(ds
, "C:\\Users\\10469\\Desktop\\乡镇\\乡镇.json");
System
.out
.println("转换成功!" );
System
.out
.println(System
.getProperty("java.library.path"));
}
5.将geojson文件转出shpfile文件
public static void main(String
[] args
) {
ogr
.RegisterAll();
gdal
.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
gdal
.SetConfigOption("SHAPE_ENCODING","");
String strVectorFile
= "E:\\opt\\trans.json";
DataSource ds
= ogr
.Open(strVectorFile
,0);
if (ds
== null
)
{
System
.out
.println("打开文件失败!" );
return;
}
System
.out
.println("打开文件成功" );
String strDriverName
= "ESRI Shapefile";
org
.gdal
.ogr
.Driver dv
= ogr
.GetDriverByName(strDriverName
);
if (dv
== null
)
{
System
.out
.println("打开驱动失败!" );
return;
}
System
.out
.println("打开驱动成功!" );
dv
.CopyDataSource(ds
, "E:\\opt\\trans\\test.shp");
System
.out
.println("转换成功!" );
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-31092.html