POM文件引入
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>3.5.0</version>
</dependency>
示例代码
ogr.RegisterAll();
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
gdal.SetConfigOption("SHAPE_ENCODING", "");
gdal.SetConfigOption("DXF_FEATURE_LIMIT_PER_BLOCK", "-1");
gdal.SetConfigOption("DXF_ENCODING", "ASCII"); //设置DXF缺省编码
gdal.SetConfigOption("OGR_XLS_FIELD_TYPES ", "STRING");
String strVectorFile = "文件路径";
DataSource ds = ogr.Open(strVectorFile, 0);
if (ds == null) {
return AjaxJson.error("打开文件失败!");
}
System.out.println("打开文件成功!");
Driver dv = ogr.GetDriverByName("XLSX");
if (dv == null) {
return AjaxJson.error("打开驱动失败!");
}
System.out.println("打开驱动成功!");
String uuid = IdUtil.simpleUUID();
String geojsonPath = absolutePath.replace(".dxf", "");
geojsonPath = geojsonPath + "" + uuid + ".xlsx";
DataSource ods = dv.CreateDataSource(geojsonPath);
Layer oLayer = ds.GetLayerByIndex(0);
System.out.println(oLayer.GetFeatureCount());
Feature feature;
Map<String, String> points = new HashMap<>();
while ((feature = oLayer.GetNextFeature()) != null) {
//获取空间属性
Geometry geometry = feature.GetGeometryRef();
if (StringUtils.isNotBlank(layer) && !layerNames.contains(feature.GetFieldAsString("Layer"))) {
continue;
}
if (geometry.GetPoints() != null) {
if ("POINT".equals(geometry.GetGeometryName())) {
feature.SetField(3, JSON.toJSON(geometry.GetPoints()).toString());
points.put((feature.GetFID() + 1) + "", JSON.toJSON(geometry.GetPoints()).toString());
}
}
}
Layer excelLayer = ods.CopyLayer(ds.GetLayerByIndex(0), "LAYER");
while ((feature = excelLayer.GetNextFeature()) != null) {
if (StringUtils.isNotBlank(layer) && !layerNames.contains(feature.GetFieldAsString("Layer"))) {
excelLayer.DeleteFeature(feature.GetFID());
continue;
}
String point = points.get(feature.GetFID() + "");
if (StringUtils.isNotBlank(point)) {
System.out.println(feature.GetFID());
feature.SetField(3, point);
}
excelLayer.SetFeature(feature);
}
System.out.println("转换成功!");
ds.FlushCache();
ds.delete();
ods.FlushCache();
ods.delete();
dv.delete();