我是靠谱客的博主 干净小海豚,这篇文章主要介绍geojson文件与shape文件的相互转换,现在分享给大家,希望可以做个参考。

一.geojson转shape

pom文件:

复制代码
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
<dependencies> <dependency> <groupId>nl.cloudfarming.client</groupId> <artifactId>lib-geotools</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies>

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
import com.alibaba.fastjson.JSONObject; import com.vividsolutions.jts.geom.*; import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geojson.geom.GeometryJSON; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import java.io.*; import java.util.HashMap; import java.util.List; import java.util.Map; public class GeojsonToShape { public static void main(String[] args) { geojson2Shape( "E:\points.geojson", "E:\college\college.shp"); } public static void geojson2Shape(String jsonPath, String shpPath){ GeometryJSON geojson = new GeometryJSON(); try{ String geojsonStr = readJson(jsonPath); Map<String, Object> geojsonMap = JSONObject.parseObject(geojsonStr, Map.class); List<Map> features = (List<Map>) geojsonMap.get("features"); Map geojsonExemple = features.get(0); String geojsonType = ((Map) geojsonExemple.get("geometry")).get("type").toString(); Map<String, Class> mapFields = new HashMap(); for (int i = 0; i < features.size(); i++) { Map<String, Object> attributes = (Map<String, Object>) features.get(i).get("properties"); for (String key : attributes.keySet()) { Class type = attributes.get(key).getClass(); mapFields.put(key, type); } } Class<?> geoType = null; switch(geojsonType){ 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<String, Serializable>(); params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() ); ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params); //定义图形信息和属性信息 SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setCRS(DefaultGeographicCRS.WGS84); tb.setName("shapefile"); tb.add("the_geom", geoType); for (String key : mapFields.keySet()) { tb.add(key, mapFields.get(key)); } ds.createSchema(tb.buildFeatureType()); //设置Writer FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds. getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT); for(int i=0,len=features.size();i<len;i++){ Map oneGeojson = features.get(i); Map<String,Object> attributes = (Map<String, Object>) oneGeojson.get("properties"); String strFeature = JSONObject.toJSONString(oneGeojson); Reader reader = new StringReader(strFeature); SimpleFeature feature = writer.next(); switch(geojsonType){ case "Point": feature.setAttribute("the_geom",geojson.readPoint(reader)); break; case "MultiPoint": feature.setAttribute("the_geom",geojson.readMultiPoint(reader)); break; case "LineString": feature.setAttribute("the_geom",geojson.readLine(reader)); break; case "MultiLineString": feature.setAttribute("the_geom",geojson.readMultiLine(reader)); break; case "Polygon": feature.setAttribute("the_geom",geojson.readPolygon(reader)); break; case "MultiPolygon": feature.setAttribute("the_geom",geojson.readMultiPolygon(reader)); break; } for (String key : attributes.keySet()) { feature.setAttribute(key,attributes.get(key)); } writer.write(); } writer.close(); ds.dispose(); } catch(Exception e){ System.out.println("转换失败"); e.printStackTrace(); } } /** * 读取文件 * @param filepath 文件路径 * @throws IOException */ public static String readJson(String filepath) { FileReader re = null; BufferedReader buff = null; String line = ""; try { File file = new File(filepath); re = new FileReader(file); buff = new BufferedReader(re); String tempString = null; while ((tempString = buff.readLine()) != null) { line += tempString; } return line; } catch (Exception e) { System.out.println("失败了"); } finally { try { re.close(); buff.close(); } catch (IOException e) { e.printStackTrace(); } } return line; } }

二.shape转geojson

pom:

复制代码
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
<dependencies> <dependency> <groupId>nl.cloudfarming.client</groupId> <artifactId>lib-geotools</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies>

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
import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geojson.feature.FeatureJSON; import org.opengis.feature.simple.SimpleFeature; import java.io.*; import java.nio.charset.Charset; public class ShapeToGeojson { public static void main(String[] args) { shape2Geojson("E:\jingwei\china_2000.shp","E:\points.geojson"); } /** * shp转换为Geojson * @param shpPath * @return */ public static void shape2Geojson(String shpPath, String jsonPath){ FeatureJSON featureJson = new FeatureJSON(); try{ BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsonPath),"UTF-8")); out.write("{"type": "FeatureCollection","features": ["); File file = new File(shpPath); ShapefileDataStore shpDataStore = null; shpDataStore = new ShapefileDataStore(file.toURL()); // 设置编码,防止中文乱码 shpDataStore.setStringCharset(Charset.forName("gbk")); String typeName = shpDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = null; featureSource = shpDataStore.getFeatureSource (typeName); SimpleFeatureCollection result = featureSource.getFeatures(); System.out.println(result.size()); SimpleFeatureIterator itertor = result.features(); int count = 1; while (itertor.hasNext()) { SimpleFeature feature = itertor.next(); StringWriter writer = new StringWriter(); featureJson.writeFeature(feature, writer); out.write(writer.toString()); System.out.println(count); if (count != result.size() ){ out.write( ","); } count ++; } out.write( "]}"); itertor.close(); out.flush(); out.close(); } catch(Exception e){ System.out.println("转换失败"); e.printStackTrace(); } } }

最后

以上就是干净小海豚最近收集整理的关于geojson文件与shape文件的相互转换的全部内容,更多相关geojson文件与shape文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部