| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package s57;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.ArrayList;
|
|---|
| 5 |
|
|---|
| 6 | import s57.S57map.AttMap;
|
|---|
| 7 | import s57.S57map.Edge;
|
|---|
| 8 | import s57.S57map.Feature;
|
|---|
| 9 | import s57.S57map.ObjTab;
|
|---|
| 10 | import s57.S57map.Pflag;
|
|---|
| 11 | import s57.S57map.Prim;
|
|---|
| 12 | import s57.S57map.Rflag;
|
|---|
| 13 | import s57.S57map.Snode;
|
|---|
| 14 | import s57.S57obj.Obj;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @author Malcolm Herring
|
|---|
| 18 | */
|
|---|
| 19 | public final class S57box { //S57 bounding box truncation
|
|---|
| 20 | private S57box() {
|
|---|
| 21 | // Hide default constructor for utilities classes
|
|---|
| 22 | }
|
|---|
| 23 | // CHECKSTYLE.OFF: LineLength
|
|---|
| 24 |
|
|---|
| 25 | enum Ext { I, N, W, S, E }
|
|---|
| 26 |
|
|---|
| 27 | static Ext getExt(S57map map, double lat, double lon) {
|
|---|
| 28 | if ((lat >= map.bounds.maxlat) && (lon < map.bounds.maxlon)) {
|
|---|
| 29 | return Ext.N;
|
|---|
| 30 | } else if (lon <= map.bounds.minlon) {
|
|---|
| 31 | return Ext.W;
|
|---|
| 32 | } else if (lat <= map.bounds.minlat) {
|
|---|
| 33 | return Ext.S;
|
|---|
| 34 | } else if (lon >= map.bounds.maxlon) {
|
|---|
| 35 | return Ext.E;
|
|---|
| 36 | }
|
|---|
| 37 | return Ext.I;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public static void bBox(S57map map) {
|
|---|
| 41 | /* Truncations
|
|---|
| 42 | * Points: delete point features outside BB
|
|---|
| 43 | * Lines: Truncate edges at BB boundaries
|
|---|
| 44 | * Areas: Truncate edges of outers & inners and add new border edges. Merge inners to outer where necessary
|
|---|
| 45 | * Remove nodes outside BB
|
|---|
| 46 | * Remove edges that are completely outside BB
|
|---|
| 47 | */
|
|---|
| 48 | class Land {
|
|---|
| 49 | long first;
|
|---|
| 50 | Snode start;
|
|---|
| 51 | Ext sbound;
|
|---|
| 52 | long last;
|
|---|
| 53 | Snode end;
|
|---|
| 54 | Ext ebound;
|
|---|
| 55 | Feature land;
|
|---|
| 56 |
|
|---|
| 57 | Land(Feature l) {
|
|---|
| 58 | land = l;
|
|---|
| 59 | first = last = 0;
|
|---|
| 60 | start = end = null;
|
|---|
| 61 | sbound = ebound = Ext.I;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (map.features.get(Obj.COALNE) != null) {
|
|---|
| 66 | ArrayList<Feature> coasts = new ArrayList<>();
|
|---|
| 67 | ArrayList<Land> lands = new ArrayList<>();
|
|---|
| 68 | if (map.features.get(Obj.LNDARE) == null) {
|
|---|
| 69 | map.features.put(Obj.LNDARE, new ArrayList<Feature>());
|
|---|
| 70 | }
|
|---|
| 71 | for (Feature feature : map.features.get(Obj.COALNE)) {
|
|---|
| 72 | Feature land = new Feature();
|
|---|
| 73 | land.id = ++map.xref;
|
|---|
| 74 | land.type = Obj.LNDARE;
|
|---|
| 75 | land.reln = Rflag.MASTER;
|
|---|
| 76 | land.objs.put(Obj.LNDARE, new ObjTab());
|
|---|
| 77 | land.objs.get(Obj.LNDARE).put(0, new AttMap());
|
|---|
| 78 | if (feature.geom.prim == Pflag.AREA) {
|
|---|
| 79 | land.geom = feature.geom;
|
|---|
| 80 | map.features.get(Obj.LNDARE).add(land);
|
|---|
| 81 | } else if (feature.geom.prim == Pflag.LINE) {
|
|---|
| 82 | land.geom.prim = Pflag.LINE;
|
|---|
| 83 | land.geom.elems.addAll(feature.geom.elems);
|
|---|
| 84 | coasts.add(land);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | while (coasts.size() > 0) {
|
|---|
| 88 | Feature land = coasts.remove(0);
|
|---|
| 89 | Edge fedge = map.edges.get(land.geom.elems.get(0).id);
|
|---|
| 90 | long first = fedge.first;
|
|---|
| 91 | long last = map.edges.get(land.geom.elems.get(land.geom.elems.size() - 1).id).last;
|
|---|
| 92 | if (coasts.size() > 0) {
|
|---|
| 93 | boolean added = true;
|
|---|
| 94 | while (added) {
|
|---|
| 95 | added = false;
|
|---|
| 96 | for (int i = 0; i < coasts.size(); i++) {
|
|---|
| 97 | Feature coast = coasts.get(i);
|
|---|
| 98 | Edge edge = map.edges.get(coast.geom.elems.get(0).id);
|
|---|
| 99 | if (edge.first == last) {
|
|---|
| 100 | land.geom.elems.add(coast.geom.elems.get(0));
|
|---|
| 101 | last = edge.last;
|
|---|
| 102 | coasts.remove(i--);
|
|---|
| 103 | added = true;
|
|---|
| 104 | } else if (edge.last == first) {
|
|---|
| 105 | land.geom.elems.add(0, coast.geom.elems.get(0));
|
|---|
| 106 | first = edge.first;
|
|---|
| 107 | coasts.remove(i--);
|
|---|
| 108 | added = true;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | lands.add(new Land(land));
|
|---|
| 114 | }
|
|---|
| 115 | ArrayList<Land> islands = new ArrayList<>();
|
|---|
| 116 | for (Land land : lands) {
|
|---|
| 117 | map.sortGeom(land.land);
|
|---|
| 118 | if (land.land.geom.prim == Pflag.AREA) {
|
|---|
| 119 | islands.add(land);
|
|---|
| 120 | map.features.get(Obj.LNDARE).add(land.land);
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | for (Land island : islands) {
|
|---|
| 124 | lands.remove(island);
|
|---|
| 125 | }
|
|---|
| 126 | for (Land land : lands) {
|
|---|
| 127 | land.first = map.edges.get(land.land.geom.elems.get(0).id).first;
|
|---|
| 128 | land.start = map.nodes.get(land.first);
|
|---|
| 129 | land.sbound = getExt(map, land.start.lat, land.start.lon);
|
|---|
| 130 | land.last = map.edges.get(land.land.geom.elems.get(land.land.geom.comps.get(0).size - 1).id).last;
|
|---|
| 131 | land.end = map.nodes.get(land.last);
|
|---|
| 132 | land.ebound = getExt(map, land.end.lat, land.end.lon);
|
|---|
| 133 | }
|
|---|
| 134 | islands = new ArrayList<>();
|
|---|
| 135 | for (Land land : lands) {
|
|---|
| 136 | if ((land.sbound == Ext.I) || (land.ebound == Ext.I)) {
|
|---|
| 137 | islands.add(land);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | for (Land island : islands) {
|
|---|
| 141 | lands.remove(island);
|
|---|
| 142 | }
|
|---|
| 143 | for (Land land : lands) {
|
|---|
| 144 | Edge nedge = new Edge();
|
|---|
| 145 | nedge.first = land.last;
|
|---|
| 146 | nedge.last = land.first;
|
|---|
| 147 | Ext bound = land.ebound;
|
|---|
| 148 | while (bound != land.sbound) {
|
|---|
| 149 | switch (bound) {
|
|---|
| 150 | case N:
|
|---|
| 151 | nedge.nodes.add(1L);
|
|---|
| 152 | bound = Ext.W;
|
|---|
| 153 | break;
|
|---|
| 154 | case W:
|
|---|
| 155 | nedge.nodes.add(2L);
|
|---|
| 156 | bound = Ext.S;
|
|---|
| 157 | break;
|
|---|
| 158 | case S:
|
|---|
| 159 | nedge.nodes.add(3L);
|
|---|
| 160 | bound = Ext.E;
|
|---|
| 161 | break;
|
|---|
| 162 | case E:
|
|---|
| 163 | nedge.nodes.add(4L);
|
|---|
| 164 | bound = Ext.N;
|
|---|
| 165 | break;
|
|---|
| 166 | default:
|
|---|
| 167 | continue;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 | map.edges.put(++map.xref, nedge);
|
|---|
| 171 | land.land.geom.elems.add(new Prim(map.xref));
|
|---|
| 172 | land.land.geom.comps.get(0).size++;
|
|---|
| 173 | land.land.geom.prim = Pflag.AREA;
|
|---|
| 174 | map.features.get(Obj.LNDARE).add(land.land);
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | return;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|