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

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

use XML DOM to parse OSM files

File size: 10.8 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 }
62
63 public static void OSMtag(ArrayList<KeyVal<?>> osm, String key, String val) {
64 KeyVal<?> kv = OSMtags.get(key + "=" + val);
65 if (kv != null) {
66 if (kv.conv == Conv.E) {
67 ArrayList<Enum<?>> list = new ArrayList<>();
68 list.add((Enum<?>) kv.val);
69 osm.add(new KeyVal<>(kv.obj, kv.att, kv.conv, list));
70 } else {
71 osm.add(kv);
72 }
73 }
74 KeyVal<?> kvl = null;
75 KeyVal<?> kvd = null;
76 boolean rc = false;
77 boolean rcl = false;
78 for (KeyVal<?> kvx : osm) {
79 if (kvx.obj == Obj.LAKARE) {
80 kvl = kvx;
81 } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS)) {
82 rc = true;
83 }
84 if (kvx.obj == Obj.DEPARE) {
85 kvd = kvx;
86 } else if ((kvx.obj == Obj.RIVERS) || (kvx.obj == Obj.CANALS) || (kvx.obj == Obj.LAKARE)) {
87 rcl = true;
88 }
89 }
90 if (rc && (kvl != null)) {
91 osm.remove(kvl);
92 }
93 if (rcl && (kvd != null)) {
94 osm.remove(kvd);
95 }
96 return;
97 }
98
99 public static void OSMmap(File in, S57map map, boolean bb) throws Exception {
100 String k = "";
101 String v = "";
102
103 double lat = 0;
104 double lon = 0;
105 long id = 0;
106 long ref = 0;
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 = nList.item(0).getAttributes();
125 map.bounds.minlat = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("minlat").getNodeValue()));
126 map.nodes.get(2L).lat = map.bounds.minlat;
127 map.nodes.get(3L).lat = map.bounds.minlat;
128 map.bounds.minlon = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("minlon").getNodeValue()));
129 map.nodes.get(1L).lon = map.bounds.minlon;
130 map.nodes.get(2L).lon = map.bounds.minlon;
131 map.bounds.maxlat = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("maxlat").getNodeValue()));
132 map.nodes.get(1L).lat = map.bounds.maxlat;
133 map.nodes.get(4L).lat = map.bounds.maxlat;
134 map.bounds.maxlon = Math.toRadians(Double.parseDouble(nnmap.getNamedItem("maxlon").getNodeValue()));
135 map.nodes.get(3L).lon = map.bounds.maxlon;
136 map.nodes.get(4L).lon = map.bounds.maxlon;
137
138 nList = doc.getElementsByTagName("node");
139 int nLen = nList.getLength();
140 for (int i = 0; i < nLen; i++) {
141 Node nNode = nList.item(i);
142 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
143 nnmap = nNode.getAttributes();
144 id = Long.parseLong(nnmap.getNamedItem("id").getNodeValue());
145 lat = Double.parseDouble(nnmap.getNamedItem("lat").getNodeValue());
146 lon = Double.parseDouble(nnmap.getNamedItem("lon").getNodeValue());
147 map.addNode(id, lat, lon);
148 NodeList tList = ((Element)nNode).getElementsByTagName("tag");
149 for (int j = 0; j < tList.getLength(); j++) {
150 NamedNodeMap ntmap = tList.item(j).getAttributes();
151 k = ntmap.getNamedItem("k").getNodeValue();
152 v = ntmap.getNamedItem("v").getNodeValue();
153 if (!k.isEmpty() && !v.isEmpty()) {
154 map.addTag(k, v);
155 }
156 }
157 map.tagsDone(id);
158 }
159 }
160
161 nList = doc.getElementsByTagName("way");
162 nLen = nList.getLength();
163 for (int i = 0; i < nLen; i++) {
164 Node nNode = nList.item(i);
165 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
166 nnmap = nNode.getAttributes();
167 id = Long.parseLong(nnmap.getNamedItem("id").getNodeValue());
168 map.addEdge(id);
169 NodeList rList = ((Element)nNode).getElementsByTagName("nd");
170 for (int j = 0; j < rList.getLength(); j++) {
171 NamedNodeMap nrmap = rList.item(j).getAttributes();
172 ref = Long.parseLong(nrmap.getNamedItem("ref").getNodeValue());
173 try {
174 map.addToEdge(ref);
175 } catch (Exception e) {
176 System.err.println("Unknown node in way");
177 System.exit(-1);
178 }
179 }
180 NodeList tList = ((Element)nNode).getElementsByTagName("tag");
181 for (int j = 0; j < tList.getLength(); j++) {
182 NamedNodeMap ntmap = tList.item(j).getAttributes();
183 k = ntmap.getNamedItem("k").getNodeValue();
184 v = ntmap.getNamedItem("v").getNodeValue();
185 if (!k.isEmpty() && !v.isEmpty()) {
186 map.addTag(k, v);
187 }
188 }
189 map.tagsDone(id);
190 }
191 }
192
193 nList = doc.getElementsByTagName("relation");
194 nLen = nList.getLength();
195 for (int i = 0; i < nLen; i++) {
196 Node nNode = nList.item(i);
197 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
198 nnmap = nNode.getAttributes();
199 id = Long.parseLong(nnmap.getNamedItem("id").getNodeValue());
200 map.addArea(id);
201 NodeList rList = ((Element)nNode).getElementsByTagName("member");
202 for (int j = 0; j < rList.getLength(); j++) {
203 NamedNodeMap nrmap = rList.item(j).getAttributes();
204 type = nrmap.getNamedItem("type").getNodeValue();
205 ref = Long.parseLong(nrmap.getNamedItem("ref").getNodeValue());
206 role = nrmap.getNamedItem("role").getNodeValue();
207 if ((role.equals("outer") || role.equals("inner")) && type.equals("way"))
208 map.addToArea(ref, role.equals("outer"));
209 }
210 NodeList tList = ((Element)nNode).getElementsByTagName("tag");
211 for (int j = 0; j < tList.getLength(); j++) {
212 NamedNodeMap ntmap = tList.item(j).getAttributes();
213 k = ntmap.getNamedItem("k").getNodeValue();
214 v = ntmap.getNamedItem("v").getNodeValue();
215 if (!k.isEmpty() && !v.isEmpty()) {
216 map.addTag(k, v);
217 }
218 }
219 map.tagsDone(id);
220 }
221 }
222
223 map.mapDone();
224 return;
225 }
226
227 public static void OSMmeta(S57map map) {
228 map.addEdge(++map.xref);
229 for (long ref = 0; ref <= 4; ref++) {
230 map.addToEdge((ref == 0) ? 4 : ref);
231 }
232 map.addTag("seamark:type", "coverage");
233 map.addTag("seamark:coverage:category", "coverage");
234 map.tagsDone(map.xref);
235 }
236
237}
Note: See TracBrowser for help on using the repository browser.