Changeset 32394 in osm for applications/editors/josm/plugins/seachart/src/s57/S57osm.java
- Timestamp:
- 2016-06-24T03:48:12+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/seachart/src/s57/S57osm.java
r32393 r32394 11 11 12 12 import java.io.BufferedReader; 13 import java.util.*; 14 15 import s57.S57obj.*; 16 import s57.S57att.*; 17 import s57.S57val.*; 13 import java.util.ArrayList; 14 import java.util.HashMap; 15 16 import s57.S57att.Att; 17 import s57.S57obj.Obj; 18 import s57.S57val.CatBUA; 19 import s57.S57val.CatROD; 20 import s57.S57val.Conv; 18 21 19 22 public 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 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 251 255 }
Note:
See TracChangeset
for help on using the changeset viewer.