java读取arcgis的gdb文件

gdb,是arcgis重要的数据格式之一,它把矢量数据按以下层级结构存为本地文件

1
2
3
4
5
图层
-- 要素
---- 属性
---- 形状
---- 样式

gdal提供了函数允许我们读取gdb,因此,我们可以通过jni调用gdal来再java中读取gdal

1、配置gdal环境

liunx下按照gdal需要编译,而windows下就比较简单了,下载编译好的dll文件,然后环境变量path里加入dll目录即可

1
2
链接:https://pan.baidu.com/s/1kH3djGQsow-llQiy82068g 
提取码:uuwn

2、建一个maven工程

前面下载的那个包里的lib文件夹拷到工程中,然后maven里添加gdal的依赖:

1
2
3
4
5
6
7
8
9
10
<dependencies>
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>2.2.3</version>
<scope>system</scope>
<!--<systemPath>$
{project.basedir}/lib/gdal-centos7x64.jar</systemPath>-->
<systemPath>$
{project.basedir}/lib/gdal-win64.jar</systemPath>
</dependency>
</dependencies>

3、写代码

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
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;

/**
* gdal读取gdb示例
* @author liuyu
* @date 2020/11/5
*/

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());
//获取geometry
System.out.println(feature.GetGeometryRef().ExportToWkt());//也可以ExportToWkb() gml等等
//获取属性
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);

/**
* feature.GetFieldType(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);
Date date = 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)
};

}

本文采用 CC BY-SA 4.0 协议 ,转载请注明原始链接: https://blog.wowtools.org/2020/11/05/2020-11-5-java-read-gdb/

×

请作者喝杯咖啡