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