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