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

Last change on this file since 35404 was 35404, checked in by malcolmh, 5 years ago

more map features

File size: 10.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package s57;
3
4import java.io.File;
5import java.util.ArrayList;
6import java.util.HashMap;
7
8import javax.xml.parsers.DocumentBuilderFactory;
9import javax.xml.parsers.DocumentBuilder;
10import org.w3c.dom.Document;
11import org.w3c.dom.NodeList;
12import org.w3c.dom.Node;
13import org.w3c.dom.Element;
14import org.w3c.dom.NamedNodeMap;
15
16import s57.S57att.Att;
17import s57.S57map.Snode;
18import s57.S57obj.Obj;
19import s57.S57val.CatROD;
20import s57.S57val.Conv;
21
22/**
23 * @author Malcolm Herring
24 */
25public final class S57osm { // OSM to S57 Object/Attribute and Object/Primitive conversions
26 private S57osm() {
27 // Hide default constructor for utilities classes
28 }
29
30 // CHECKSTYLE.OFF: LineLength
31
32 static class KeyVal<V> {
33 Obj obj;
34 Att att;
35 Conv conv;
36 V val;
37 KeyVal(Obj o, Att a, Conv c, V v) {
38 obj = o;
39 att = a;
40 conv = c;
41 val = v;
42 }
43 }
44
45 private static final HashMap<String, KeyVal<?>> OSMtags = new HashMap<>();
46 static {
47 OSMtags.put("natural=coastline", new KeyVal<>(Obj.COALNE, Att.UNKATT, null, null)); OSMtags.put("natural=water", new KeyVal<>(Obj.LAKARE, Att.UNKATT, null, null));
48 OSMtags.put("water=river", new KeyVal<>(Obj.RIVERS, Att.UNKATT, null, null)); OSMtags.put("water=canal", new KeyVal<>(Obj.CANALS, Att.UNKATT, null, null));
49 OSMtags.put("waterway=riverbank", new KeyVal<>(Obj.RIVERS, Att.UNKATT, null, null)); OSMtags.put("waterway=dock", new KeyVal<>(Obj.HRBBSN, Att.UNKATT, null, null));
50 OSMtags.put("waterway=lock", new KeyVal<>(Obj.HRBBSN, Att.UNKATT, null, null)); OSMtags.put("landuse=basin", new KeyVal<>(Obj.LAKARE, Att.UNKATT, null, null));
51 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));
52 OSMtags.put("natural=mud", new KeyVal<>(Obj.DEPARE, Att.UNKATT, null, null)); OSMtags.put("natural=sand", new KeyVal<>(Obj.DEPARE, Att.UNKATT, null, null));
53 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));
54 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));
55 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));
56 OSMtags.put("highway=unclassified", new KeyVal<>(Obj.ROADWY, Att.UNKATT, null, null)); OSMtags.put("railway=rail", new KeyVal<>(Obj.RAILWY, Att.UNKATT, null, null));
57 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));
58 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));
59 OSMtags.put("landuse=industrial", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null)); OSMtags.put("landuse=commercial", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null));
60 OSMtags.put("landuse=retail", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null)); OSMtags.put("landuse=residential", new KeyVal<>(Obj.BUAARE, Att.UNKATT, null, null));
61 OSMtags.put("boundary_type=territorial_waters", new KeyVal<>(Obj.TESARE, Att.UNKATT, null, null));
62 }
63
64 public static void OSMtag(ArrayList<KeyVal<?>> osm, String key, String val) {
65 KeyVal<?> kv = OSMtags.get(key + "=" + val);
66 if (kv != null) {
67 if (kv.conv == Conv.E) {
68 ArrayList<Enum<?>> list = new ArrayList<>();
69 list.add((Enum<?>) kv.val);
70 osm.add(new KeyVal<>(kv.obj, kv.att, kv.conv, list));
71 } else {
72 osm.add(kv);
73 }
74 }
75 KeyVal<?> kvl = null;
76 KeyVal<?> kvd = null;
77 boolean rc = false;
78 boolean rcl = false;
79 for (KeyVal<?> kvx : osm) {
80 if (kvx.obj == Obj.LAKARE) {
81 kvl = kvx;
82 } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS)) {
83 rc = true;
84 }
85 if (kvx.obj == Obj.DEPARE) {
86 kvd = kvx;
87 } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS) || (kvx.obj == Obj.LAKARE)) {
88 rcl = true;
89 }
90 }
91 if (rc && (kvl != null)) {
92 osm.remove(kvl);
93 }
94 if (rcl && (kvd != null)) {
95 osm.remove(kvd);
96 }
97 return;
98 }
99
100 public static void OSMmap(File in, S57map map, boolean bb) throws Exception {
101 double lat = 0;
102 double lon = 0;
103 long id = 0;
104 long ref = 0;
105 String k = "";
106 String v = "";
107 String type = "";
108 String role = "";
109
110 map.nodes.put(1L, new Snode());
111 map.nodes.put(2L, new Snode());
112 map.nodes.put(3L, new Snode());
113 map.nodes.put(4L, new Snode());
114
115 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
116 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
117 Document doc = dBuilder.parse(in);
118 doc.getDocumentElement().normalize();
119 if (!doc.getDocumentElement().getNodeName().equals("osm")) {
120 System.err.println("OSM file format error");
121 System.exit(-1);
122 }
123 NodeList nList = doc.getElementsByTagName("bounds");
124 NamedNodeMap nnmap;
125 if (nList.getLength() != 0) {
126 nnmap = nList.item(0).getAttributes();
127 map.bounds.minlat = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("minlat").getNodeValue()));
128 map.nodes.get(2L).lat = map.bounds.minlat;
129 map.nodes.get(3L).lat = map.bounds.minlat;
130 map.bounds.minlon = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("minlon").getNodeValue()));
131 map.nodes.get(1L).lon = map.bounds.minlon;
132 map.nodes.get(2L).lon = map.bounds.minlon;
133 map.bounds.maxlat = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("maxlat").getNodeValue()));
134 map.nodes.get(1L).lat = map.bounds.maxlat;
135 map.nodes.get(4L).lat = map.bounds.maxlat;
136 map.bounds.maxlon = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("maxlon").getNodeValue()));
137 map.nodes.get(3L).lon = map.bounds.maxlon;
138 map.nodes.get(4L).lon = map.bounds.maxlon;
139 }
140
141 nList = doc.getElementsByTagName("node");
142 int nLen = nList.getLength();
143 for (int i = 0; i < nLen; i++) {
144 Node nNode = nList.item(i);
145 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
146 nnmap = nNode.getAttributes();
147 id = Long.parseLong(nnmap.getNamedItem("id").getNodeValue());
148 lat = Double.parseDouble(nnmap.getNamedItem("lat").getNodeValue());
149 lon = Double.parseDouble(nnmap.getNamedItem("lon").getNodeValue());
150 map.addNode(id, lat, lon);
151 NodeList tList = ((Element)nNode).getElementsByTagName("tag");
152 for (int j = 0; j < tList.getLength(); j++) {
153 NamedNodeMap ntmap = tList.item(j).getAttributes();
154 k = ntmap.getNamedItem("k").getNodeValue();
155 v = ntmap.getNamedItem("v").getNodeValue();
156 if (!k.isEmpty() && !v.isEmpty()) {
157 map.addTag(k, v);
158 }
159 }
160 map.tagsDone(id);
161 }
162 }
163
164 nList = doc.getElementsByTagName("way");
165 nLen = nList.getLength();
166 for (int i = 0; i < nLen; i++) {
167 Node nNode = nList.item(i);
168 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
169 nnmap = nNode.getAttributes();
170 id = Long.parseLong(nnmap.getNamedItem("id").getNodeValue());
171 map.addEdge(id);
172 NodeList rList = ((Element)nNode).getElementsByTagName("nd");
173 for (int j = 0; j < rList.getLength(); j++) {
174 NamedNodeMap nrmap = rList.item(j).getAttributes();
175 ref = Long.parseLong(nrmap.getNamedItem("ref").getNodeValue());
176 try {
177 map.addToEdge(ref);
178 } catch (Exception e) {
179 System.err.println("Unknown node in way");
180 System.exit(-1);
181 }
182 }
183 NodeList tList = ((Element)nNode).getElementsByTagName("tag");
184 for (int j = 0; j < tList.getLength(); j++) {
185 NamedNodeMap ntmap = tList.item(j).getAttributes();
186 k = ntmap.getNamedItem("k").getNodeValue();
187 v = ntmap.getNamedItem("v").getNodeValue();
188 if (!k.isEmpty() && !v.isEmpty()) {
189 map.addTag(k, v);
190 }
191 }
192 map.tagsDone(id);
193 }
194 }
195
196 nList = doc.getElementsByTagName("relation");
197 nLen = nList.getLength();
198 for (int i = 0; i < nLen; i++) {
199 Node nNode = nList.item(i);
200 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
201 nnmap = nNode.getAttributes();
202 id = Long.parseLong(nnmap.getNamedItem("id").getNodeValue());
203 map.addArea(id);
204 NodeList rList = ((Element)nNode).getElementsByTagName("member");
205 for (int j = 0; j < rList.getLength(); j++) {
206 NamedNodeMap nrmap = rList.item(j).getAttributes();
207 type = nrmap.getNamedItem("type").getNodeValue();
208 ref = Long.parseLong(nrmap.getNamedItem("ref").getNodeValue());
209 role = nrmap.getNamedItem("role").getNodeValue();
210 if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))
211 map.addToArea(ref, role.equals("outer"));
212 }
213 NodeList tList = ((Element)nNode).getElementsByTagName("tag");
214 for (int j = 0; j < tList.getLength(); j++) {
215 NamedNodeMap ntmap = tList.item(j).getAttributes();
216 k = ntmap.getNamedItem("k").getNodeValue();
217 v = ntmap.getNamedItem("v").getNodeValue();
218 if (!k.isEmpty() && !v.isEmpty()) {
219 map.addTag(k, v);
220 }
221 }
222 map.tagsDone(id);
223 }
224 }
225
226 map.mapDone();
227 return;
228 }
229
230 public static void OSMmeta(S57map map) {
231 map.addEdge(++map.xref);
232 for (long ref = 0; ref <= 4; ref++) {
233 map.addToEdge((ref == 0) ? 4 : ref);
234 }
235 map.addTag("seamark:type", "coverage");
236 map.addTag("seamark:coverage:category", "coverage");
237 map.tagsDone(map.xref);
238 }
239
240}
Note: See TracBrowser for help on using the repository browser.