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

Last change on this file since 36344 was 36169, checked in by taylor.smock, 16 months ago

Fix #23157: Change S57osm.OSMmap to accept either a file or inputstream (patch by oobayly)

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