1 | /* Copyright 2015 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.io.BufferedReader;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.HashMap;
|
---|
16 |
|
---|
17 | import s57.S57obj.*;
|
---|
18 | import s57.S57att.*;
|
---|
19 | import s57.S57val.*;
|
---|
20 |
|
---|
21 | public class S57osm { // OSM to S57 Object/Attribute conversions
|
---|
22 |
|
---|
23 | static class KeyVal<V> {
|
---|
24 | Obj obj;
|
---|
25 | Att att;
|
---|
26 | Conv conv;
|
---|
27 | V val;
|
---|
28 | KeyVal(Obj o, Att a, Conv c, V v) {
|
---|
29 | obj = o;
|
---|
30 | att = a;
|
---|
31 | conv = c;
|
---|
32 | val = v;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | private static final HashMap<String, KeyVal<?>> OSMtags = new HashMap<String, KeyVal<?>>();
|
---|
37 | static {
|
---|
38 | OSMtags.put("natural=coastline", new KeyVal<>(Obj.COALNE, Att.UNKATT, null, null)); OSMtags.put("natural=water", new KeyVal<>(Obj.LAKARE, Att.UNKATT, null, null));
|
---|
39 | OSMtags.put("waterway=river", new KeyVal<>(Obj.RIVERS, Att.UNKATT, null, null)); OSMtags.put("waterway=riverbank", new KeyVal<>(Obj.LAKARE, Att.UNKATT, null, null));
|
---|
40 | OSMtags.put("waterway=canal", new KeyVal<>(Obj.CANALS, Att.UNKATT, null, null)); OSMtags.put("waterway=dock", new KeyVal<>(Obj.HRBBSN, Att.UNKATT, null, null));
|
---|
41 | OSMtags.put("waterway=lock", new KeyVal<>(Obj.HRBBSN, Att.UNKATT, null, null)); OSMtags.put("landuse=basin", new KeyVal<>(Obj.LAKARE, Att.UNKATT, null, null));
|
---|
42 | OSMtags.put("wetland=tidalflat", new KeyVal<Double>(Obj.DEPARE, Att.DRVAL2, Conv.F, (Double)0.0)); OSMtags.put("tidal=yes", new KeyVal<Double>(Obj.DEPARE, Att.DRVAL2, Conv.F, (Double)0.0));
|
---|
43 | OSMtags.put("natural=mud", new KeyVal<>(Obj.DEPARE, Att.UNKATT, null, null)); OSMtags.put("natural=sand", new KeyVal<>(Obj.DEPARE, Att.UNKATT, null, null));
|
---|
44 | 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));
|
---|
45 | 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));
|
---|
46 | 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));
|
---|
47 | OSMtags.put("highway=unclassified", new KeyVal<>(Obj.ROADWY, Att.UNKATT, null, null)); OSMtags.put("railway=rail", new KeyVal<>(Obj.RAILWY, Att.UNKATT, null, null));
|
---|
48 | 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));
|
---|
49 | 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));
|
---|
50 | OSMtags.put("landuse=industrial", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null)); OSMtags.put("landuse=commercial", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null));
|
---|
51 | OSMtags.put("landuse=retail", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null)); OSMtags.put("landuse=residential", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null));
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static KeyVal<?> OSMtag(String key, String val) {
|
---|
55 | KeyVal<?> kv = OSMtags.get(key + "=" + val);
|
---|
56 | if (kv != null) {
|
---|
57 | if (kv.conv == Conv.E) {
|
---|
58 | ArrayList<Enum<?>> list = new ArrayList<Enum<?>>();
|
---|
59 | list.add((Enum<?>)kv.val);
|
---|
60 | return new KeyVal<>(kv.obj, kv.att, kv.conv, list);
|
---|
61 | }
|
---|
62 | return kv;
|
---|
63 | }
|
---|
64 | return new KeyVal<>(Obj.UNKOBJ, Att.UNKATT, null, null);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static void OSMmap(BufferedReader in, S57map map) throws IOException {
|
---|
68 | String k = "";
|
---|
69 | String v = "";
|
---|
70 |
|
---|
71 | double lat = 0;
|
---|
72 | double lon = 0;
|
---|
73 | long id = 0;
|
---|
74 |
|
---|
75 | boolean inOsm = false;
|
---|
76 | boolean inNode = false;
|
---|
77 | boolean inWay = false;
|
---|
78 | boolean inRel = false;
|
---|
79 |
|
---|
80 | String ln;
|
---|
81 | while ((ln = in.readLine()) != null) {
|
---|
82 | if (inOsm) {
|
---|
83 | if (ln.contains("<bounds")) {
|
---|
84 | for (String token : ln.split("[ ]+")) {
|
---|
85 | if (token.matches("^minlat=.+")) {
|
---|
86 | map.bounds.minlat = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
87 | } else if (token.matches("^minlon=.+")) {
|
---|
88 | map.bounds.minlon = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
89 | } else if (token.matches("^maxlat=.+")) {
|
---|
90 | map.bounds.maxlat = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
91 | } else if (token.matches("^maxlon=.+")) {
|
---|
92 | map.bounds.maxlon = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
|
---|
93 | }
|
---|
94 | }
|
---|
95 | } else {
|
---|
96 | if ((inNode || inWay || inRel) && (ln.contains("<tag"))) {
|
---|
97 | k = v = "";
|
---|
98 | String[] token = ln.split("k=");
|
---|
99 | k = token[1].split("[\"\']")[1];
|
---|
100 | token = token[1].split("v=");
|
---|
101 | v = token[1].split("[\"\']")[1];
|
---|
102 | if (!k.isEmpty() && !v.isEmpty()) {
|
---|
103 | map.addTag(k, v);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | if (inNode) {
|
---|
107 | if (ln.contains("</node")) {
|
---|
108 | inNode = false;
|
---|
109 | map.tagsDone(id);
|
---|
110 | }
|
---|
111 | } else if (ln.contains("<node")) {
|
---|
112 | for (String token : ln.split("[ ]+")) {
|
---|
113 | if (token.matches("^id=.+")) {
|
---|
114 | id = Long.parseLong(token.split("[\"\']")[1]);
|
---|
115 | } else if (token.matches("^lat=.+")) {
|
---|
116 | lat = Double.parseDouble(token.split("[\"\']")[1]);
|
---|
117 | } else if (token.matches("^lon=.+")) {
|
---|
118 | lon = Double.parseDouble(token.split("[\"\']")[1]);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | map.addNode(id, lat, lon);
|
---|
122 | if (ln.contains("/>")) {
|
---|
123 | map.tagsDone(id);
|
---|
124 | } else {
|
---|
125 | inNode = true;
|
---|
126 | }
|
---|
127 | } else if (inWay) {
|
---|
128 | if (ln.contains("<nd")) {
|
---|
129 | long ref = 0;
|
---|
130 | for (String token : ln.split("[ ]+")) {
|
---|
131 | if (token.matches("^ref=.+")) {
|
---|
132 | ref = Long.parseLong(token.split("[\"\']")[1]);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | map.addToEdge(ref);
|
---|
136 | }
|
---|
137 | if (ln.contains("</way")) {
|
---|
138 | inWay = false;
|
---|
139 | map.tagsDone(id);
|
---|
140 | }
|
---|
141 | } else if (ln.contains("<way")) {
|
---|
142 | for (String token : ln.split("[ ]+")) {
|
---|
143 | if (token.matches("^id=.+")) {
|
---|
144 | id = Long.parseLong(token.split("[\"\']")[1]);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | map.addEdge(id);
|
---|
148 | if (ln.contains("/>")) {
|
---|
149 | map.tagsDone(0);
|
---|
150 | } else {
|
---|
151 | inWay = true;
|
---|
152 | }
|
---|
153 | } else if (ln.contains("</osm")) {
|
---|
154 | map.mapDone();
|
---|
155 | inOsm = false;
|
---|
156 | break;
|
---|
157 | } else if (inRel) {
|
---|
158 | if (ln.contains("<member")) {
|
---|
159 | String type = "";
|
---|
160 | String role = "";
|
---|
161 | long ref = 0;
|
---|
162 | for (String token : ln.split("[ ]+")) {
|
---|
163 | if (token.matches("^ref=.+")) {
|
---|
164 | ref = Long.parseLong(token.split("[\"\']")[1]);
|
---|
165 | } else if (token.matches("^type=.+")) {
|
---|
166 | type = (token.split("[\"\']")[1]);
|
---|
167 | } else if (token.matches("^role=.+")) {
|
---|
168 | String str[] = token.split("[\"\']");
|
---|
169 | if (str.length > 1) {
|
---|
170 | role = (token.split("[\"\']")[1]);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))
|
---|
175 | map.addToArea(ref, role.equals("outer"));
|
---|
176 | }
|
---|
177 | if (ln.contains("</relation")) {
|
---|
178 | inRel = false;
|
---|
179 | map.tagsDone(id);
|
---|
180 | }
|
---|
181 | } else if (ln.contains("<relation")) {
|
---|
182 | for (String token : ln.split("[ ]+")) {
|
---|
183 | if (token.matches("^id=.+")) {
|
---|
184 | id = Long.parseLong(token.split("[\"\']")[1]);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | map.addArea(id);
|
---|
188 | if (ln.contains("/>")) {
|
---|
189 | map.tagsDone(id);
|
---|
190 | } else {
|
---|
191 | inRel = true;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | } else if (ln.contains("<osm")) {
|
---|
196 | inOsm = true;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | return;
|
---|
200 | }
|
---|
201 |
|
---|
202 | }
|
---|