我是靠谱客的博主 勤劳可乐,这篇文章主要介绍dxf转geojson后,使用java代码找出dxf每个图层中的标志性图标(三角形、四边形等等),现在分享给大家,希望可以做个参考。

第一步:先熟悉geojson的数据结构

geojson将所有的地理要素分为Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon、GeometryCollection。

感觉有篇博文讲的挺好,大家可以去看一下geojson数据结构讲解

第二步:试图使用Java代码去获取json里面你想要的图层里面的数据

我之前写过一篇根据Java代码获取json数据的文章,大家可以去看一下java操作json文件

第三步:开始分析你想要找的图标有什么特点

复制代码
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
//这代表一个三角形 { "geometry": { "geometries": [{ "coordinates": [ [19776.669056702118, 32717.534270796306, 9.181425E-9], [20103.606839973643, 32717.534325811146, 9.181425E-9] ], "type": "LineString" }, { "coordinates": [ [19776.669056702118, 32717.534270796306, 9.181425E-9], [19940.13805881275, 32417.534298303737, 9.181425E-9] ], "type": "LineString" }, { "coordinates": [ [19940.13805881275, 32417.534298303737, 9.181425E-9], [20103.606839973643, 32717.534325811146, 9.181425E-9] ], "type": "LineString" }, { "coordinates": [ [19834.25117925165, 32717.534280485826, 9.181425E-9], [19834.251181033436, 32706.94559757729, 9.181425E-9] ], "type": "LineString" }, 。。。。。。。。 , { "coordinates": [ [19940.138027930516, 32601.05878631602, 9.181425E-9], [19940.138045748416, 32495.171957236857, 9.181425E-9] ], "type": "LineString" }, { "coordinates": [ [20046.02483741, 32717.53431612164, 9.181425E-9], [20046.02483919179, 32706.945633213105, 9.181425E-9] ], "type": "LineString" }, { "coordinates": [19940.138045748416, 32495.171957236857, 9.181425E-9], "type": "Point" }, { "coordinates": [19940.138010112612, 32706.945615395198, 9.181425E-9], "type": "Point" }], "type": "GeometryCollection" }, "type": "Feature", "properties": { "ExtendedEntity": null, "SubClasses": "AcDbEntity:AcDbBlockReference", "Text": null, "EntityHandle": "3DA17", "Linetype": null, "Layer": "I-POWER" } },

这是一个三角形在json文件中的数据结构,我找了几个对比了一下,发现每个三角形的三个边都是三条线组成

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{ "coordinates": [ [19776.669056702118, 32717.534270796306, 9.181425E-9], [20103.606839973643, 32717.534325811146, 9.181425E-9] ], "type": "LineString" }, { "coordinates": [ [19776.669056702118, 32717.534270796306, 9.181425E-9], [19940.13805881275, 32417.534298303737, 9.181425E-9] ], "type": "LineString" }, { "coordinates": [ [19940.13805881275, 32417.534298303737, 9.181425E-9], [20103.606839973643, 32717.534325811146, 9.181425E-9] ], "type": "LineString" }

就是这三条线,三角形的三条边是根据三个点组成,那就根据这个特点(这三条线的坐标总会有相交的点,就是顶点,两两都有相等的)开始写代码查询。

第四步:上完整代码

复制代码
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
package parse; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONArray; //import net.sf.json.JSONArray; //import net.sf.json.JSONObject; import java.util.Iterator; import org.gavaghan.geodesy.Ellipsoid; import org.gavaghan.geodesy.GeodeticCalculator; import org.gavaghan.geodesy.GeodeticCurve; import org.gavaghan.geodesy.GlobalCoordinates; import java.util.List; public class JsonConvert { /** * @param args */ public static void main(String[] args){ int a = 0; // 读取原始json文件并进行操作和输出 try { BufferedReader br = new BufferedReader(new FileReader( "src/json/28f.json"));// 读取原始json文件 String s = null, ws = null; List<JSONArray> list = new ArrayList<JSONArray>(); while ((s = br.readLine()) != null) { // System.out.println(s); try { JSONObject dataJson = new JSONObject(s);// 创建一个包含原始json串的json对象 JSONArray features = dataJson.getJSONArray("features");// 找到features的json数组 for (int i = 0; i < features.length(); i++) { JSONObject info = features.getJSONObject(i);// 获取features数组的第i个json对象 JSONObject properties = info.getJSONObject("properties");// 找到properties的json对象 JSONObject geometry = info.getJSONObject("geometry"); String name = properties.getString("Layer");// 读取properties对象里的name字段值 // if(name.equals("I-FURN")){ // if(name.equals("I-ROOM-NUB")){ if(name.equals("I-POWER")){ // a+=digui(geometry).size(); // list.addAll(digui(geometry)); List<JSONArray> list1 = new ArrayList<JSONArray>(); list1 = digui(geometry); list.addAll(list1); for (JSONArray s1 : list1) { for (JSONArray s2 : list1) { if(s1.getJSONArray(0).toString().equals(s2.getJSONArray(0).toString())){ for (JSONArray s3 : list1) { if(s2.getJSONArray(1).toString().equals(s3.getJSONArray(0).toString())){ if(!s1.toString().equals(s2.toString()) && !s1.toString().equals(s3.toString()) && !s2.toString().equals(s3.toString())){ a++; System.out.println("s1: "+s1+" s2: "+s2+" s3: "+s3); } } } } } } } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } br.close(); System.out.println(a); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // getJson(); // getLength(); } public static void getJson(){ // 读取原始json文件并进行操作和输出 try { BufferedReader br = new BufferedReader(new FileReader( "src/json/28f.json"));// 读取原始json文件 BufferedWriter bw = new BufferedWriter(new FileWriter( "src/json/28f_POWER.json"));// 输出新的json文件 String s = null, ws = null; while ((s = br.readLine()) != null) { // System.out.println(s); try { JSONObject dataJson = new JSONObject(s);// 创建一个包含原始json串的json对象 JSONArray arr = new JSONArray(); JSONArray features = dataJson.getJSONArray("features");// 找到features的json数组 for (int i = 0; i < features.length(); i++) { JSONObject info = features.getJSONObject(i);// 获取features数组的第i个json对象 JSONObject properties = info.getJSONObject("properties");// 找到properties的json对象 // JSONObject geometry = info.getJSONObject("geometry"); String name = properties.getString("Layer");// 读取properties对象里的name字段值 // if(name.equals("I-FURN")){ if(name.equals("I-POWER")){ // if(name.equals("I-ROOM-NUB")){ // if(name.equals("I-POWER")){ // System.out.println(info); arr.put(info); } } ws = "{"type": "FeatureCollection""+","+""features":"+arr.toString()+"}"; } catch (JSONException e) { e.printStackTrace(); } } bw.write(ws); bw.flush(); br.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static List digui(JSONObject obj){ List<JSONArray> list = new ArrayList<JSONArray>(); try{ if(!obj.getString("type").equals("Point") && !obj.getString("type").equals("Polygon")){ // System.out.println(obj.getJSONArray("coordinates")); list.add(obj.getJSONArray("coordinates")); } }catch(Exception e){ try{ // System.out.println("geometry"+obj); JSONArray geometries = obj.getJSONArray("geometries"); for (int j = 0; j < geometries.length(); j++) { JSONObject jj = geometries.getJSONObject(j); try{ if(jj.getString("type").equals("LineString")){ // System.out.println(jj.getJSONArray("coordinates")); list.add(jj.getJSONArray("coordinates")); } // System.out.println("jj:"+jj); // System.out.println(jj.getString("type")+" "); }catch(Exception es){ if(jj.getJSONArray("geometries").length()>0){ for(int k = 0; k < jj.getJSONArray("geometries").length(); k++){ digui(jj.getJSONArray("geometries").getJSONObject(k)); } }else{ digui(jj.getJSONObject("geometry")); } } } }catch(Exception ee){ System.out.println("出错了"); } } return list; } public static void getLength(){ // GlobalCoordinates source = new GlobalCoordinates(19776.669056702118, 32717.534270796306); // GlobalCoordinates target = new GlobalCoordinates(20103.606839973643, 32717.534325811146); // GlobalCoordinates source = new GlobalCoordinates(19776.669056702118, 32717.534270796306); // GlobalCoordinates target = new GlobalCoordinates(19940.13805881275, 32417.534298303737); GlobalCoordinates source = new GlobalCoordinates(61008.986616448245, 27727.143809821988); GlobalCoordinates target = new GlobalCoordinates(60708.98658894084, 27890.61259098288); double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere); double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84); System.out.println("Sphere坐标系计算结果:"+meter1 + "米"); System.out.println("WGS84坐标系计算结果:"+meter2 + "米"); } public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){ //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离 GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo); return geoCurve.getEllipsoidalDistance(); } }

完成,这只是一个思路,你想要的找什么样的图形,需要你自己找出他们的特点,慢慢探寻吧

最后

以上就是勤劳可乐最近收集整理的关于dxf转geojson后,使用java代码找出dxf每个图层中的标志性图标(三角形、四边形等等)的全部内容,更多相关dxf转geojson后,使用java代码找出dxf每个图层中内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部