source: osm/applications/editors/josm/plugins/seachart/src/s57/S57osm.java@ 32394

Last change on this file since 32394 was 32394, checked in by donvip, 8 years ago

checkstyle

File size: 12.2 KB
Line 
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
10package s57;
11
12import java.io.BufferedReader;
13import java.util.ArrayList;
14import java.util.HashMap;
15
16import s57.S57att.Att;
17import s57.S57obj.Obj;
18import s57.S57val.CatBUA;
19import s57.S57val.CatROD;
20import s57.S57val.Conv;
21
22public class S57osm { // OSM to S57 Object/Attribute and Object/Primitive conversions
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 OSMtags.put("place=city", new KeyVal<>(Obj.BUAARE, Att.CATBUA, Conv.E, CatBUA.BUA_CITY)); OSMtags.put("place=town", new KeyVal<>(Obj.BUAARE, Att.CATBUA, Conv.E, CatBUA.BUA_TOWN));
55 OSMtags.put("place=village", new KeyVal<>(Obj.BUAARE, Att.CATBUA, Conv.E, CatBUA.BUA_VLLG));
56 }
57
58 public static void OSMtag(ArrayList<KeyVal<?>> osm, String key, String val) {
59 KeyVal<?> kv = OSMtags.get(key + "=" + val);
60 if (kv != null) {
61 if (kv.conv == Conv.E) {
62 ArrayList<Enum<?>> list = new ArrayList<>();
63 list.add((Enum<?>) kv.val);
64 osm.add(new KeyVal<>(kv.obj, kv.att, kv.conv, list));
65 } else {
66 osm.add(kv);
67 }
68 }
69 KeyVal<?> kvl = null;
70 KeyVal<?> kvd = null;
71 boolean rc = false;
72 boolean rcl = false;
73 for (KeyVal<?> kvx : osm) {
74 if (kvx.obj == Obj.LAKARE) {
75 kvl = kvx;
76 } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS)) {
77 rc = true;
78 }
79 if (kvx.obj == Obj.DEPARE) {
80 kvd = kvx;
81 } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS) || (kvx.obj == Obj.LAKARE)) {
82 rcl = true;
83 }
84 }
85 if (rc && (kvl != null)) {
86 osm.remove(kvl);
87 }
88 if (rcl && (kvd != null)) {
89 osm.remove(kvd);
90 }
91 return;
92 }
93
94 public static void OSMmap(BufferedReader in, S57map map, boolean bb) throws Exception {
95 String k = "";
96 String v = "";
97
98 double lat = 0;
99 double lon = 0;
100 long id = 0;
101
102 boolean inOsm = false;
103 boolean inNode = false;
104 boolean inWay = false;
105 boolean inRel = false;
106 map.nodes.put(1L, map.new Snode());
107 map.nodes.put(2L, map.new Snode());
108 map.nodes.put(3L, map.new Snode());
109 map.nodes.put(4L, map.new Snode());
110
111 String ln;
112 while ((ln = in.readLine()) != null) {
113 if (inOsm) {
114 if (ln.contains("<bounds") && !bb) {
115 for (String token : ln.split("[ ]+")) {
116 if (token.matches("^minlat=.+")) {
117 map.bounds.minlat = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
118 map.nodes.get(2L).lat = map.bounds.minlat;
119 map.nodes.get(3L).lat = map.bounds.minlat;
120 } else if (token.matches("^minlon=.+")) {
121 map.bounds.minlon = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
122 map.nodes.get(1L).lon = map.bounds.minlon;
123 map.nodes.get(2L).lon = map.bounds.minlon;
124 } else if (token.matches("^maxlat=.+")) {
125 map.bounds.maxlat = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
126 map.nodes.get(1L).lat = map.bounds.maxlat;
127 map.nodes.get(4L).lat = map.bounds.maxlat;
128 } else if (token.matches("^maxlon=.+")) {
129 map.bounds.maxlon = Math.toRadians(Double.parseDouble(token.split("[\"\']")[1]));
130 map.nodes.get(3L).lon = map.bounds.maxlon;
131 map.nodes.get(4L).lon = map.bounds.maxlon;
132 }
133 }
134 } else {
135 if ((inNode || inWay || inRel) && (ln.contains("<tag"))) {
136 k = v = "";
137 String[] token = ln.split("k=");
138 k = token[1].split("[\"\']")[1];
139 token = token[1].split("v=");
140 v = token[1].split("[\"\']")[1];
141 if (!k.isEmpty() && !v.isEmpty()) {
142 map.addTag(k, v);
143 }
144 }
145 if (inNode) {
146 if (ln.contains("</node")) {
147 inNode = false;
148 map.tagsDone(id);
149 }
150 } else if (ln.contains("<node")) {
151 for (String token : ln.split("[ ]+")) {
152 if (token.matches("^id=.+")) {
153 id = Long.parseLong(token.split("[\"\']")[1]);
154 } else if (token.matches("^lat=.+")) {
155 lat = Double.parseDouble(token.split("[\"\']")[1]);
156 } else if (token.matches("^lon=.+")) {
157 lon = Double.parseDouble(token.split("[\"\']")[1]);
158 }
159 }
160 map.addNode(id, lat, lon);
161 if (ln.contains("/>")) {
162 map.tagsDone(id);
163 } else {
164 inNode = true;
165 }
166 } else if (inWay) {
167 if (ln.contains("<nd")) {
168 long ref = 0;
169 for (String token : ln.split("[ ]+")) {
170 if (token.matches("^ref=.+")) {
171 ref = Long.parseLong(token.split("[\"\']")[1]);
172 }
173 }
174 try {
175 map.addToEdge(ref);
176 } catch (Exception e) {
177 inWay = false;
178 }
179 }
180 if (ln.contains("</way")) {
181 inWay = false;
182 map.tagsDone(id);
183 }
184 } else if (ln.contains("<way")) {
185 for (String token : ln.split("[ ]+")) {
186 if (token.matches("^id=.+")) {
187 id = Long.parseLong(token.split("[\"\']")[1]);
188 }
189 }
190 map.addEdge(id);
191 if (ln.contains("/>")) {
192 map.tagsDone(0);
193 } else {
194 inWay = true;
195 }
196 } else if (ln.contains("</osm")) {
197 map.mapDone();
198 inOsm = false;
199 break;
200 } else if (inRel) {
201 if (ln.contains("<member")) {
202 String type = "";
203 String role = "";
204 long ref = 0;
205 for (String token : ln.split("[ ]+")) {
206 if (token.matches("^ref=.+")) {
207 ref = Long.parseLong(token.split("[\"\']")[1]);
208 } else if (token.matches("^type=.+")) {
209 type = (token.split("[\"\']")[1]);
210 } else if (token.matches("^role=.+")) {
211 String[] str = token.split("[\"\']");
212 if (str.length > 1) {
213 role = (token.split("[\"\']")[1]);
214 }
215 }
216 }
217 if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))
218 map.addToArea(ref, role.equals("outer"));
219 }
220 if (ln.contains("</relation")) {
221 inRel = false;
222 map.tagsDone(id);
223 }
224 } else if (ln.contains("<relation")) {
225 for (String token : ln.split("[ ]+")) {
226 if (token.matches("^id=.+")) {
227 id = Long.parseLong(token.split("[\"\']")[1]);
228 }
229 }
230 map.addArea(id);
231 if (ln.contains("/>")) {
232 map.tagsDone(id);
233 } else {
234 inRel = true;
235 }
236 }
237 }
238 } else if (ln.contains("<osm")) {
239 inOsm = true;
240 }
241 }
242 return;
243 }
244
245 public static void OSMmeta(S57map map) {
246 map.addEdge(++map.xref);
247 for (long ref = 0; ref <= 4; ref++) {
248 map.addToEdge((ref == 0) ? 4 : ref);
249 }
250 map.addTag("seamark:type", "coverage");
251 map.addTag("seamark:coverage:category", "coverage");
252 map.tagsDone(map.xref);
253 }
254
255}
Note: See TracBrowser for help on using the repository browser.