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