1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| package com;
import org.gdal.gdal.gdal; import org.gdal.ogr.*;
import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime;
public class Test { static { gdal.AllRegister(); }
public static void main(String[] args) { Driver driver = ogr.GetDriverByName("OpenFileGDB"); DataSource dataSource = driver.Open("D:\\data\\云南省矢量地图gdb\\云南省地图综合成果.gdb", 0); for (int i = 0; i < dataSource.GetLayerCount(); i++) { Layer layer = dataSource.GetLayer(i); System.out.println(i + "\t" + layer.GetName()); do { Feature feature = layer.GetNextFeature(); if (null == feature) { break; } System.out.println(feature.GetStyleString()); System.out.println(feature.GetGeometryRef().ExportToWkt()); for (int p = 0; p < feature.GetFieldCount(); p++) { System.out.println(feature.GetFieldDefnRef(p).GetName() + "\t" + getProperty(feature, p)); } System.out.println("---------"); } while (true); } }
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); Date date = 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), };
}
|