- 简述
这儿基于GeoTools实现GeoJSON和ShapeFile数据格式的互转帮助类的实现。这儿的GeoTools版本为24.2,IDE 用的NeatBeans 12.2。
- Maven核心配置
复制代码
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<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <geotools.version>24.2</geotools.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>${geotools.version}</version> <exclusions> <exclusion> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-swing</artifactId> <version>${geotools.version}</version> <exclusions> <exclusion> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>${geotools.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/aws-json-protocol --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-opengis</artifactId> <version>${geotools.version}</version> <type>jar</type> </dependency> <dependency> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> <version>1.17.1</version> <type>jar</type> </dependency> </dependencies> <repositories> <repository> <id>osgeo</id> <name>OSGeo Release Repository</name> <url>https://repo.osgeo.org/repository/release/</url> <snapshots><enabled>false</enabled></snapshots> <releases><enabled>true</enabled></releases> </repository> <repository> <id>osgeo-snapshot</id> <name>OSGeo Snapshot Repository</name> <url>https://repo.osgeo.org/repository/snapshot/</url> <snapshots><enabled>true</enabled></snapshots> <releases><enabled>false</enabled></releases> </repository> </repositories> <build> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
- 代码实现 (JAVA)
复制代码
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import java.util.HashMap; import java.util.Map; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.geojson.geom.GeometryJSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.Feature; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Serializable; import java.io.StringWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.feature.AttributeTypeBuilder; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureIterator; import org.geotools.feature.NameImpl; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.feature.type.AttributeTypeImpl; import org.geotools.geojson.feature.FeatureJSON; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.geotools.util.SimpleInternationalString; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.LineString; import org.locationtech.jts.geom.MultiLineString; import org.locationtech.jts.geom.MultiPoint; import org.locationtech.jts.geom.MultiPolygon; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.feature.type.AttributeDescriptor; import org.opengis.referencing.crs.CoordinateReferenceSystem; /** * * @author Administrator */ public class FileFormat { public static Map geojson2Shape(String jsonPath, String shpPath) { Map map = new HashMap(); GeometryJSON gjson = new GeometryJSON(); try { String strJson = readFileContent(jsonPath); JSONObject json = JSON.parseObject(strJson,Feature.IgnoreNotMatch); JSONArray features = json.getJSONArray("features"); JSONObject feature0 = JSON.parseObject(features.getString(0),Feature.InitStringFieldAsEmpty); String strType = (feature0.getJSONObject("geometry")).getString("type"); Map<String, Object> properties0 = (Map<String, Object>) feature0.getJSONObject("properties"); Class<?> geoType = null; switch (strType) { case "Point": geoType = Point.class; break; case "MultiPoint": geoType = MultiPoint.class; break; case "LineString": geoType = LineString.class; break; case "MultiLineString": geoType = MultiLineString.class; break; case "Polygon": geoType = Polygon.class; break; case "MultiPolygon": geoType = MultiPolygon.class; break; } //创建shape文件对象 File file = new File(shpPath); Map<String, Serializable> params = new HashMap<>(); params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL()); ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params); //设置编码 Charset charset = Charset.forName("GBK"); ds.setCharset(charset); //定义图形信息和属性信息 SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setCRS(DefaultGeographicCRS.WGS84); tb.setName("shapefile"); tb.add("the_geom", geoType); tb.add("gid", Long.class); List<AttributeDescriptor> attributeDesList=new ArrayList<>(); for (Entry<String, Object> entry : properties0.entrySet()) { //属性构造器 AttributeTypeBuilder build = new AttributeTypeBuilder(); String name=entry.getKey(); build.setName(name); build.setBinding(entry.getValue().getClass()); build.setDescription(name); AttributeTypeImpl type = new AttributeTypeImpl(new NameImpl(name), entry.getValue().getClass(), true, true, null, build.buildType(),new SimpleInternationalString(name)); String desString=type.getDescription().toString(); AttributeDescriptor descriptor= build.buildDescriptor(name,type); attributeDesList.add(descriptor); } tb.addAll(attributeDesList); ds.createSchema(tb.buildFeatureType()); try ( //设置Writer FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT)) { int index = 0; System.out.println(index); for (Iterator iterator = features.iterator(); iterator.hasNext();) { JSONObject feature = (JSONObject) iterator.next(); Map<String, Object> properties = (Map<String, Object>) feature.getJSONObject("properties"); String strFeature = feature.toString(); System.out.println(strFeature); Geometry geometry = gjson.read(strFeature); System.out.println(geometry); SimpleFeature simpleFeature = writer.next(); //System.out.println(feature); simpleFeature.setAttribute("the_geom", geometry); simpleFeature.setAttribute("gid", index); for (Entry<String, Object> entry : properties.entrySet()) { //simpleFeature.setAttribute(entry.getKey(), entry.getValue()); simpleFeature.setAttribute(entry.getKey(), entry.getValue()); } writer.write(); index++; } } ds.dispose(); map.put("status", "success"); map.put("message", shpPath); } catch (Exception e) { map.put("status", "failure"); map.put("message", e.getMessage()); e.printStackTrace(); } return map; } /** * shp转换为Geojson * * @param shpPath * @param jsonPath * @return */ public static Map shape2Geojson(String shpPath, String jsonPath) { Map map = new HashMap(); FeatureJSON fjson = new FeatureJSON(); try { StringBuilder sb = new StringBuilder(); sb.append("{"type": "FeatureCollection","features": "); File file = new File(shpPath); ShapefileDataStore shpDataStore = new ShapefileDataStore(file.toURI().toURL()); SimpleFeatureType featureType=shpDataStore.getSchema(); CoordinateReferenceSystem crs=featureType.getCoordinateReferenceSystem(); //CoordinateReferenceSystem crs_4490 = CRS.decode("EPSG:4490"); if(crs.toWKT().startsWith(("PROJCS"))){ System.out.println("该数据坐标系为投影坐标系,不符合入库条件"); }else if(crs.toWKT().startsWith(("GEOGCS"))){ if(crs.toWKT().contains("CGCS2000") || crs.toWKT().contains("2000")){ System.out.println("该数据坐标系是EPSG:4490,符合入库条件"); }else{ System.out.println("该数据坐标系为投影坐标系,不符合入库条件"); } } //设置编码 Charset charset = Charset.forName("GBK"); //shpDataStore.setCharset(charset); String typeName = shpDataStore.getTypeNames()[0]; JSONArray array; FeatureSource<SimpleFeatureType, SimpleFeature> source = shpDataStore .getFeatureSource(typeName); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(); FeatureIterator<SimpleFeature> features = collection.features(); array = new JSONArray(); while (features.hasNext()) { SimpleFeature feature = features.next(); Geometry geometry= (Geometry)feature.getAttribute("the_geom"); //(Geometry)feature.getDefaultGeometry(); StringWriter writer = new StringWriter(); fjson.writeFeature(feature, writer); JSONObject json = JSON.parseObject(writer.toString()); array.add(json); System.out.print(feature.getID()); System.out.print(": "); System.out.println(feature.getDefaultGeometryProperty().getValue());//此行输出的空间信息的wkt表达形式 } sb.append(new String(array.toString().getBytes("ISO-8859-1"), "GBK")); sb.append("}"); //写入文件 append2File(jsonPath, sb.toString()); map.put("status", "success"); map.put("message", sb.toString()); }catch(Exception e){ map.put("status", "failure"); map.put("message", e.getMessage()); e.printStackTrace(); } return map; } public static void append2File(String JsonPath, String messsage) { try { File f = new File(JsonPath);//向指定文本框内写入 FileWriter fw = new FileWriter(f); fw.write(new String(messsage.getBytes(),"GBK")); fw.close(); } catch (Exception e) {} } public static String readFileContent(String fileName) { File file = new File(fileName); BufferedReader reader = null; StringBuffer sbf = new StringBuffer(); try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); String tempStr; while ((tempStr = reader.readLine()) != null) { sbf.append(tempStr); } reader.close(); return sbf.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return sbf.toString(); } }
最后
以上就是文静蜜粉最近收集整理的关于GeoTools之GeoJSON和ShapeFile格式互转的全部内容,更多相关GeoTools之GeoJSON和ShapeFile格式互转内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复