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 ArrayList<KeyVal<?>> osm;
|
---|
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 = new ArrayList<>();
|
---|
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 = new ArrayList<>();
|
---|
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 = new ArrayList<>();
|
---|
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 | if (obj != Obj.UNKOBJ) {
|
---|
469 | if (val.equals("yes")) {
|
---|
470 | ObjTab objs = feature.objs.get(obj);
|
---|
471 | if (objs == null) {
|
---|
472 | objs = new ObjTab();
|
---|
473 | feature.objs.put(obj, objs);
|
---|
474 | }
|
---|
475 | }
|
---|
476 | } else {
|
---|
477 | Att att = S57att.enumAttribute(subkeys[1], Obj.UNKOBJ);
|
---|
478 | if (att != Att.UNKATT) {
|
---|
479 | AttVal<?> attval = S57val.convertValue(val, att);
|
---|
480 | if (attval.val != null)
|
---|
481 | feature.atts.put(att, attval);
|
---|
482 | }
|
---|
483 | }
|
---|
484 | }
|
---|
485 | }
|
---|
486 | } else if (!sea) {
|
---|
487 | S57osm.OSMtag(osm, key, val);
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | public void tagsDone(long id) {
|
---|
492 | switch (feature.geom.prim) {
|
---|
493 | case POINT:
|
---|
494 | Snode node = nodes.get(id);
|
---|
495 | if ((node.flg != Nflag.CONN) && (node.flg != Nflag.DPTH) && (!feature.objs.isEmpty() || !osm.isEmpty())) {
|
---|
496 | node.flg = Nflag.ISOL;
|
---|
497 | }
|
---|
498 | break;
|
---|
499 | case LINE:
|
---|
500 | edges.put(id, edge);
|
---|
501 | nodes.get(edge.first).flg = Nflag.CONN;
|
---|
502 | nodes.get(edge.last).flg = Nflag.CONN;
|
---|
503 | if (edge.first == edge.last) {
|
---|
504 | feature.geom.prim = Pflag.AREA;
|
---|
505 | }
|
---|
506 | break;
|
---|
507 | case AREA:
|
---|
508 | break;
|
---|
509 | default:
|
---|
510 | break;
|
---|
511 | }
|
---|
512 | if (sortGeom(feature) && !((edge != null) && (edge.last == 0))) {
|
---|
513 | if (feature.type != Obj.UNKOBJ) {
|
---|
514 | index.put(id, feature);
|
---|
515 | if (features.get(feature.type) == null) {
|
---|
516 | features.put(feature.type, new ArrayList<Feature>());
|
---|
517 | }
|
---|
518 | features.get(feature.type).add(feature);
|
---|
519 | }
|
---|
520 | for (KeyVal<?> kvx : osm) {
|
---|
521 | Feature base = new Feature();
|
---|
522 | base.reln = Rflag.MASTER;
|
---|
523 | base.geom = feature.geom;
|
---|
524 | base.type = kvx.obj;
|
---|
525 | ObjTab objs = new ObjTab();
|
---|
526 | base.objs.put(kvx.obj, objs);
|
---|
527 | AttMap atts = new AttMap();
|
---|
528 | objs.put(0, atts);
|
---|
529 | if (kvx.att != Att.UNKATT) {
|
---|
530 | atts.put(kvx.att, new AttVal<>(kvx.conv, kvx.val));
|
---|
531 | }
|
---|
532 | index.put(++xref, base);
|
---|
533 | if (features.get(kvx.obj) == null) {
|
---|
534 | features.put(kvx.obj, new ArrayList<Feature>());
|
---|
535 | }
|
---|
536 | features.get(kvx.obj).add(base);
|
---|
537 | }
|
---|
538 | /* if (!osm.isEmpty()) {
|
---|
539 | if (feature.type == Obj.UNKOBJ) {
|
---|
540 | feature.type = osm.obj;
|
---|
541 | ObjTab objs = feature.objs.get(osm.obj);
|
---|
542 | if (objs == null) {
|
---|
543 | objs = new ObjTab();
|
---|
544 | feature.objs.put(osm.obj, objs);
|
---|
545 | }
|
---|
546 | AttMap atts = objs.get(0);
|
---|
547 | if (atts == null) {
|
---|
548 | atts = new AttMap();
|
---|
549 | objs.put(0, atts);
|
---|
550 | }
|
---|
551 | if (osm.att != Att.UNKATT) {
|
---|
552 | atts.put(osm.att, new AttVal<>(osm.conv, osm.val));
|
---|
553 | }
|
---|
554 | } else {
|
---|
555 | Feature base = new Feature();
|
---|
556 | base.reln = Rflag.MASTER;
|
---|
557 | base.geom = feature.geom;
|
---|
558 | base.type = osm.obj;
|
---|
559 | ObjTab objs = new ObjTab();
|
---|
560 | base.objs.put(osm.obj, objs);
|
---|
561 | AttMap atts = new AttMap();
|
---|
562 | objs.put(0, atts);
|
---|
563 | if (osm.att != Att.UNKATT) {
|
---|
564 | atts.put(osm.att, new AttVal<>(osm.conv, osm.val));
|
---|
565 | }
|
---|
566 | index.put(++xref, base);
|
---|
567 | if (features.get(osm.obj) == null) {
|
---|
568 | features.put(osm.obj, new ArrayList<Feature>());
|
---|
569 | }
|
---|
570 | features.get(osm.obj).add(base);
|
---|
571 | }
|
---|
572 | }*/
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | public void mapDone() {
|
---|
577 | if (!sea) {
|
---|
578 | S57box.bBox(this);
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | // Utility methods
|
---|
583 |
|
---|
584 | public boolean sortGeom(Feature feature) {
|
---|
585 | try {
|
---|
586 | Geom sort = new Geom(feature.geom.prim);
|
---|
587 | long first = 0;
|
---|
588 | long last = 0;
|
---|
589 | Comp comp = null;
|
---|
590 | boolean next = true;
|
---|
591 | feature.geom.length = 0;
|
---|
592 | feature.geom.area = 0;
|
---|
593 | if (feature.geom.elems.isEmpty()) {
|
---|
594 | return false;
|
---|
595 | }
|
---|
596 | if (feature.geom.prim == Pflag.POINT) {
|
---|
597 | feature.geom.centre = nodes.get(feature.geom.elems.get(0).id);
|
---|
598 | return true;
|
---|
599 | }
|
---|
600 | Geom outer = new Geom(feature.geom.prim);
|
---|
601 | Geom inner = new Geom(feature.geom.prim);
|
---|
602 | for (Prim prim : feature.geom.elems) {
|
---|
603 | if (prim.outer) {
|
---|
604 | outer.elems.add(prim);
|
---|
605 | } else {
|
---|
606 | inner.elems.add(prim);
|
---|
607 | }
|
---|
608 | }
|
---|
609 | boolean outin = true;
|
---|
610 | int sweep = outer.elems.size();
|
---|
611 | if (sweep == 0) {
|
---|
612 | return false;
|
---|
613 | }
|
---|
614 | int prev = sweep;
|
---|
615 | int top = 0;
|
---|
616 | while (!outer.elems.isEmpty()) {
|
---|
617 | Prim prim = outer.elems.remove(0);
|
---|
618 | Edge edge = edges.get(prim.id);
|
---|
619 | if (edge == null) {
|
---|
620 | return false;
|
---|
621 | }
|
---|
622 | if (next == true) {
|
---|
623 | next = false;
|
---|
624 | first = edge.first;
|
---|
625 | last = edge.last;
|
---|
626 | prim.forward = true;
|
---|
627 | sort.elems.add(prim);
|
---|
628 | if (prim.outer) {
|
---|
629 | sort.outers++;
|
---|
630 | } else {
|
---|
631 | sort.inners++;
|
---|
632 | }
|
---|
633 | comp = new Comp(cref++, 1);
|
---|
634 | sort.comps.add(comp);
|
---|
635 | } else {
|
---|
636 | if (edge.first == last) {
|
---|
637 | sort.elems.add(prim);
|
---|
638 | last = edge.last;
|
---|
639 | prim.forward = true;
|
---|
640 | comp.size++;
|
---|
641 | } else if (edge.last == first) {
|
---|
642 | sort.elems.add(top, prim);
|
---|
643 | first = edge.first;
|
---|
644 | prim.forward = true;
|
---|
645 | comp.size++;
|
---|
646 | } else if (edge.last == last) {
|
---|
647 | sort.elems.add(prim);
|
---|
648 | last = edge.first;
|
---|
649 | prim.forward = false;
|
---|
650 | comp.size++;
|
---|
651 | } else if (edge.first == first) {
|
---|
652 | sort.elems.add(top, prim);
|
---|
653 | first = edge.last;
|
---|
654 | prim.forward = false;
|
---|
655 | comp.size++;
|
---|
656 | } else {
|
---|
657 | outer.elems.add(prim);
|
---|
658 | }
|
---|
659 | }
|
---|
660 | if (--sweep == 0) {
|
---|
661 | sweep = outer.elems.size();
|
---|
662 | if ((sweep == 0) || (sweep == prev)) {
|
---|
663 | if ((sort.prim == Pflag.AREA) && (first != last)) {
|
---|
664 | return false;
|
---|
665 | }
|
---|
666 | if (outin) {
|
---|
667 | if (sweep != 0) {
|
---|
668 | return false;
|
---|
669 | }
|
---|
670 | outer = inner;
|
---|
671 | outin = false;
|
---|
672 | sweep = outer.elems.size();
|
---|
673 | }
|
---|
674 | next = true;
|
---|
675 | top = sort.elems.size();
|
---|
676 | }
|
---|
677 | prev = sweep;
|
---|
678 | }
|
---|
679 | }
|
---|
680 | if ((sort.prim == Pflag.LINE) && (sort.outers == 1) && (sort.inners == 0) && (first == last)) {
|
---|
681 | sort.prim = Pflag.AREA;
|
---|
682 | }
|
---|
683 | feature.geom = sort;
|
---|
684 | if (feature.geom.prim == Pflag.AREA) {
|
---|
685 | int ie = 0;
|
---|
686 | int ic = 0;
|
---|
687 | while (ie < feature.geom.elems.size()) {
|
---|
688 | double area = calcArea(feature.geom, ic);
|
---|
689 | if (ie == 0)
|
---|
690 | feature.geom.area = Math.abs(area) * 3444 * 3444;
|
---|
691 | if (((ie == 0) && (area < 0.0)) || ((ie > 0) && (area >= 0.0))) {
|
---|
692 | ArrayList<Prim> tmp = new ArrayList<>();
|
---|
693 | for (int i = 0; i < feature.geom.comps.get(ic).size; i++) {
|
---|
694 | Prim p = feature.geom.elems.remove(ie);
|
---|
695 | p.forward = !p.forward;
|
---|
696 | tmp.add(0, p);
|
---|
697 | }
|
---|
698 | feature.geom.elems.addAll(ie, tmp);
|
---|
699 | }
|
---|
700 | ie += feature.geom.comps.get(ic).size;
|
---|
701 | ic++;
|
---|
702 | }
|
---|
703 | }
|
---|
704 | feature.geom.length = calcLength(feature.geom);
|
---|
705 | feature.geom.centre = calcCentroid(feature);
|
---|
706 | return true;
|
---|
707 | } catch (Exception e) {
|
---|
708 | return false;
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 | public boolean cmpGeoms (Geom g1, Geom g2) {
|
---|
713 | return ((g1.prim == g2.prim) && (g1.outers == g2.outers) && (g1.inners == g2.inners) && (g1.elems.size() == g2.elems.size()));
|
---|
714 | }
|
---|
715 |
|
---|
716 | public class EdgeIterator {
|
---|
717 | Edge edge;
|
---|
718 | boolean forward;
|
---|
719 | ListIterator<Long> it;
|
---|
720 |
|
---|
721 | public EdgeIterator(Edge e, boolean dir) {
|
---|
722 | edge = e;
|
---|
723 | forward = dir;
|
---|
724 | it = null;
|
---|
725 | }
|
---|
726 |
|
---|
727 | public boolean hasNext() {
|
---|
728 | return (edge != null);
|
---|
729 | }
|
---|
730 |
|
---|
731 | public long nextRef() {
|
---|
732 | long ref = 0;
|
---|
733 | if (forward) {
|
---|
734 | if (it == null) {
|
---|
735 | ref = edge.first;
|
---|
736 | it = edge.nodes.listIterator();
|
---|
737 | } else {
|
---|
738 | if (it.hasNext()) {
|
---|
739 | ref = it.next();
|
---|
740 | } else {
|
---|
741 | ref = edge.last;
|
---|
742 | edge = null;
|
---|
743 | }
|
---|
744 | }
|
---|
745 | } else {
|
---|
746 | if (it == null) {
|
---|
747 | ref = edge.last;
|
---|
748 | it = edge.nodes.listIterator(edge.nodes.size());
|
---|
749 | } else {
|
---|
750 | if (it.hasPrevious()) {
|
---|
751 | ref = it.previous();
|
---|
752 | } else {
|
---|
753 | ref = edge.first;
|
---|
754 | edge = null;
|
---|
755 | }
|
---|
756 | }
|
---|
757 | }
|
---|
758 | return ref;
|
---|
759 | }
|
---|
760 |
|
---|
761 | public Snode next() {
|
---|
762 | return nodes.get(nextRef());
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | public class GeomIterator {
|
---|
767 | Geom geom;
|
---|
768 | Prim prim;
|
---|
769 | EdgeIterator eit;
|
---|
770 | ListIterator<S57map.Prim> ite;
|
---|
771 | ListIterator<Comp> itc;
|
---|
772 | Comp comp;
|
---|
773 | int ec;
|
---|
774 | long lastref;
|
---|
775 |
|
---|
776 | public GeomIterator(Geom g) {
|
---|
777 | geom = g;
|
---|
778 | lastref = 0;
|
---|
779 | ite = geom.elems.listIterator();
|
---|
780 | itc = geom.comps.listIterator();
|
---|
781 | }
|
---|
782 |
|
---|
783 | public boolean hasComp() {
|
---|
784 | return (itc.hasNext());
|
---|
785 | }
|
---|
786 |
|
---|
787 | public long nextComp() {
|
---|
788 | comp = itc.next();
|
---|
789 | ec = comp.size;
|
---|
790 | lastref = 0;
|
---|
791 | return comp.ref;
|
---|
792 | }
|
---|
793 |
|
---|
794 | public boolean hasEdge() {
|
---|
795 | return (ec > 0) && ite.hasNext();
|
---|
796 | }
|
---|
797 |
|
---|
798 | public long nextEdge() {
|
---|
799 | prim = ite.next();
|
---|
800 | eit = new EdgeIterator(edges.get(prim.id), prim.forward);
|
---|
801 | ec--;
|
---|
802 | return prim.id;
|
---|
803 | }
|
---|
804 |
|
---|
805 | public boolean hasNode() {
|
---|
806 | return (eit.hasNext());
|
---|
807 | }
|
---|
808 |
|
---|
809 | public long nextRef(boolean all) {
|
---|
810 | long ref = eit.nextRef();
|
---|
811 | if (!all && (ref == lastref)) {
|
---|
812 | ref = eit.nextRef();
|
---|
813 | }
|
---|
814 | lastref = ref;
|
---|
815 | return ref;
|
---|
816 | }
|
---|
817 |
|
---|
818 | public long nextRef() {
|
---|
819 | return nextRef(false);
|
---|
820 | }
|
---|
821 |
|
---|
822 | public Snode next() {
|
---|
823 | return nodes.get(nextRef());
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | double calcArea(Geom geom, int comp) {
|
---|
828 | Snode node;
|
---|
829 | double lat, lon, llon, llat;
|
---|
830 | lat = lon = llon = llat = 0;
|
---|
831 | double sigma = 0;
|
---|
832 | GeomIterator git = new GeomIterator(geom);
|
---|
833 | for (int i = 0; i <= comp; i++) {
|
---|
834 | if (git.hasComp()) {
|
---|
835 | git.nextComp();
|
---|
836 | while (git.hasEdge()) {
|
---|
837 | git.nextEdge();
|
---|
838 | while (git.hasNode()) {
|
---|
839 | node = git.next();
|
---|
840 | if (node == null)
|
---|
841 | continue;
|
---|
842 | llon = lon;
|
---|
843 | llat = lat;
|
---|
844 | lat = node.lat;
|
---|
845 | lon = node.lon;
|
---|
846 | sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
|
---|
847 | }
|
---|
848 | }
|
---|
849 | if (i != comp)
|
---|
850 | sigma = lat = lon = llon = llat = 0;
|
---|
851 | }
|
---|
852 | }
|
---|
853 | return sigma / 2.0;
|
---|
854 | }
|
---|
855 |
|
---|
856 | double calcLength(Geom geom) {
|
---|
857 | Snode node;
|
---|
858 | double lat, lon, llon, llat;
|
---|
859 | lat = lon = llon = llat = 0;
|
---|
860 | double sigma = 0;
|
---|
861 | boolean first = true;
|
---|
862 | GeomIterator git = new GeomIterator(geom);
|
---|
863 | while (git.hasComp()) {
|
---|
864 | git.nextComp();
|
---|
865 | while (git.hasEdge()) {
|
---|
866 | git.nextEdge();
|
---|
867 | while (git.hasNode()) {
|
---|
868 | node = git.next();
|
---|
869 | if (first) {
|
---|
870 | first = false;
|
---|
871 | lat = node.lat;
|
---|
872 | lon = node.lon;
|
---|
873 | } else if (node != null) {
|
---|
874 | llat = lat;
|
---|
875 | llon = lon;
|
---|
876 | lat = node.lat;
|
---|
877 | lon = node.lon;
|
---|
878 | sigma += Math.acos(Math.sin(lat) * Math.sin(llat) + Math.cos(lat) * Math.cos(llat) * Math.cos(llon - lon));
|
---|
879 | }
|
---|
880 | }
|
---|
881 | }
|
---|
882 | }
|
---|
883 | return sigma * 3444;
|
---|
884 | }
|
---|
885 |
|
---|
886 | Snode calcCentroid(Feature feature) {
|
---|
887 | double lat, lon, slat, slon, llat, llon;
|
---|
888 | llat = llon = lat = lon = slat = slon = 0;
|
---|
889 | double sarc = 0;
|
---|
890 | boolean first = true;
|
---|
891 | switch (feature.geom.prim) {
|
---|
892 | case POINT:
|
---|
893 | return nodes.get(feature.geom.elems.get(0).id);
|
---|
894 | case LINE:
|
---|
895 | GeomIterator git = new GeomIterator(feature.geom);
|
---|
896 | while (git.hasComp()) {
|
---|
897 | git.nextComp();
|
---|
898 | while (git.hasEdge()) {
|
---|
899 | git.nextEdge();
|
---|
900 | while (git.hasNode()) {
|
---|
901 | Snode node = git.next();
|
---|
902 | if (node == null) continue;
|
---|
903 | lat = node.lat;
|
---|
904 | lon = node.lon;
|
---|
905 | if (first) {
|
---|
906 | first = false;
|
---|
907 | } else {
|
---|
908 | sarc += (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
---|
909 | }
|
---|
910 | llat = lat;
|
---|
911 | llon = lon;
|
---|
912 | }
|
---|
913 | }
|
---|
914 | }
|
---|
915 | double harc = sarc / 2;
|
---|
916 | sarc = 0;
|
---|
917 | first = true;
|
---|
918 | git = new GeomIterator(feature.geom);
|
---|
919 | while (git.hasComp()) {
|
---|
920 | git.nextComp();
|
---|
921 | while (git.hasEdge()) {
|
---|
922 | git.nextEdge();
|
---|
923 | while (git.hasNode()) {
|
---|
924 | Snode node = git.next();
|
---|
925 | if (node == null) continue;
|
---|
926 | lat = node.lat;
|
---|
927 | lon = node.lon;
|
---|
928 | if (first) {
|
---|
929 | first = false;
|
---|
930 | } else {
|
---|
931 | sarc = (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
---|
932 | if (sarc > harc)
|
---|
933 | break;
|
---|
934 | }
|
---|
935 | harc -= sarc;
|
---|
936 | llat = lat;
|
---|
937 | llon = lon;
|
---|
938 | }
|
---|
939 | }
|
---|
940 | }
|
---|
941 | return new Snode(llat + ((lat - llat) * harc / sarc), llon + ((lon - llon) * harc / sarc));
|
---|
942 | case AREA:
|
---|
943 | git = new GeomIterator(feature.geom);
|
---|
944 | while (git.hasComp()) {
|
---|
945 | git.nextComp();
|
---|
946 | while (git.hasEdge()) {
|
---|
947 | git.nextEdge();
|
---|
948 | while (git.hasNode()) {
|
---|
949 | Snode node = git.next();
|
---|
950 | lat = node.lat;
|
---|
951 | lon = node.lon;
|
---|
952 | if (first) {
|
---|
953 | first = false;
|
---|
954 | } else {
|
---|
955 | double arc = (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
---|
956 | slat += ((lat + llat) / 2 * arc);
|
---|
957 | slon += ((lon + llon) / 2 * arc);
|
---|
958 | sarc += arc;
|
---|
959 | }
|
---|
960 | llon = lon;
|
---|
961 | llat = lat;
|
---|
962 | }
|
---|
963 | }
|
---|
964 | }
|
---|
965 | return new Snode((sarc > 0.0 ? slat / sarc : 0.0), (sarc > 0.0 ? slon / sarc : 0.0));
|
---|
966 | default:
|
---|
967 | }
|
---|
968 | return null;
|
---|
969 | }
|
---|
970 |
|
---|
971 | }
|
---|