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