代码
package com.epf.gdbh.exam.testGeo;
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
public class GdbUtils {
static {
gdal.AllRegister();
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
gdal.SetConfigOption("SHAPE_ENCODING", "");
}
public static LayerDto getLayerDto(String FirePath){
Driver driver = ogr.GetDriverByName("ESRI Shapefile");
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();
System.out.println(feature);
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);
private static final PropertyGetter[] propertyGetters = new PropertyGetter[]{
(feature, index) -> feature.GetFieldAsInteger(index),
(feature, index) -> feature.GetFieldAsIntegerList(index),
(feature, index) -> feature.GetFieldAsDouble(index),
(feature, index) -> feature.GetFieldAsDoubleList(index),
stringPropertyGetter,
(feature, index) -> feature.GetFieldAsStringList(index),
stringPropertyGetter,
stringPropertyGetter,
(feature, index) -> feature.GetFieldAsBinary(index),
(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;
},
(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;
},
(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;
},
(feature, index) -> feature.GetFieldAsInteger64(index),
(feature, index) -> feature.GetFieldAsIntegerList(index),
};
}
通过Feature 获取属性和值
package com.epf.gdbh.tool.geo;
import org.gdal.ogr.*;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
public class FeatureUtils {
public static List<Map> getLayerFeature(Feature feature){
List<Map> list = new ArrayList<>();
Map map = new HashMap();
for (int p = 0; p < feature.GetFieldCount(); p++) {
map.put(feature.GetFieldDefnRef(p).GetName(),getProperty(feature, p));
}
list.add(map);
return list;
}
public static List<Map> getLayerFeatures(List<Feature> features) {
List<Map> list = new ArrayList<>();
Map map = new HashMap();
for (Feature feature :features) {
for (int p = 0; p < feature.GetFieldCount(); p++) {
map.put(feature.GetFieldDefnRef(p).GetName(),getProperty(feature, p));
}
list.add(map);
}
return list;
}
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);
private static final PropertyGetter[] propertyGetters = new PropertyGetter[]{
(feature, index) -> feature.GetFieldAsInteger(index),
(feature, index) -> feature.GetFieldAsIntegerList(index),
(feature, index) -> feature.GetFieldAsDouble(index),
(feature, index) -> feature.GetFieldAsDoubleList(index),
stringPropertyGetter,
(feature, index) -> feature.GetFieldAsStringList(index),
stringPropertyGetter,
stringPropertyGetter,
(feature, index) -> feature.GetFieldAsBinary(index),
(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;
},
(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;
},
(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;
},
(feature, index) -> feature.GetFieldAsInteger64(index),
(feature, index) -> feature.GetFieldAsIntegerList(index),
};
}