GeoTools是ArcGis地图与java对象的桥梁,恰如jdbc之于oracle与java。
shp文件本身是存有地理对象边界坐标、对象中心城市及城市编号的多多边形字符串。
需要使用的依赖如下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!-- 添加GeoTools依赖 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version> 1.6 . 1 </version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version> 1.2 . 47 </version>
</dependency>
|
对象:
?
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
|
public class CutProperty {
private String province;
private int x;
private int y;
/**
* 图片宽
* */
private int width;
/**
* 图片高
* */
private int height;
/**
* 画图时线条粗细
* */
private float weight;
/**
* 地图坐标右边界
* */
private int rightborder;
/**
* 地图坐标放大倍数
* */
private int bei;
/**
* 文字大小
* */
private int stringsize;
|
来自@author yukun24@126.com的工具类,读取shp文件:
?
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
|
package com.gwhn.geotools;
import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStoreFactorySpi;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.junit.Test;
import org.locationtech.jts.geom.*;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.*;
public class ShapeUtil {
//读shp文件【几何信息+属性信息】
public List<Property> SHPRead(String path) throws Exception {
List<Property> propertyList = new ArrayList<>();
//基于上面新建的shapfile文件,进行读取
//构建shapefile数据存储的实例
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
//基于路径构建文件对象
File file = new File(path);
//构建一个已存在的shapfile数据源
//ShapefileDataStore:数据存储实现,允许从Shapefiles读取和写入
ShapefileDataStore ds = (ShapefileDataStore) dataStoreFactory.createDataStore(file.toURI().toURL());
//设置编码,防止中文读取乱码
ds.setCharset(Charset.forName( "UTF-8" ));
//getFeatureSource():ContentFeatureSource
//这个特性是由 FeatureCollection提供的操作完成的。单独的特征记忆实现由子类提供:
//SimpleFeatureSource特征资源明确地使用FeatureCollection【集合】,可迭代
SimpleFeatureSource featureSource = ds.getFeatureSource();
//getFeatures():以FeatureCollection的形式检索所有特性。
//一个用于处理FeatureCollection的实用工具类。提供一个获取FeatureCollection实例的机制
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
System.out.println( "几何对象总共有:" + result.size());
//features():返回一个FeatureIterator迭代器
SimpleFeatureIterator it = (SimpleFeatureIterator) result.features();
while (it.hasNext()) {
SimpleFeature feature = it.next();
//迭代属性【属性我们可以理解为一个几何对象的属性节点,也就是对一个几何图形的描述字段】
Iterator<Property> ip = feature.getProperties().iterator();
// System.out.println("========================");
//再来个while
while (ip.hasNext()) {
Property pro = ip.next();
// System.out.println(pro.getName()+" = "+pro.getValue());
propertyList.add(pro);
} //end 属性迭代
}
it.close();
return propertyList;
}
}
|
来自@author yukun24@126.com的工具类,此处用来将shp中获取的字符串转化为多多边形对象MultiPolygon
?
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|