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

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

use diamond operator

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