1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package s57;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import s57.S57att.Att;
|
---|
9 | import s57.S57map.Snode;
|
---|
10 | import s57.S57obj.Obj;
|
---|
11 | import s57.S57val.CatBUA;
|
---|
12 | import s57.S57val.CatROD;
|
---|
13 | import s57.S57val.Conv;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * @author Malcolm Herring
|
---|
17 | */
|
---|
18 | public final class S57osm { // OSM to S57 Object/Attribute and Object/Primitive conversions
|
---|
19 | private S57osm() {
|
---|
20 | // Hide default constructor for utilities classes
|
---|
21 | }
|
---|
22 |
|
---|
23 | // CHECKSTYLE.OFF: LineLength
|
---|
24 |
|
---|
25 | static class KeyVal<V> {
|
---|
26 | Obj obj;
|
---|
27 | Att att;
|
---|
28 | Conv conv;
|
---|
29 | V val;
|
---|
30 | KeyVal(Obj o, Att a, Conv c, V v) {
|
---|
31 | obj = o;
|
---|
32 | att = a;
|
---|
33 | conv = c;
|
---|
34 | val = v;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | private static final HashMap<String, KeyVal<?>> OSMtags = new HashMap<>();
|
---|
39 | static {
|
---|
40 | OSMtags.put("natural=coastline", new KeyVal<>(Obj.COALNE, Att.UNKATT, null, null)); OSMtags.put("natural=water", new KeyVal<>(Obj.LAKARE, Att.UNKATT, null, null));
|
---|
41 | OSMtags.put("water=river", new KeyVal<>(Obj.RIVERS, Att.UNKATT, null, null)); OSMtags.put("water=canal", new KeyVal<>(Obj.CANALS, Att.UNKATT, null, null));
|
---|
42 | OSMtags.put("waterway=riverbank", new KeyVal<>(Obj.RIVERS, Att.UNKATT, null, null)); OSMtags.put("waterway=dock", new KeyVal<>(Obj.HRBBSN, Att.UNKATT, null, null));
|
---|
43 | OSMtags.put("waterway=lock", new KeyVal<>(Obj.HRBBSN, Att.UNKATT, null, null)); OSMtags.put("landuse=basin", new KeyVal<>(Obj.LAKARE, Att.UNKATT, null, null));
|
---|
44 | OSMtags.put("wetland=tidalflat", new KeyVal<>(Obj.DEPARE, Att.DRVAL2, Conv.F, 0.0)); OSMtags.put("tidal=yes", new KeyVal<>(Obj.DEPARE, Att.DRVAL2, Conv.F, 0.0));
|
---|
45 | OSMtags.put("natural=mud", new KeyVal<>(Obj.DEPARE, Att.UNKATT, null, null)); OSMtags.put("natural=sand", new KeyVal<>(Obj.DEPARE, Att.UNKATT, null, null));
|
---|
46 | OSMtags.put("highway=motorway", new KeyVal<>(Obj.ROADWY, Att.CATROD, Conv.E, CatROD.ROD_MWAY)); OSMtags.put("highway=trunk", new KeyVal<>(Obj.ROADWY, Att.CATROD, Conv.E, CatROD.ROD_MAJR));
|
---|
47 | OSMtags.put("highway=primary", new KeyVal<>(Obj.ROADWY, Att.CATROD, Conv.E, CatROD.ROD_MAJR)); OSMtags.put("highway=secondary", new KeyVal<>(Obj.ROADWY, Att.CATROD, Conv.E, CatROD.ROD_MINR));
|
---|
48 | OSMtags.put("highway=tertiary", new KeyVal<>(Obj.ROADWY, Att.CATROD, Conv.E, CatROD.ROD_MINR)); OSMtags.put("highway=residential", new KeyVal<>(Obj.ROADWY, Att.UNKATT, null, null));
|
---|
49 | OSMtags.put("highway=unclassified", new KeyVal<>(Obj.ROADWY, Att.UNKATT, null, null)); OSMtags.put("railway=rail", new KeyVal<>(Obj.RAILWY, Att.UNKATT, null, null));
|
---|
50 | OSMtags.put("man_made=breakwater", new KeyVal<>(Obj.SLCONS, Att.UNKATT, null, null)); OSMtags.put("man_made=groyne", new KeyVal<>(Obj.SLCONS, Att.UNKATT, null, null));
|
---|
51 | OSMtags.put("man_made=pier", new KeyVal<>(Obj.SLCONS, Att.UNKATT, null, null)); OSMtags.put("man_made=jetty", new KeyVal<>(Obj.SLCONS, Att.UNKATT, null, null));
|
---|
52 | OSMtags.put("landuse=industrial", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null)); OSMtags.put("landuse=commercial", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null));
|
---|
53 | OSMtags.put("landuse=retail", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null)); OSMtags.put("landuse=residential", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null));
|
---|
54 | }
|
---|
55 |
|
---|
56 | public static void OSMtag(ArrayList<KeyVal<?>> osm, String key, String val) {
|
---|
57 | KeyVal<?> kv = OSMtags.get(key + "=" + val);
|
---|
58 | if (kv != null) {
|
---|
59 | if (kv.conv == Conv.E) {
|
---|
60 | ArrayList<Enum<?>> list = new ArrayList<>();
|
---|
61 | list.add((Enum<?>) kv.val);
|
---|
62 | osm.add(new KeyVal<>(kv.obj, kv.att, kv.conv, list));
|
---|
63 | } else {
|
---|
64 | osm.add(kv);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | KeyVal<?> kvl = null;
|
---|
68 | KeyVal<?> kvd = null;
|
---|
69 | boolean rc = false;
|
---|
70 | boolean rcl = false;
|
---|
71 | for (KeyVal<?> kvx : osm) {
|
---|
72 | if (kvx.obj == Obj.LAKARE) {
|
---|
73 | kvl = kvx;
|
---|
74 | } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS)) {
|
---|
75 | rc = true;
|
---|
76 | }
|
---|
77 | if (kvx.obj == Obj.DEPARE) {
|
---|
78 | kvd = kvx;
|
---|
79 | } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS) || (kvx.obj == Obj.LAKARE)) {
|
---|
80 | rcl = true;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | if (rc && (kvl != null)) {
|
---|
84 | osm.remove(kvl);
|
---|
85 | }
|
---|
86 | if (rcl && (kvd != null)) {
|
---|
87 | osm.remove(kvd);
|
---|
88 | }
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static void OSMmap(BufferedReader in, S57map map, boolean bb) throws Exception {
|
---|
93 | String k = "";
|
---|
94 | String v = "";
|
---|
95 |
|
---|
96 | double lat = 0;
|
---|
97 | double lon = 0;
|
---|
98 | long id = 0;
|
---|
99 |
|
---|
100 | boolean inOsm = false;
|
---|
101 | boolean inNode = false;
|
---|
102 | boolean inWay = false;
|
---|
103 | boolean inRel = false;
|
---|
104 | map.nodes.put(1L, new Snode());
|
---|
105 | map.nodes.put(2L, new Snode());
|
---|
106 | map.nodes.put(3L, new Snode());
|
---|
107 | map.nodes.put(4L, new Snode());
|
---|
108 |
|
---|
109 | String ln;
|
---|
110 | while ((ln = in.readLine()) != null) {
|
---|
111 | if (inOsm) {
|
---|
112 | if (ln.contains("<bounds") && !bb) {
|
---|
113 | for (String token : ln.split("[ ]+")) {
|
---|
114 | if (token.matches("^minlat=.+")) {
|
---|
115 | map.bounds.minlat = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
116 | map.nodes.get(2L).lat = map.bounds.minlat;
|
---|
117 | map.nodes.get(3L).lat = map.bounds.minlat;
|
---|
118 | } else if (token.matches("^minlon=.+")) {
|
---|
119 | map.bounds.minlon = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
120 | map.nodes.get(1L).lon = map.bounds.minlon;
|
---|
121 | map.nodes.get(2L).lon = map.bounds.minlon;
|
---|
122 | } else if (token.matches("^maxlat=.+")) {
|
---|
123 | map.bounds.maxlat = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
124 | map.nodes.get(1L).lat = map.bounds.maxlat;
|
---|
125 | map.nodes.get(4L).lat = map.bounds.maxlat;
|
---|
126 | } else if (token.matches("^maxlon=.+")) {
|
---|
127 | map.bounds.maxlon = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
128 | map.nodes.get(3L).lon = map.bounds.maxlon;
|
---|
129 | map.nodes.get(4L).lon = map.bounds.maxlon;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | } else {
|
---|
133 | if ((inNode || inWay || inRel) && (ln.contains("<tag"))) {
|
---|
134 | k = v = "";
|
---|
135 | String[] token = ln.split("k=");
|
---|
136 | k = token[1].split("[\"\']")[1];
|
---|
137 | token = token[1].split("v=");
|
---|
138 | v = token[1].split("[\"\']")[1];
|
---|
139 | if (!k.isEmpty() && !v.isEmpty()) {
|
---|
140 | map.addTag(k, v);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | if (inNode) {
|
---|
144 | if (ln.contains("</node")) {
|
---|
145 | inNode = false;
|
---|
146 | map.tagsDone(id);
|
---|
147 | }
|
---|
148 | } else if (ln.contains("<node")) {
|
---|
149 | for (String token : ln.split("[ ]+")) {
|
---|
150 | if (token.matches("^id=.+")) {
|
---|
151 | id = Long.parseLong(token.split("[\"\']")[1]);
|
---|
152 | } else if (token.matches("^lat=.+")) {
|
---|
153 | lat = Double.parseDouble(token.split("[\"\']")[1]);
|
---|
154 | } else if (token.matches("^lon=.+")) {
|
---|
155 | lon = Double.parseDouble(token.split("[\"\']")[1]);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | map.addNode(id, lat, lon);
|
---|
159 | if (ln.contains("/>")) {
|
---|
160 | map.tagsDone(id);
|
---|
161 | } else {
|
---|
162 | inNode = true;
|
---|
163 | }
|
---|
164 | } else if (inWay) {
|
---|
165 | if (ln.contains("<nd")) {
|
---|
166 | long ref = 0;
|
---|
167 | for (String token : ln.split("[ ]+")) {
|
---|
168 | if (token.matches("^ref=.+")) {
|
---|
169 | ref = Long.parseLong(token.split("[\"\']")[1]);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | try {
|
---|
173 | map.addToEdge(ref);
|
---|
174 | } catch (Exception e) {
|
---|
175 | inWay = false;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | if (ln.contains("</way")) {
|
---|
179 | inWay = false;
|
---|
180 | map.tagsDone(id);
|
---|
181 | }
|
---|
182 | } else if (ln.contains("<way")) {
|
---|
183 | for (String token : ln.split("[ ]+")) {
|
---|
184 | if (token.matches("^id=.+")) {
|
---|
185 | id = Long.parseLong(token.split("[\"\']")[1]);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | map.addEdge(id);
|
---|
189 | if (ln.contains("/>")) {
|
---|
190 | map.tagsDone(0);
|
---|
191 | } else {
|
---|
192 | inWay = true;
|
---|
193 | }
|
---|
194 | } else if (ln.contains("</osm")) {
|
---|
195 | map.mapDone();
|
---|
196 | inOsm = false;
|
---|
197 | break;
|
---|
198 | } else if (inRel) {
|
---|
199 | if (ln.contains("<member")) {
|
---|
200 | String type = "";
|
---|
201 | String role = "";
|
---|
202 | long ref = 0;
|
---|
203 | for (String token : ln.split("[ ]+")) {
|
---|
204 | if (token.matches("^ref=.+")) {
|
---|
205 | ref = Long.parseLong(token.split("[\"\']")[1]);
|
---|
206 | } else if (token.matches("^type=.+")) {
|
---|
207 | type = (token.split("[\"\']")[1]);
|
---|
208 | } else if (token.matches("^role=.+")) {
|
---|
209 | String[] str = token.split("[\"\']");
|
---|
210 | if (str.length > 1) {
|
---|
211 | role = (token.split("[\"\']")[1]);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))
|
---|
216 | map.addToArea(ref, role.equals("outer"));
|
---|
217 | }
|
---|
218 | if (ln.contains("</relation")) {
|
---|
219 | inRel = false;
|
---|
220 | map.tagsDone(id);
|
---|
221 | }
|
---|
222 | } else if (ln.contains("<relation")) {
|
---|
223 | for (String token : ln.split("[ ]+")) {
|
---|
224 | if (token.matches("^id=.+")) {
|
---|
225 | id = Long.parseLong(token.split("[\"\']")[1]);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | map.addArea(id);
|
---|
229 | if (ln.contains("/>")) {
|
---|
230 | map.tagsDone(id);
|
---|
231 | } else {
|
---|
232 | inRel = true;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 | } else if (ln.contains("<osm")) {
|
---|
237 | inOsm = true;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | return;
|
---|
241 | }
|
---|
242 |
|
---|
243 | public static void OSMmeta(S57map map) {
|
---|
244 | map.addEdge(++map.xref);
|
---|
245 | for (long ref = 0; ref <= 4; ref++) {
|
---|
246 | map.addToEdge((ref == 0) ? 4 : ref);
|
---|
247 | }
|
---|
248 | map.addTag("seamark:type", "coverage");
|
---|
249 | map.addTag("seamark:coverage:category", "coverage");
|
---|
250 | map.tagsDone(map.xref);
|
---|
251 | }
|
---|
252 |
|
---|
253 | }
|
---|