java 调用gdal读取gdb数据
package com.epf.zjgg.utils;
import com.epf.zjgg.dto.LayerDto;
import org.apache.commons.lang.StringUtils;
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import java.io.IOException;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
/**
* gdal读取gdb示例
*/
public class GdbUtils {
static {
gdal.AllRegister();
// 配置GDAL_DATA路径
gdal.SetConfigOption("GDAL_DATA", "F:\\BaiduNetdiskDownload\\release-1900-x64-gdal-2-4-4-mapserver-7-4-3\\bin\\gdal-data");
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
// 属性表字段支持中文
gdal.SetConfigOption("SHAPE_ENCODING", "");
}
public static LayerDto getLayerDto(String FirePath){
Driver driver = ogr.GetDriverByName("OpenFileGDB");
if (driver == null) {
return null;
}
List<Map> list = new ArrayList<>();
List<Map<String,String>> list1 =new ArrayList<>();
LayerDto layerDto = new LayerDto();
DataSource dataSource = null;
try{
dataSource = driver.Open(FirePath, 0);
int num = dataSource.GetLayerCount();
for (int i = 0; i < num; i++) {
// 获取图层
Layer layer = dataSource.GetLayer(i);
String strlayerName = layer.GetName(); //表名称
// 获取图层要数个数
long count = layer.GetFeatureCount();
if (0!=count){
do {//获取图层下的要素
Feature feature = layer.GetNextFeature();
if (null == feature) {
break;
}
//获取边界坐标
Geometry geometry = feature.GetGeometryRef();
if (geometry!=null){
String geometryJson = geometry.ExportToJson();
String str = geometryJson.substring(geometryJson.lastIndexOf(":") + 1, geometryJson.length());
if (!" [ [ ] ] }".equals(str)){
String s1 = str.substring(7, str.length() - 8);
String[] split = s1.replaceAll(" ", "").replaceAll("\\[","").replaceAll("]","").split(",");
List<String> xList = new ArrayList();
List<String> yList = new ArrayList();
Map<String,String> map1 = new HashMap();
for (int j = 0; j < split.length; j++) {
if (j!=(split.length-1)&&j%2==0) {
map1.put(split[j], split[j + 1]);
}
}
list1.add(map1);
}
}
Map map = new HashMap();
//获取属性
for (int p = 0; p < feature.GetFieldCount(); p++) {
map.put(feature.GetFieldDefnRef(p).GetName(),getProperty(feature, p));
}
list.add(map);
feature.delete();
} while (true);
}
layerDto.setStrlayerName(strlayerName);
layerDto.setList(list);
layerDto.setCount(count);
layerDto.setList1(list1);
}
}catch (Exception e){
e.printStackTrace();
}finally{
if(dataSource != null){
dataSource.delete();
}
}
return layerDto;
}
private static Object getProperty(Feature feature, int index) {
int type = feature.GetFieldType(index);
PropertyGetter propertyGetter;
if (type < 0 || type >= propertyGetters.length) {
propertyGetter = stringPropertyGetter;
} else {
propertyGetter = propertyGetters[type];
}
try {
return propertyGetter.get(feature, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 属性获取器
*/
@FunctionalInterface
private interface PropertyGetter {
Object get(Feature feature, int index);
}
private static final PropertyGetter stringPropertyGetter = (feature, index) -> feature.GetFieldAsString(index);
/**
* (index)得到一个属性类型的int值,该值对应具体类型
*/
private static final PropertyGetter[] propertyGetters = new PropertyGetter[]{
(feature, index) -> feature.GetFieldAsInteger(index),//0 Integer
(feature, index) -> feature.GetFieldAsIntegerList(index),//1 IntegerList
(feature, index) -> feature.GetFieldAsDouble(index),//2 Real
(feature, index) -> feature.GetFieldAsDoubleList(index),//3 RealList
stringPropertyGetter,//4 String
(feature, index) -> feature.GetFieldAsStringList(index),//5 StringList
stringPropertyGetter,//6 (unknown)
stringPropertyGetter,//7 (unknown)
(feature, index) -> feature.GetFieldAsBinary(index),//8 Binary
(feature, index) -> {
int[] pnYear = new int[1];
int[] pnMonth = new int[1];
int[] pnDay = new int[1];
int[] pnHour = new int[1];
int[] pnMinute = new int[1];
float[] pfSecond = new float[1];
int[] pnTZFlag = new int[1];
feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
java.sql.Date date = java.sql.Date.valueOf(LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]));
return date;
},//9 Date
(feature, index) -> {
int[] pnYear = new int[1];
int[] pnMonth = new int[1];
int[] pnDay = new int[1];
int[] pnHour = new int[1];
int[] pnMinute = new int[1];
float[] pfSecond = new float[1];
int[] pnTZFlag = new int[1];
feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
float fSecond = pfSecond[0];
int s = (int) fSecond;
int ns = (int) (1000000000 * fSecond - s);
Time time = Time.valueOf(LocalTime.of(pnHour[0], pnMinute[0], s, ns));
return time;
},// 10 Time
(feature, index) -> {
int[] pnYear = new int[1];
int[] pnMonth = new int[1];
int[] pnDay = new int[1];
int[] pnHour = new int[1];
int[] pnMinute = new int[1];
float[] pfSecond = new float[1];
int[] pnTZFlag = new int[1];
feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
float fSecond = pfSecond[0];
int s = (int) fSecond;
int ns = (int) (1000000000 * fSecond - s);
LocalDateTime localDateTime = LocalDateTime.of(
LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]),
LocalTime.of(pnHour[0], pnMinute[0], s, ns)
);
Timestamp timestamp = Timestamp.valueOf(localDateTime);
return timestamp;
},//11 DateTime
(feature, index) -> feature.GetFieldAsInteger64(index),//12 Integer64
(feature, index) -> feature.GetFieldAsIntegerList(index),//13 Integer64List
//>=14 (unknown)
};
}