| 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 | break;
|
|---|
| 470 | case LINE:
|
|---|
| 471 | edges.put(id, edge);
|
|---|
| 472 | nodes.get(edge.first).flg = Nflag.CONN;
|
|---|
| 473 | nodes.get(edge.last).flg = Nflag.CONN;
|
|---|
| 474 | if (edge.first == edge.last) {
|
|---|
| 475 | feature.geom.prim = Pflag.AREA;
|
|---|
| 476 | }
|
|---|
| 477 | break;
|
|---|
| 478 | case AREA:
|
|---|
| 479 | break;
|
|---|
| 480 | default:
|
|---|
| 481 | break;
|
|---|
| 482 | }
|
|---|
| 483 | if ((sortGeom(feature)) && (feature.type != Obj.UNKOBJ) && !((edge != null) && (edge.last == 0))) {
|
|---|
| 484 | index.put(id, feature);
|
|---|
| 485 | if (features.get(feature.type) == null) {
|
|---|
| 486 | features.put(feature.type, new ArrayList<Feature>());
|
|---|
| 487 | }
|
|---|
| 488 | features.get(feature.type).add(feature);
|
|---|
| 489 | feature.geom.centre = findCentroid(feature);
|
|---|
| 490 | }
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | // Utility methods
|
|---|
| 494 |
|
|---|
| 495 | public boolean sortGeom(Feature feature) {
|
|---|
| 496 | Geom sort = new Geom(feature.geom.prim);
|
|---|
| 497 | long first = 0;
|
|---|
| 498 | long last = 0;
|
|---|
| 499 | Comp comp = null;
|
|---|
| 500 | boolean next = true;
|
|---|
| 501 | feature.geom.length = 0;
|
|---|
| 502 | feature.geom.area = 0;
|
|---|
| 503 | if (feature.geom.prim == Pflag.POINT) {
|
|---|
| 504 | return true;
|
|---|
| 505 | } else {
|
|---|
| 506 | int sweep = feature.geom.elems.size();
|
|---|
| 507 | while (!feature.geom.elems.isEmpty()) {
|
|---|
| 508 | Prim prim = feature.geom.elems.remove(0);
|
|---|
| 509 | Edge edge = edges.get(prim.id);
|
|---|
| 510 | if (edge == null) {
|
|---|
| 511 | return false;
|
|---|
| 512 | }
|
|---|
| 513 | if (next == true) {
|
|---|
| 514 | next = false;
|
|---|
| 515 | if (prim.forward) {
|
|---|
| 516 | first = edge.first;
|
|---|
| 517 | last = edge.last;
|
|---|
| 518 | } else {
|
|---|
| 519 | first = edge.last;
|
|---|
| 520 | last = edge.first;
|
|---|
| 521 | }
|
|---|
| 522 | sort.elems.add(prim);
|
|---|
| 523 | if (prim.outer) {
|
|---|
| 524 | sort.outers++;
|
|---|
| 525 | } else {
|
|---|
| 526 | sort.inners++;
|
|---|
| 527 | }
|
|---|
| 528 | comp = new Comp(ref++, 1);
|
|---|
| 529 | sort.refs.add(comp);
|
|---|
| 530 | } else {
|
|---|
| 531 | if (prim.forward) {
|
|---|
| 532 | if (edge.first == last) {
|
|---|
| 533 | sort.elems.add(prim);
|
|---|
| 534 | last = edge.last;
|
|---|
| 535 | comp.size++;
|
|---|
| 536 | } else if (edge.last == first) {
|
|---|
| 537 | sort.elems.add(0, prim);
|
|---|
| 538 | first = edge.first;
|
|---|
| 539 | comp.size++;
|
|---|
| 540 | } else {
|
|---|
| 541 | feature.geom.elems.add(prim);
|
|---|
| 542 | }
|
|---|
| 543 | } else {
|
|---|
| 544 | if (edge.last == last) {
|
|---|
| 545 | sort.elems.add(prim);
|
|---|
| 546 | last = edge.first;
|
|---|
| 547 | comp.size++;
|
|---|
| 548 | } else if (edge.first == first) {
|
|---|
| 549 | sort.elems.add(0, prim);
|
|---|
| 550 | first = edge.last;
|
|---|
| 551 | comp.size++;
|
|---|
| 552 | } else {
|
|---|
| 553 | feature.geom.elems.add(prim);
|
|---|
| 554 | }
|
|---|
| 555 | }
|
|---|
| 556 | }
|
|---|
| 557 | if (--sweep == 0) {
|
|---|
| 558 | next = true;
|
|---|
| 559 | sweep = feature.geom.elems.size();
|
|---|
| 560 | }
|
|---|
| 561 | }
|
|---|
| 562 | if ((sort.prim == Pflag.LINE) && (sort.outers == 1) && (sort.inners == 0) && (first == last)) {
|
|---|
| 563 | sort.prim = Pflag.AREA;
|
|---|
| 564 | }
|
|---|
| 565 | feature.geom = sort;
|
|---|
| 566 | }
|
|---|
| 567 | if (feature.geom.prim == Pflag.AREA) {
|
|---|
| 568 | ArrayList<Prim> outers = new ArrayList<Prim>();
|
|---|
| 569 | ArrayList<Prim> inners = new ArrayList<Prim>();
|
|---|
| 570 | for (Prim prim : feature.geom.elems) {
|
|---|
| 571 | if (prim.outer) {
|
|---|
| 572 | outers.add(prim);
|
|---|
| 573 | } else {
|
|---|
| 574 | inners.add(prim);
|
|---|
| 575 | }
|
|---|
| 576 | }
|
|---|
| 577 | ArrayList<Prim> sorting = outers.size() > 0 ? outers : inners;
|
|---|
| 578 | ArrayList<Prim> closed = null;
|
|---|
| 579 | sort = new Geom(feature.geom.prim);
|
|---|
| 580 | sort.outers = feature.geom.outers;
|
|---|
| 581 | sort.inners = feature.geom.inners;
|
|---|
| 582 | sort.refs = feature.geom.refs;
|
|---|
| 583 | next = true;
|
|---|
| 584 | while (!sorting.isEmpty()) {
|
|---|
| 585 | Prim prim = sorting.remove(0);
|
|---|
| 586 | Edge edge = edges.get(prim.id);
|
|---|
| 587 | if (next == true) {
|
|---|
| 588 | next = false;
|
|---|
| 589 | closed = new ArrayList<Prim>();
|
|---|
| 590 | closed.add(prim);
|
|---|
| 591 | if (prim.forward) {
|
|---|
| 592 | first = edge.first;
|
|---|
| 593 | last = edge.last;
|
|---|
| 594 | } else {
|
|---|
| 595 | first = edge.last;
|
|---|
| 596 | last = edge.first;
|
|---|
| 597 | }
|
|---|
| 598 | } else {
|
|---|
| 599 | if (prim.forward) {
|
|---|
| 600 | if (edge.first == last) {
|
|---|
| 601 | last = edge.last;
|
|---|
| 602 | closed.add(prim);
|
|---|
| 603 | } else {
|
|---|
| 604 | sorting.add(0, prim);
|
|---|
| 605 | next = true;
|
|---|
| 606 | }
|
|---|
| 607 | } else {
|
|---|
| 608 | if (edge.last == last) {
|
|---|
| 609 | last = edge.first;
|
|---|
| 610 | closed.add(prim);
|
|---|
| 611 | } else {
|
|---|
| 612 | sorting.add(0, prim);
|
|---|
| 613 | next = true;
|
|---|
| 614 | }
|
|---|
| 615 | }
|
|---|
| 616 | }
|
|---|
| 617 | if (first == last) {
|
|---|
| 618 | sort.elems.addAll(closed);
|
|---|
| 619 | next = true;
|
|---|
| 620 | }
|
|---|
| 621 | if (sorting.isEmpty() && sorting == outers) {
|
|---|
| 622 | sorting = inners;
|
|---|
| 623 | next = true;
|
|---|
| 624 | }
|
|---|
| 625 | }
|
|---|
| 626 | feature.geom = sort;
|
|---|
| 627 | }
|
|---|
| 628 | feature.geom.length = calcLength(feature.geom);
|
|---|
| 629 | feature.geom.area = calcArea(feature.geom);
|
|---|
| 630 | return true;
|
|---|
| 631 | }
|
|---|
| 632 |
|
|---|
| 633 | public boolean cmpGeoms (Geom g1, Geom g2) {
|
|---|
| 634 | return ((g1.prim == g2.prim) && (g1.outers == g2.outers) && (g1.inners == g2.inners) && (g1.elems.size() == g2.elems.size()));
|
|---|
| 635 | }
|
|---|
| 636 |
|
|---|
| 637 | public class EdgeIterator {
|
|---|
| 638 | Edge edge;
|
|---|
| 639 | boolean forward;
|
|---|
| 640 | ListIterator<Long> it;
|
|---|
| 641 |
|
|---|
| 642 | public EdgeIterator(Edge e, boolean dir) {
|
|---|
| 643 | edge = e;
|
|---|
| 644 | forward = dir;
|
|---|
| 645 | it = null;
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | public boolean hasNext() {
|
|---|
| 649 | return (edge != null);
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 | public long nextRef() {
|
|---|
| 653 | long ref = 0;
|
|---|
| 654 | if (forward) {
|
|---|
| 655 | if (it == null) {
|
|---|
| 656 | ref = edge.first;
|
|---|
| 657 | it = edge.nodes.listIterator();
|
|---|
| 658 | } else {
|
|---|
| 659 | if (it.hasNext()) {
|
|---|
| 660 | ref = it.next();
|
|---|
| 661 | } else {
|
|---|
| 662 | ref = edge.last;
|
|---|
| 663 | edge = null;
|
|---|
| 664 | }
|
|---|
| 665 | }
|
|---|
| 666 | } else {
|
|---|
| 667 | if (it == null) {
|
|---|
| 668 | ref = edge.last;
|
|---|
| 669 | it = edge.nodes.listIterator(edge.nodes.size());
|
|---|
| 670 | } else {
|
|---|
| 671 | if (it.hasPrevious()) {
|
|---|
| 672 | ref = it.previous();
|
|---|
| 673 | } else {
|
|---|
| 674 | ref = edge.first;
|
|---|
| 675 | edge = null;
|
|---|
| 676 | }
|
|---|
| 677 | }
|
|---|
| 678 | }
|
|---|
| 679 | return ref;
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | public Snode next() {
|
|---|
| 683 | return nodes.get(nextRef());
|
|---|
| 684 | }
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | public class GeomIterator {
|
|---|
| 688 | Geom geom;
|
|---|
| 689 | Prim prim;
|
|---|
| 690 | EdgeIterator eit;
|
|---|
| 691 | ListIterator<S57map.Prim> ite;
|
|---|
| 692 | ListIterator<Comp> itc;
|
|---|
| 693 | Comp comp;
|
|---|
| 694 | int ec;
|
|---|
| 695 | long lastref;
|
|---|
| 696 |
|
|---|
| 697 | public GeomIterator(Geom g) {
|
|---|
| 698 | geom = g;
|
|---|
| 699 | lastref = 0;
|
|---|
| 700 | ite = geom.elems.listIterator();
|
|---|
| 701 | itc = geom.refs.listIterator();
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | public boolean hasComp() {
|
|---|
| 705 | return (itc.hasNext());
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | public long nextComp() {
|
|---|
| 709 | comp = itc.next();
|
|---|
| 710 | ec = comp.size;
|
|---|
| 711 | lastref = 0;
|
|---|
| 712 | return comp.ref;
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | public boolean hasEdge() {
|
|---|
| 716 | return (ec > 0) && ite.hasNext();
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | public long nextEdge() {
|
|---|
| 720 | prim = ite.next();
|
|---|
| 721 | eit = new EdgeIterator(edges.get(prim.id), prim.forward);
|
|---|
| 722 | ec--;
|
|---|
| 723 | return prim.id;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | public boolean hasNode() {
|
|---|
| 727 | return (eit.hasNext());
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | public long nextRef(boolean all) {
|
|---|
| 731 | long ref = eit.nextRef();
|
|---|
| 732 | if (!all && (ref == lastref)) {
|
|---|
| 733 | ref = eit.nextRef();
|
|---|
| 734 | }
|
|---|
| 735 | lastref = ref;
|
|---|
| 736 | return ref;
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | public long nextRef() {
|
|---|
| 740 | return nextRef(false);
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | public Snode next() {
|
|---|
| 744 | return nodes.get(nextRef());
|
|---|
| 745 | }
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | double signedArea(Geom geom) {
|
|---|
| 749 | Snode node;
|
|---|
| 750 | double lat, lon, llon, llat;
|
|---|
| 751 | lat = lon = llon = llat = 0;
|
|---|
| 752 | double sigma = 0;
|
|---|
| 753 | GeomIterator git = new GeomIterator(geom);
|
|---|
| 754 | if (git.hasComp()) {
|
|---|
| 755 | git.nextComp();
|
|---|
| 756 | while (git.hasEdge()) {
|
|---|
| 757 | git.nextEdge();
|
|---|
| 758 | while (git.hasNode()) {
|
|---|
| 759 | llon = lon;
|
|---|
| 760 | llat = lat;
|
|---|
| 761 | node = git.next();
|
|---|
| 762 | lat = node.lat;
|
|---|
| 763 | lon = node.lon;
|
|---|
| 764 | sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
|
|---|
| 765 | }
|
|---|
| 766 | }
|
|---|
| 767 | }
|
|---|
| 768 | return sigma / 2.0;
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | public boolean handOfArea(Geom geom) {
|
|---|
| 772 | return (signedArea(geom) < 0);
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | public double calcArea(Geom geom) {
|
|---|
| 776 | return Math.abs(signedArea(geom)) * 3444 * 3444;
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | public double calcLength(Geom geom) {
|
|---|
| 780 | Snode node;
|
|---|
| 781 | double lat, lon, llon, llat;
|
|---|
| 782 | lat = lon = llon = llat = 0;
|
|---|
| 783 | double sigma = 0;
|
|---|
| 784 | boolean first = true;
|
|---|
| 785 | GeomIterator git = new GeomIterator(geom);
|
|---|
| 786 | while (git.hasComp()) {
|
|---|
| 787 | git.nextComp();
|
|---|
| 788 | while (git.hasEdge()) {
|
|---|
| 789 | git.nextEdge();
|
|---|
| 790 | while (git.hasNode()) {
|
|---|
| 791 | node = git.next();
|
|---|
| 792 | if (first) {
|
|---|
| 793 | first = false;
|
|---|
| 794 | lat = node.lat;
|
|---|
| 795 | lon = node.lon;
|
|---|
| 796 | } else {
|
|---|
| 797 | llat = lat;
|
|---|
| 798 | llon = lon;
|
|---|
| 799 | lat = node.lat;
|
|---|
| 800 | lon = node.lon;
|
|---|
| 801 | sigma += Math.acos(Math.sin(lat) * Math.sin(llat) + Math.cos(lat) * Math.cos(llat) * Math.cos(llon - lon));
|
|---|
| 802 | }
|
|---|
| 803 | }
|
|---|
| 804 | }
|
|---|
| 805 | }
|
|---|
| 806 | return sigma * 3444;
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | public Snode findCentroid(Feature feature) {
|
|---|
| 810 | double lat, lon, slat, slon, llat, llon;
|
|---|
| 811 | llat = llon = lat = lon = slat = slon = 0;
|
|---|
| 812 | double sarc = 0;
|
|---|
| 813 | boolean first = true;
|
|---|
| 814 | switch (feature.geom.prim) {
|
|---|
| 815 | case POINT:
|
|---|
| 816 | return nodes.get(feature.geom.elems.get(0).id);
|
|---|
| 817 | case LINE:
|
|---|
| 818 | GeomIterator git = new GeomIterator(feature.geom);
|
|---|
| 819 | while (git.hasComp()) {
|
|---|
| 820 | git.nextComp();
|
|---|
| 821 | while (git.hasEdge()) {
|
|---|
| 822 | git.nextEdge();
|
|---|
| 823 | while (git.hasNode()) {
|
|---|
| 824 | Snode node = git.next();
|
|---|
| 825 | lat = node.lat;
|
|---|
| 826 | lon = node.lon;
|
|---|
| 827 | if (first) {
|
|---|
| 828 | first = false;
|
|---|
| 829 | } else {
|
|---|
| 830 | sarc += (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
|---|
| 831 | }
|
|---|
| 832 | llat = lat;
|
|---|
| 833 | llon = lon;
|
|---|
| 834 | }
|
|---|
| 835 | }
|
|---|
| 836 | }
|
|---|
| 837 | double harc = sarc / 2;
|
|---|
| 838 | sarc = 0;
|
|---|
| 839 | first = true;
|
|---|
| 840 | git = new GeomIterator(feature.geom);
|
|---|
| 841 | while (git.hasComp()) {
|
|---|
| 842 | git.nextComp();
|
|---|
| 843 | while (git.hasEdge()) {
|
|---|
| 844 | git.nextEdge();
|
|---|
| 845 | while (git.hasNode()) {
|
|---|
| 846 | Snode node = git.next();
|
|---|
| 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 | if (sarc > harc)
|
|---|
| 854 | break;
|
|---|
| 855 | }
|
|---|
| 856 | harc -= sarc;
|
|---|
| 857 | llat = lat;
|
|---|
| 858 | llon = lon;
|
|---|
| 859 | }
|
|---|
| 860 | }
|
|---|
| 861 | }
|
|---|
| 862 | return new Snode(llat + ((lat - llat) * harc / sarc), llon + ((lon - llon) * harc / sarc));
|
|---|
| 863 | case AREA:
|
|---|
| 864 | git = new GeomIterator(feature.geom);
|
|---|
| 865 | while (git.hasComp()) {
|
|---|
| 866 | git.nextComp();
|
|---|
| 867 | while (git.hasEdge()) {
|
|---|
| 868 | git.nextEdge();
|
|---|
| 869 | while (git.hasNode()) {
|
|---|
| 870 | Snode node = git.next();
|
|---|
| 871 | lat = node.lat;
|
|---|
| 872 | lon = node.lon;
|
|---|
| 873 | if (first) {
|
|---|
| 874 | first = false;
|
|---|
| 875 | } else {
|
|---|
| 876 | double arc = (Math.acos(Math.cos(lon - llon) * Math.cos(lat - llat)));
|
|---|
| 877 | slat += ((lat + llat) / 2 * arc);
|
|---|
| 878 | slon += ((lon + llon) / 2 * arc);
|
|---|
| 879 | sarc += arc;
|
|---|
| 880 | }
|
|---|
| 881 | llon = lon;
|
|---|
| 882 | llat = lat;
|
|---|
| 883 | }
|
|---|
| 884 | }
|
|---|
| 885 | }
|
|---|
| 886 | return new Snode((sarc > 0.0 ? slat / sarc : 0.0), (sarc > 0.0 ? slon / sarc : 0.0));
|
|---|
| 887 | default:
|
|---|
| 888 | }
|
|---|
| 889 | return null;
|
|---|
| 890 | }
|
|---|
| 891 |
|
|---|
| 892 | }
|
|---|