1 | /* Copyright 2014 Malcolm Herring
|
---|
2 | *
|
---|
3 | * This is free software: you can redistribute it and/or modify
|
---|
4 | * it under the terms of the GNU General Public License as published by
|
---|
5 | * the Free Software Foundation, version 3 of the License.
|
---|
6 | *
|
---|
7 | * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
|
---|
8 | */
|
---|
9 |
|
---|
10 | package s57;
|
---|
11 |
|
---|
12 | import java.util.*;
|
---|
13 |
|
---|
14 | import s57.S57obj;
|
---|
15 | import s57.S57obj.*;
|
---|
16 | import s57.S57att;
|
---|
17 | import s57.S57att.*;
|
---|
18 | import s57.S57val;
|
---|
19 | import s57.S57val.*;
|
---|
20 | import s57.S57osm;
|
---|
21 | import s57.S57osm.*;
|
---|
22 |
|
---|
23 | public class S57map { // S57/OSM map generation methods
|
---|
24 |
|
---|
25 | public class MapBounds {
|
---|
26 | public double minlat;
|
---|
27 | public double minlon;
|
---|
28 | public double maxlat;
|
---|
29 | public double maxlon;
|
---|
30 | public MapBounds() {
|
---|
31 | minlat = Math.toRadians(90);
|
---|
32 | minlon = Math.toRadians(180);
|
---|
33 | maxlat = Math.toRadians(-90);
|
---|
34 | maxlon = Math.toRadians(-180);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public enum Nflag {
|
---|
39 | ANON, // Edge inner nodes
|
---|
40 | ISOL, // Node not part of Edge
|
---|
41 | CONN, // Edge first and last nodes
|
---|
42 | TRNK, // Edge truncated polygon nodes
|
---|
43 | DPTH // Sounding nodes
|
---|
44 | }
|
---|
45 |
|
---|
46 | public class Snode { // All coordinates in map
|
---|
47 | public double lat; // Latitude in radians
|
---|
48 | public double lon; // Longitude in radians
|
---|
49 | public Nflag flg; // Role of node
|
---|
50 | public double val; // Optional value
|
---|
51 |
|
---|
52 | public Snode() {
|
---|
53 | flg = Nflag.ANON;
|
---|
54 | lat = 0;
|
---|
55 | lon = 0;
|
---|
56 | val = 0;
|
---|
57 | }
|
---|
58 | public Snode(double ilat, double ilon) {
|
---|
59 | flg = Nflag.ANON;
|
---|
60 | lat = ilat;
|
---|
61 | lon = ilon;
|
---|
62 | val = 0;
|
---|
63 | }
|
---|
64 | public Snode(double ilat, double ilon, Nflag iflg) {
|
---|
65 | lat = ilat;
|
---|
66 | lon = ilon;
|
---|
67 | flg = iflg;
|
---|
68 | val = 0;
|
---|
69 | }
|
---|
70 | public Snode(double ilat, double ilon, double ival) {
|
---|
71 | flg = Nflag.DPTH;
|
---|
72 | lat = ilat;
|
---|
73 | lon = ilon;
|
---|
74 | val = ival;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public class Edge { // A polyline segment
|
---|
79 | public long first; // First CONN node
|
---|
80 | public long last; // Last CONN node
|
---|
81 | public ArrayList<Long> nodes; // Inner ANON nodes
|
---|
82 |
|
---|
83 | public Edge() {
|
---|
84 | first = 0;
|
---|
85 | last = 0;
|
---|
86 | nodes = new ArrayList<Long>();
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public enum Rflag {
|
---|
91 | UNKN, MASTER, SLAVE
|
---|
92 | }
|
---|
93 |
|
---|
94 | public class Reln {
|
---|
95 | public long id;
|
---|
96 | public Rflag reln;
|
---|
97 | public Reln(long i, Rflag r) {
|
---|
98 | id = i;
|
---|
99 | reln = r;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public class RelTab extends ArrayList<Reln> {
|
---|
104 | public RelTab() {
|
---|
105 | super();
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public class ObjTab extends HashMap<Integer, AttMap> {
|
---|
110 | public ObjTab() {
|
---|
111 | super();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public class ObjMap extends EnumMap<Obj, ObjTab> {
|
---|
116 | public ObjMap() {
|
---|
117 | super(Obj.class);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public class AttMap extends HashMap<Att, AttVal<?>> {
|
---|
122 | public AttMap() {
|
---|
123 | super();
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public class NodeTab extends HashMap<Long, Snode> {
|
---|
128 | public NodeTab() {
|
---|
129 | super();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | public class EdgeTab extends HashMap<Long, Edge> {
|
---|
134 | public EdgeTab() {
|
---|
135 | super();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public class FtrMap extends EnumMap<Obj, ArrayList<Feature>> {
|
---|
140 | public FtrMap() {
|
---|
141 | super(Obj.class);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public class FtrTab extends HashMap<Long, Feature> {
|
---|
146 | public FtrTab() {
|
---|
147 | super();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public class Prim { // Spatial element
|
---|
152 | public long id; // Snode ID for POINTs, Edge ID for LINEs & AREAs)
|
---|
153 | public boolean forward; // Direction of vector used (LINEs & AREAs)
|
---|
154 | public boolean outer; // Exterior/Interior boundary (AREAs)
|
---|
155 | public boolean trunc; // Cell limit truncation
|
---|
156 | public Prim() {
|
---|
157 | id = 0; forward = true; outer = true; trunc = false;
|
---|
158 | }
|
---|
159 | public Prim(long i) {
|
---|
160 | id = i; forward = true; outer = true; trunc = false;
|
---|
161 | }
|
---|
162 | public Prim(long i, boolean o) {
|
---|
163 | id = i; forward = true; outer = o; trunc = false;
|
---|
164 | }
|
---|
165 | public Prim(long i, boolean f, boolean o) {
|
---|
166 | id = i; forward = f; outer = o; trunc = false;
|
---|
167 | }
|
---|
168 | public Prim(long i, boolean f, boolean o, boolean t) {
|
---|
169 | id = i; forward = f; outer = o; trunc = t;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | public class Comp { // Composite spatial element
|
---|
174 | public long ref; // ID of Comp
|
---|
175 | public int size; // Number of Prims in this Comp
|
---|
176 | public Comp(long r, int s) {
|
---|
177 | ref = r;
|
---|
178 | size = s;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | public enum Pflag {
|
---|
183 | NOSP, POINT, LINE, AREA
|
---|
184 | }
|
---|
185 |
|
---|
186 | public class Geom { // Geometric structure of feature
|
---|
187 | public Pflag prim; // Geometry type
|
---|
188 | public ArrayList<Prim> elems; // Ordered list of elements
|
---|
189 | public int outers; // Number of outers
|
---|
190 | public int inners; // Number of inners
|
---|
191 | public ArrayList<Comp> comps; // Ordered list of compounds
|
---|
192 | public double area; // Area of feature
|
---|
193 | public double length; // Length of feature
|
---|
194 | public Snode centre; // Centre of feature
|
---|
195 | public Geom(Pflag p) {
|
---|
196 | prim = p;
|
---|
197 | elems = new ArrayList<>();
|
---|
198 | outers = inners = 0;
|
---|
199 | comps = new ArrayList<>();
|
---|
200 | area = 0;
|
---|
201 | length = 0;
|
---|
202 | centre = new Snode();
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | public class Feature {
|
---|
207 | public long id; // Ref for this feature
|
---|
208 | public Rflag reln; // Relationship status
|
---|
209 | public Geom geom; // Geometry data
|
---|
210 | public Obj type; // Feature type
|
---|
211 | public AttMap atts; // Feature attributes
|
---|
212 | public RelTab rels; // Related objects
|
---|
213 | public ObjMap objs; // Slave object attributes
|
---|
214 |
|
---|
215 | Feature() {
|
---|
216 | id = 0;
|
---|
217 | reln = Rflag.UNKN;
|
---|
218 | geom = new Geom(Pflag.NOSP);
|
---|
219 | type = Obj.UNKOBJ;
|
---|
220 | atts = new AttMap();
|
---|
221 | rels = new RelTab();
|
---|
222 | objs = new ObjMap();
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | public MapBounds bounds;
|
---|
227 | public NodeTab nodes;
|
---|
228 | public EdgeTab edges;
|
---|
229 | public FtrMap features;
|
---|
230 | public FtrTab index;
|
---|
231 | public long xref;
|
---|
232 |
|
---|
233 | private long cref;
|
---|
234 | private Feature feature;
|
---|
235 | private Edge edge;
|
---|
236 | private KeyVal<?> osm = S57osm.OSMtag("", "");
|
---|
237 | private boolean sea;
|
---|
238 |
|
---|
239 | public S57map(boolean s) {
|
---|
240 | sea = s;
|
---|
241 | nodes = new NodeTab(); // All nodes in map
|
---|
242 | edges = new EdgeTab(); // All edges in map
|
---|
243 | feature = new Feature(); // Current feature being built
|
---|
244 | features = new FtrMap(); // All features in map, grouped by type
|
---|
245 | index = new FtrTab(); // Feature look-up table
|
---|
246 | bounds = new MapBounds();
|
---|
247 | cref = 0x0000ffffffff0000L;// Compound reference generator
|
---|
248 | xref = 0x0fff000000000000L;// Extras reference generator
|
---|
249 | }
|
---|
250 |
|
---|
251 | // S57 map building methods
|
---|
252 |
|
---|
253 | public void newNode(long id, double lat, double lon, Nflag flag) {
|
---|
254 | nodes.put(id, new Snode(Math.toRadians(lat), Math.toRadians(lon), flag));
|
---|
255 | if (flag == Nflag.ANON) {
|
---|
256 | edge.nodes.add(id);
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | public void newNode(long id, double lat, double lon, double depth) {
|
---|
261 | nodes.put(id, new Snode(Math.toRadians(lat), Math.toRadians(lon), depth));
|
---|
262 | }
|
---|
263 |
|
---|
264 | public void newFeature(long id, Pflag p, long objl) {
|
---|
265 | feature = new Feature();
|
---|
266 | Obj obj = S57obj.decodeType(objl);
|
---|
267 | feature.geom = new Geom(p);
|
---|
268 | feature.type = obj;
|
---|
269 | if (obj != Obj.UNKOBJ) {
|
---|
270 | index.put(id, feature);
|
---|
271 | feature.id = id;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | public void newObj(long id, int rind) {
|
---|
276 | Rflag r = Rflag.UNKN;
|
---|
277 | switch (rind) {
|
---|
278 | case 1:
|
---|
279 | r = Rflag.MASTER;
|
---|
280 | break;
|
---|
281 | case 2:
|
---|
282 | r = Rflag.SLAVE;
|
---|
283 | break;
|
---|
284 | case 3:
|
---|
285 | r = Rflag.UNKN;
|
---|
286 | break;
|
---|
287 | }
|
---|
288 | feature.rels.add(new Reln(id, r));
|
---|
289 | }
|
---|
290 |
|
---|
291 | public void endFeature() {
|
---|
292 |
|
---|
293 | }
|
---|
294 |
|
---|
295 | public void newAtt(long attl, String atvl) {
|
---|
296 | Att att = S57att.decodeAttribute(attl);
|
---|
297 | AttVal<?> val = S57val.decodeValue(atvl, att);
|
---|
298 | feature.atts.put(att, val);
|
---|
299 | }
|
---|
300 |
|
---|
301 | public void newPrim(long id, long ornt, long usag) {
|
---|
302 | feature.geom.elems.add(new Prim(id, (ornt != 2), (usag != 2)));
|
---|
303 | }
|
---|
304 |
|
---|
305 | public void addConn(long id, int topi) {
|
---|
306 | if (topi == 1) {
|
---|
307 | edge.first = id;
|
---|
308 | } else {
|
---|
309 | edge.last = id;
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | public void newEdge(long id) {
|
---|
314 | edge = new Edge();
|
---|
315 | edges.put(id, edge);
|
---|
316 | }
|
---|
317 |
|
---|
318 | public void endFile() {
|
---|
319 | for (long id : index.keySet()) {
|
---|
320 | Feature feature = index.get(id);
|
---|
321 | sortGeom(feature);
|
---|
322 | for (Reln reln : feature.rels) {
|
---|
323 | Feature rel = index.get(reln.id);
|
---|
324 | if (cmpGeoms(feature.geom, rel.geom)) {
|
---|
325 | switch (reln.reln) {
|
---|
326 | case SLAVE:
|
---|
327 | feature.reln = Rflag.MASTER;
|
---|
328 | break;
|
---|
329 | default:
|
---|
330 | feature.reln = Rflag.UNKN;
|
---|
331 | break;
|
---|
332 | }
|
---|
333 | rel.reln = reln.reln;
|
---|
334 | } else {
|
---|
335 | reln.reln = Rflag.UNKN;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | }
|
---|
339 | for (long id : index.keySet()) {
|
---|
340 | Feature feature = index.get(id);
|
---|
341 | if (feature.reln == Rflag.UNKN) {
|
---|
342 | feature.reln = Rflag.MASTER;
|
---|
343 | }
|
---|
344 | if ((feature.type != Obj.UNKOBJ) && (feature.reln == Rflag.MASTER)) {
|
---|
345 | if (features.get(feature.type) == null) {
|
---|
346 | features.put(feature.type, new ArrayList<Feature>());
|
---|
347 | }
|
---|
348 | features.get(feature.type).add(feature);
|
---|
349 | }
|
---|
350 | }
|
---|
351 | for (long id : index.keySet()) {
|
---|
352 | Feature feature = index.get(id);
|
---|
353 | for (Reln reln : feature.rels) {
|
---|
354 | Feature rel = index.get(reln.id);
|
---|
355 | if (rel.reln == Rflag.SLAVE) {
|
---|
356 | if (feature.objs.get(rel.type) == null) {
|
---|
357 | feature.objs.put(rel.type, new ObjTab());
|
---|
358 | }
|
---|
359 | ObjTab tab = feature.objs.get(rel.type);
|
---|
360 | int ix = tab.size();
|
---|
361 | tab.put(ix, rel.atts);
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | // OSM map building methods
|
---|
368 |
|
---|
369 | public void addNode(long id, double lat, double lon) {
|
---|
370 | nodes.put(id, new Snode(Math.toRadians(lat), Math.toRadians(lon)));
|
---|
371 | feature = new Feature();
|
---|
372 | feature.id = id;
|
---|
373 | feature.reln = Rflag.UNKN;
|
---|
374 | feature.geom.prim = Pflag.POINT;
|
---|
375 | feature.geom.elems.add(new Prim(id));
|
---|
376 | edge = null;
|
---|
377 | osm = S57osm.OSMtag("", "");
|
---|
378 | }
|
---|
379 |
|
---|
380 | public void addEdge(long id) {
|
---|
381 | feature = new Feature();
|
---|
382 | feature.id = id;
|
---|
383 | feature.reln = Rflag.UNKN;
|
---|
384 | feature.geom.prim = Pflag.LINE;
|
---|
385 | feature.geom.elems.add(new Prim(id));
|
---|
386 | edge = new Edge();
|
---|
387 | osm = S57osm.OSMtag("", "");
|
---|
388 | }
|
---|
389 |
|
---|
390 | public void addToEdge(long node) {
|
---|
391 | if (edge.first == 0) {
|
---|
392 | edge.first = node;
|
---|
393 | nodes.get(node).flg = Nflag.CONN;
|
---|
394 | } else {
|
---|
395 | if (edge.last != 0) {
|
---|
396 | edge.nodes.add(edge.last);
|
---|
397 | }
|
---|
398 | edge.last = node;
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | public void addArea(long id) {
|
---|
403 | feature = new Feature();
|
---|
404 | feature.id = id;
|
---|
405 | feature.reln = Rflag.UNKN;
|
---|
406 | feature.geom.prim = Pflag.AREA;
|
---|
407 | edge = null;
|
---|
408 | osm = S57osm.OSMtag("", "");
|
---|
409 | }
|
---|
410 |
|
---|
411 | public void addToArea(long id, boolean outer) {
|
---|
412 | feature.geom.elems.add(new Prim(id, outer));
|
---|
413 | }
|
---|
414 |
|
---|
415 | public void addTag(String key, String val) {
|
---|
416 | feature.reln = Rflag.MASTER;
|
---|
417 | String subkeys[] = key.split(":");
|
---|
418 | if ((subkeys.length > 1) && subkeys[0].equals("seamark")) {
|
---|
419 | Obj obj = S57obj.enumType(subkeys[1]);
|
---|
420 | if ((subkeys.length > 2) && (obj != Obj.UNKOBJ)) {
|
---|
421 | int idx = 0;
|
---|
422 | Att att = Att.UNKATT;
|
---|
423 | try {
|
---|
424 | idx = Integer.parseInt(subkeys[2]);
|
---|
425 | if (subkeys.length == 4) {
|
---|
426 | att = s57.S57att.enumAttribute(subkeys[3], obj);
|
---|
427 | }
|
---|
428 | } catch (Exception e) {
|
---|
429 | att = S57att.enumAttribute(subkeys[2], obj);
|
---|
430 | }
|
---|
431 | ObjTab objs = feature.objs.get(obj);
|
---|
432 | if (objs == null) {
|
---|
433 | objs = new ObjTab();
|
---|
434 | feature.objs.put(obj, objs);
|
---|
435 | }
|
---|
436 | AttMap atts = objs.get(idx);
|
---|
437 | if (atts == null) {
|
---|
438 | atts = new AttMap();
|
---|
439 | objs.put(idx, atts);
|
---|
440 | }
|
---|
441 | AttVal<?> attval = S57val.convertValue(val, att);
|
---|
442 | if (attval.val != null) {
|
---|
443 | if (att == Att.VALSOU) {
|
---|
444 | Snode node = nodes.get(feature.geom.elems.get(0).id);
|
---|
445 | node.val = (Double) attval.val;
|
---|
446 | }
|
---|
447 | atts.put(att, attval);
|
---|
448 | }
|
---|
449 | } else {
|
---|
450 | if (subkeys[1].equals("type")) {
|
---|
451 | obj = S57obj.enumType(val);
|
---|
452 | feature.type = obj;
|
---|
453 | ObjTab objs = feature.objs.get(obj);
|
---|
454 | if (objs == null) {
|
---|
455 | objs = new ObjTab();
|
---|
456 | feature.objs.put(obj, objs);
|
---|
457 | }
|
---|
458 | AttMap atts = objs.get(0);
|
---|
459 | if (atts == null) {
|
---|
460 | atts = new AttMap();
|
---|
461 | objs.put(0, atts);
|
---|
462 | }
|
---|
463 | if ((obj == Obj.SOUNDG) && (feature.geom.prim == Pflag.POINT)) {
|
---|
464 | Snode node = nodes.get(feature.geom.elems.get(0).id);
|
---|
465 | node.flg = Nflag.DPTH;
|
---|
466 | }
|
---|
467 | } else {
|
---|
468 | Att att = S57att.enumAttribute(subkeys[1], Obj.UNKOBJ);
|
---|
469 | if (att != Att.UNKATT) {
|
---|
470 | AttVal<?> attval = S57val.convertValue(val, att);
|
---|
471 | if (attval.val != null)
|
---|
472 | feature.atts.put(att, attval);
|
---|
473 | }
|
---|
474 | }
|
---|
475 | }
|
---|
476 | } else if (!sea) {
|
---|
477 | KeyVal<?> kv = S57osm.OSMtag(key, val);
|
---|
478 | if (kv.obj != Obj.UNKOBJ) {
|
---|
479 | osm.obj = kv.obj;
|
---|
480 | if (kv.att != Att.UNKATT) {
|
---|
481 | osm = kv;
|
---|
482 | }
|
---|
483 | }
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | public void tagsDone(long id) {
|
---|
488 | switch (feature.geom.prim) {
|
---|
489 | case POINT:
|
---|
490 | Snode node = nodes.get(id);
|
---|
491 | if ((node.flg != Nflag.CONN) && (node.flg != Nflag.DPTH) && (!feature.objs.isEmpty() || (osm.obj != Obj.UNKOBJ))) {
|
---|
492 | node.flg = Nflag.ISOL;
|
---|
493 | }
|
---|
494 | break;
|
---|
495 | case LINE:
|
---|
496 | edges.put(id, edge);
|
---|
497 | nodes.get(edge.first).flg = Nflag.CONN;
|
---|
498 | nodes.get(edge.last).flg = Nflag.CONN;
|
---|
499 | if (edge.first == edge.last) {
|
---|
500 | feature.geom.prim = Pflag.AREA;
|
---|
501 | }
|
---|
502 | break;
|
---|
503 | case AREA:
|
---|
504 | break;
|
---|
505 | default:
|
---|
506 | break;
|
---|
507 | }
|
---|
508 | if (sortGeom(feature) && !((edge != null) && (edge.last == 0))) {
|
---|
509 | if (osm.obj != Obj.UNKOBJ) {
|
---|
510 | if (feature.type == Obj.UNKOBJ) {
|
---|
511 | feature.type = osm.obj;
|
---|
512 | ObjTab objs = feature.objs.get(osm.obj);
|
---|
513 | if (objs == null) {
|
---|
514 | objs = new ObjTab();
|
---|
515 | feature.objs.put(osm.obj, objs);
|
---|
516 | }
|
---|
517 | AttMap atts = objs.get(0);
|
---|
518 | if (atts == null) {
|
---|
519 | atts = new AttMap();
|
---|
520 | objs.put(0, atts);
|
---|
521 | }
|
---|
522 | if (osm.att != Att.UNKATT) {
|
---|
523 | atts.put(osm.att, new AttVal<>(osm.conv, osm.val));
|
---|
524 | }
|
---|
525 | } else {
|
---|
526 | Feature base = new Feature();
|
---|
527 | base.reln = Rflag.MASTER;
|
---|
528 | base.geom = feature.geom;
|
---|
529 | base.type = osm.obj;
|
---|
530 | ObjTab objs = new ObjTab();
|
---|
531 | base.objs.put(osm.obj, objs);
|
---|
532 | AttMap atts = new AttMap();
|
---|
533 | objs.put(0, atts);
|
---|
534 | if (osm.att != Att.UNKATT) {
|
---|
535 | atts.put(osm.att, new AttVal<>(osm.conv, osm.val));
|
---|
536 | }
|
---|
537 | index.put(++xref, base);
|
---|
538 | if (features.get(osm.obj) == null) {
|
---|
539 | features.put(osm.obj, new ArrayList<Feature>());
|
---|
540 | }
|
---|
541 | features.get(osm.obj).add(base);
|
---|
542 | }
|
---|
543 | }
|
---|
544 | if (feature.type != Obj.UNKOBJ) {
|
---|
545 | index.put(id, feature);
|
---|
546 | if (features.get(feature.type) == null) {
|
---|
547 | features.put(feature.type, new ArrayList<Feature>());
|
---|
548 | }
|
---|
549 | features.get(feature.type).add(feature);
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | public void mapDone() {
|
---|
555 | if (!sea) {
|
---|
556 | S57box.bBox(this);
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | // Utility methods
|
---|
561 |
|
---|
562 | public boolean sortGeom(Feature feature) {
|
---|
563 | Geom sort = new Geom(feature.geom.prim);
|
---|
564 | long first = 0;
|
---|
565 | long last = 0;
|
---|
566 | Comp comp = null;
|
---|
567 | boolean next = true;
|
---|
568 | feature.geom.length = 0;
|
---|
569 | feature.geom.area = 0;
|
---|
570 | if (feature.geom.elems.isEmpty()) {
|
---|
571 | return false;
|
---|
572 | }
|
---|
573 | if (feature.geom.prim == Pflag.POINT) {
|
---|
574 | feature.geom.centre = nodes.get(feature.geom.elems.get(0).id);
|
---|
575 | return true;
|
---|
576 | } else {
|
---|
577 | int sweep = feature.geom.elems.size();
|
---|
578 | while (!feature.geom.elems.isEmpty()) {
|
---|
579 | Prim prim = feature.geom.elems.remove(0);
|
---|
580 | Edge edge = edges.get(prim.id);
|
---|
581 | if (edge == null) {
|
---|
582 | return false;
|
---|
583 | }
|
---|
584 | if (next == true) {
|
---|
585 | next = false;
|
---|
586 | first = edge.first;
|
---|
587 | last = edge.last;
|
---|
588 | prim.forward = true;
|
---|
589 | sort.elems.add(prim);
|
---|
590 | if (prim.outer) {
|
---|
591 | sort.outers++;
|
---|
592 | } else {
|
---|
593 | sort.inners++;
|
---|
594 | }
|
---|
595 | comp = new Comp(cref++, 1);
|
---|
596 | sort.comps.add(comp);
|
---|
597 | } else {
|
---|
598 | if (edge.first == last) {
|
---|
599 | sort.elems.add(prim);
|
---|
600 | last = edge.last;
|
---|
601 | prim.forward = true;
|
---|
602 | comp.size++;
|
---|
603 | } else if (edge.last == first) {
|
---|
604 | sort.elems.add(0, prim);
|
---|
605 | first = edge.first;
|
---|
606 | prim.forward = true;
|
---|
607 | comp.size++;
|
---|
608 | } else if (edge.last == last) {
|
---|
609 | sort.elems.add(prim);
|
---|
610 | last = edge.first;
|
---|
611 | prim.forward = false;
|
---|
612 | comp.size++;
|
---|
613 | } else if (edge.first == first) {
|
---|
614 | sort.elems.add(0, prim);
|
---|
615 | first = edge.last;
|
---|
616 | prim.forward = false;
|
---|
617 | comp.size++;
|
---|
618 | } else {
|
---|
619 | feature.geom.elems.add(prim);
|
---|
620 | }
|
---|
621 | }
|
---|
622 | if (--sweep == 0) {
|
---|
623 | next = true;
|
---|
624 | sweep = feature.geom.elems.size();
|
---|
625 | }
|
---|
626 | }
|
---|
627 | if ((sort.prim == Pflag.LINE) && (sort.outers == 1) && (sort.inners == 0) && (first == last)) {
|
---|
628 | sort.prim = Pflag.AREA;
|
---|
629 | }
|
---|
630 | feature.geom = sort;
|
---|
631 | }
|
---|
632 | if (feature.geom.prim == Pflag.AREA) {
|
---|
633 | int ie = 0;
|
---|
634 | int ic = 0;
|
---|
635 | while (ie < feature.geom.elems.size()) {
|
---|
636 | double area = calcArea(feature.geom, ic);
|
---|
637 | if (ie == 0) feature.geom.area = Math.abs(area) * 3444 * 3444;
|
---|
638 | if (((ie == 0) && (area < 0.0)) || ((ie > 0) && (area >= 0.0))) {
|
---|
639 | ArrayList<Prim> tmp = new ArrayList<>();
|
---|
640 | for (int i = 0; i < feature.geom.comps.get(ic).size; i++) {
|
---|
641 | Prim p = feature.geom.elems.remove(ie);
|
---|
642 | p.forward = !p.forward;
|
---|
643 | tmp.add(0, p);
|
---|
644 | }
|
---|
645 | feature.geom.elems.addAll(ie, tmp);
|
---|
646 | }
|
---|
647 | ie += feature.geom.comps.get(ic).size;
|
---|
648 | ic++;
|
---|
649 | }
|
---|
650 | }
|
---|
651 | feature.geom.length = calcLength(feature.geom);
|
---|
652 | feature.geom.centre = calcCentroid(feature);
|
---|
653 | return true;
|
---|
654 | }
|
---|
655 |
|
---|
656 | public boolean cmpGeoms (Geom g1, Geom g2) {
|
---|
657 | return ((g1.prim == g2.prim) && (g1.outers == g2.outers) && (g1.inners == g2.inners) && (g1.elems.size() == g2.elems.size()));
|
---|
658 | }
|
---|
659 |
|
---|
660 | public class EdgeIterator {
|
---|
661 | Edge edge;
|
---|
662 | boolean forward;
|
---|
663 | ListIterator<Long> it;
|
---|
664 |
|
---|
665 | public EdgeIterator(Edge e, boolean dir) {
|
---|
666 | edge = e;
|
---|
667 | forward = dir;
|
---|
668 | it = null;
|
---|
669 | }
|
---|
670 |
|
---|
671 | public boolean hasNext() {
|
---|
672 | return (edge != null);
|
---|
673 | }
|
---|
674 |
|
---|
675 | public long nextRef() {
|
---|
676 | long ref = 0;
|
---|
677 | if (forward) {
|
---|
678 | if (it == null) {
|
---|
679 | ref = edge.first;
|
---|
680 | it = edge.nodes.listIterator();
|
---|
681 | } else {
|
---|
682 | if (it.hasNext()) {
|
---|
683 | ref = it.next();
|
---|
684 | } else {
|
---|
685 | ref = edge.last;
|
---|
686 | edge = null;
|
---|
687 | }
|
---|
688 | }
|
---|
689 | } else {
|
---|
690 | if (it == null) {
|
---|
691 | ref = edge.last;
|
---|
692 | it = edge.nodes.listIterator(edge.nodes.size());
|
---|
693 | } else {
|
---|
694 | if (it.hasPrevious()) {
|
---|
695 | ref = it.previous();
|
---|
696 | } else {
|
---|
697 | ref = edge.first;
|
---|
698 | edge = null;
|
---|
699 | }
|
---|
700 | }
|
---|
701 | }
|
---|
702 | return ref;
|
---|
703 | }
|
---|
704 |
|
---|
705 | public Snode next() {
|
---|
706 | return nodes.get(nextRef());
|
---|
707 | }
|
---|
708 | }
|
---|
709 |
|
---|
710 | public class GeomIterator {
|
---|
711 | Geom geom;
|
---|
712 | Prim prim;
|
---|
713 | EdgeIterator eit;
|
---|
714 | ListIterator<S57map.Prim> ite;
|
---|
715 | ListIterator<Comp> itc;
|
---|
716 | Comp comp;
|
---|
717 | int ec;
|
---|
718 | long lastref;
|
---|
719 |
|
---|
720 | public GeomIterator(Geom g) {
|
---|
721 | geom = g;
|
---|
722 | lastref = 0;
|
---|
723 | ite = geom.elems.listIterator();
|
---|
724 | itc = geom.comps.listIterator();
|
---|
725 | }
|
---|
726 |
|
---|
727 | public boolean hasComp() {
|
---|
728 | return (itc.hasNext());
|
---|
729 | }
|
---|
730 |
|
---|
731 | public long nextComp() {
|
---|
732 | comp = itc.next();
|
---|
733 | ec = comp.size;
|
---|
734 | lastref = 0;
|
---|
735 | return comp.ref;
|
---|
736 | }
|
---|
737 |
|
---|
738 | public boolean hasEdge() {
|
---|
739 | return (ec > 0) && ite.hasNext();
|
---|
740 | }
|
---|
741 |
|
---|
742 | public long nextEdge() {
|
---|
743 | prim = ite.next();
|
---|
744 | eit = new EdgeIterator(edges.get(prim.id), prim.forward);
|
---|
745 | ec--;
|
---|
746 | return prim.id;
|
---|
747 | }
|
---|
748 |
|
---|
749 | public boolean hasNode() {
|
---|
750 | return (eit.hasNext());
|
---|
751 | }
|
---|
752 |
|
---|
753 | public long nextRef(boolean all) {
|
---|
754 | long ref = eit.nextRef();
|
---|
755 | if (!all && (ref == lastref)) {
|
---|
756 | ref = eit.nextRef();
|
---|
757 | }
|
---|
758 | lastref = ref;
|
---|
759 | return ref;
|
---|
760 | }
|
---|
761 |
|
---|
762 | public long nextRef() {
|
---|
763 | return nextRef(false);
|
---|
764 | }
|
---|
765 |
|
---|
766 | public Snode next() {
|
---|
767 | return nodes.get(nextRef());
|
---|
768 | }
|
---|
769 | }
|
---|
770 |
|
---|
771 | double calcArea(Geom geom, int comp) {
|
---|
772 | Snode node;
|
---|
773 | double lat, lon, llon, llat;
|
---|
774 | lat = lon = llon = llat = 0;
|
---|
775 | double sigma = 0;
|
---|
776 | GeomIterator git = new GeomIterator(geom);
|
---|
777 | for (int i = 0; i <= comp; i++) {
|
---|
778 | if (git.hasComp()) {
|
---|
779 | git.nextComp();
|
---|
780 | while (git.hasEdge()) {
|
---|
781 | git.nextEdge();
|
---|
782 | while (git.hasNode()) {
|
---|
783 | node = git.next();
|
---|
784 | if (node == null)
|
---|
785 | continue;
|
---|
786 | llon = lon;
|
---|
787 | llat = lat;
|
---|
788 | lat = node.lat;
|
---|
789 | lon = node.lon;
|
---|
790 | sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
|
---|
791 | }
|
---|
792 | }
|
---|
793 | if (i != comp)
|
---|
794 | sigma = lat = lon = llon = llat = 0;
|
---|
795 | }
|
---|
796 | }
|
---|
797 | return sigma / 2.0;
|
---|
798 | }
|
---|
799 |
|
---|
800 | double calcLength(Geom geom) {
|
---|
801 | Snode node;
|
---|
802 | double lat, lon, llon, llat;
|
---|
803 | lat = lon = llon = llat = 0;
|
---|
804 | double sigma = 0;
|
---|
805 | boolean first = true;
|
---|
806 | GeomIterator git = new GeomIterator(geom);
|
---|
807 | while (git.hasComp()) {
|
---|
808 | git.nextComp();
|
---|
809 | while (git.hasEdge()) {
|
---|
810 | git.nextEdge();
|
---|
811 | while (git.hasNode()) {
|
---|
812 | node = git.next();
|
---|
813 | if (first) {
|
---|
814 | first = false;
|
---|
815 | lat = node.lat;
|
---|
816 | lon = node.lon;
|
---|
817 | } else if (node != null) {
|
---|
818 | llat = lat;
|
---|
819 | llon = lon;
|
---|
820 | lat = node.lat;
|
---|
821 | lon = node.lon;
|
---|
822 | sigma += Math.acos(Math.sin(lat) * Math.sin(llat) + Math.cos(lat) * Math.cos(llat) * Math.cos(llon - lon));
|
---|
823 | }
|
---|
824 | }
|
---|
825 | }
|
---|
826 | }
|
---|
827 | return sigma * 3444;
|
---|
828 | }
|
---|
829 |
|
---|
830 | Snode calcCentroid(Feature feature) {
|
---|
831 | double lat, lon, slat, slon, llat, llon;
|
---|
832 | llat = llon = lat = lon = slat = slon = 0;
|
---|
833 | double sarc = 0;
|
---|
834 | boolean first = true;
|
---|
835 | switch (feature.geom.prim) {
|
---|
836 | case POINT:
|
---|
837 | return nodes.get(feature.geom.elems.get(0).id);
|
---|
838 | case LINE:
|
---|
839 | GeomIterator git = new GeomIterator(feature.geom);
|
---|
840 | while (git.hasComp()) {
|
---|
841 | git.nextComp();
|
---|
842 | while (git.hasEdge()) {
|
---|
843 | git.nextEdge();
|
---|
844 | while (git.hasNode()) {
|
---|
845 | Snode node = git.next();
|
---|
846 | if (node == null) continue;
|
---|
847 | lat = node.lat;
|
---|
848 | lon = node.lon;
|
---|
849 | if (first) {
|
---|
850 | first = false;
|
---|
851 | } else {
|
---|
852 | sarc += (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
---|
853 | }
|
---|
854 | llat = lat;
|
---|
855 | llon = lon;
|
---|
856 | }
|
---|
857 | }
|
---|
858 | }
|
---|
859 | double harc = sarc / 2;
|
---|
860 | sarc = 0;
|
---|
861 | first = true;
|
---|
862 | git = new GeomIterator(feature.geom);
|
---|
863 | while (git.hasComp()) {
|
---|
864 | git.nextComp();
|
---|
865 | while (git.hasEdge()) {
|
---|
866 | git.nextEdge();
|
---|
867 | while (git.hasNode()) {
|
---|
868 | Snode node = git.next();
|
---|
869 | if (node == null) continue;
|
---|
870 | lat = node.lat;
|
---|
871 | lon = node.lon;
|
---|
872 | if (first) {
|
---|
873 | first = false;
|
---|
874 | } else {
|
---|
875 | sarc = (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
---|
876 | if (sarc > harc)
|
---|
877 | break;
|
---|
878 | }
|
---|
879 | harc -= sarc;
|
---|
880 | llat = lat;
|
---|
881 | llon = lon;
|
---|
882 | }
|
---|
883 | }
|
---|
884 | }
|
---|
885 | return new Snode(llat + ((lat - llat) * harc / sarc), llon + ((lon - llon) * harc / sarc));
|
---|
886 | case AREA:
|
---|
887 | git = new GeomIterator(feature.geom);
|
---|
888 | while (git.hasComp()) {
|
---|
889 | git.nextComp();
|
---|
890 | while (git.hasEdge()) {
|
---|
891 | git.nextEdge();
|
---|
892 | while (git.hasNode()) {
|
---|
893 | Snode node = git.next();
|
---|
894 | lat = node.lat;
|
---|
895 | lon = node.lon;
|
---|
896 | if (first) {
|
---|
897 | first = false;
|
---|
898 | } else {
|
---|
899 | double arc = (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
---|
900 | slat += ((lat + llat) / 2 * arc);
|
---|
901 | slon += ((lon + llon) / 2 * arc);
|
---|
902 | sarc += arc;
|
---|
903 | }
|
---|
904 | llon = lon;
|
---|
905 | llat = lat;
|
---|
906 | }
|
---|
907 | }
|
---|
908 | }
|
---|
909 | return new Snode((sarc > 0.0 ? slat / sarc : 0.0), (sarc > 0.0 ? slon / sarc : 0.0));
|
---|
910 | default:
|
---|
911 | }
|
---|
912 | return null;
|
---|
913 | }
|
---|
914 |
|
---|
915 | }
|
---|