source: osm/applications/editors/josm/plugins/smed2/js57toosm/src/js57toosm/Js57toosm.java@ 30314

Last change on this file since 30314 was 30314, checked in by malcolmh, 11 years ago

save

File size: 6.5 KB
Line 
1/* Copyright 2014 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 js57toosm;
11
12import java.io.*;
13import java.util.*;
14
15import s57.S57obj;
16import s57.S57obj.*;
17import s57.S57att;
18import s57.S57att.*;
19import s57.S57val;
20import s57.S57val.*;
21import s57.S57map;
22import s57.S57map.*;
23import s57.S57dec;
24
25public class Js57toosm {
26
27 static FileInputStream in;
28 static PrintStream out;
29 static S57map map;
30
31 public static void main(String[] args) throws IOException {
32
33 ArrayList<Long> done = new ArrayList<Long>();
34
35 if (args.length < 1) {
36 System.err.println("Usage: java -jar js57toosm.jar S57_filename [types_filename]");
37 System.exit(-1);
38 }
39 in = new FileInputStream(args[0]);
40 out = System.out;
41 ArrayList<Obj> types = new ArrayList<Obj>();
42 if (args.length == 2) {
43 Scanner tin = new Scanner(new FileInputStream(args[1]));
44 while (tin.hasNext()) {
45 types.add(S57obj.enumType(tin.next()));
46 }
47 tin.close();
48 }
49
50 map = new S57map();
51 MapBounds bounds = S57dec.decodeFile(in, types, map);
52
53 out.format("<?xml version='1.0' encoding='UTF-8'?>%n");
54 out.format("<osm version='0.6' upload='false' generator='js57toosm'>%n");
55 out.format("<bounds minlat='%.8f' minlon='%.8f' maxlat='%.8f' maxlon='%.8f'/>%n", bounds.minlat, bounds.minlon, bounds.maxlat, bounds.maxlon);
56
57 for (long id : map.index.keySet()) {
58 Feature feature = map.index.get(id);
59 String type = S57obj.stringType(feature.type);
60 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {
61 if (feature.reln == Rflag.MASTER) {
62 if (feature.geom.prim == Pflag.POINT) {
63 for (Prim prim : feature.geom.elems) {
64 long ref = prim.id;
65 Snode node;
66 while ((node = map.nodes.get(ref)) != null) {
67 if (!done.contains(ref)) {
68 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon));
69 out.format(" <tag k='seamark:type' v=\"%s\"/>%n", type);
70 if ((feature.type == Obj.SOUNDG) && (node.flg == S57map.Nflag.DPTH))
71 out.format(" <tag k='seamark:sounding:depth' v='%.1f'/>%n", ((Dnode) node).val);
72 writeAtts(feature, type);
73 out.format(" </node>%n");
74 done.add(ref);
75 }
76 ref++;
77 }
78 }
79 }
80 }
81 }
82 }
83 for (long id : map.index.keySet()) {
84 Feature feature = map.index.get(id);
85 String type = S57obj.stringType(feature.type);
86 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {
87 if (feature.reln == Rflag.MASTER) {
88 if ((feature.geom.prim == Pflag.LINE) || ((feature.geom.prim == Pflag.AREA) && (feature.geom.outers == 1) && (feature.geom.inners == 0))) {
89 GeomIterator git = map.new GeomIterator(feature.geom);
90 while (git.hasComp()) {
91 git.nextComp();
92 while (git.hasEdge()) {
93 git.nextEdge();
94 while (git.hasNode()) {
95 long ref = git.nextRef();
96 Snode node = map.nodes.get(ref);
97 if (!done.contains(ref)) {
98 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'/>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon));
99 done.add(ref);
100 }
101 }
102 }
103 }
104 git = map.new GeomIterator(feature.geom);
105 while (git.hasComp()) {
106 long edge = git.nextComp();
107 out.format(" <way id='%d' version='1'>%n", -edge);
108 while (git.hasEdge()) {
109 git.nextEdge();
110 while (git.hasNode()) {
111 long ref = git.nextRef();
112 out.format(" <nd ref='%d'/>%n", -ref);
113 }
114 }
115 out.format(" <tag k='seamark:type' v=\"%s\"/>%n", type);
116 writeAtts(feature, type);
117 out.format(" </way>%n");
118 }
119 } else if (feature.geom.prim == Pflag.AREA) {
120 GeomIterator git = map.new GeomIterator(feature.geom);
121 while (git.hasComp()) {
122 git.nextComp();
123 while (git.hasEdge()) {
124 git.nextEdge();
125 while (git.hasNode()) {
126 long ref = git.nextRef();
127 Snode node = map.nodes.get(ref);
128 if (!done.contains(ref)) {
129 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'/>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon));
130 done.add(ref);
131 }
132 }
133 }
134 }
135 git = map.new GeomIterator(feature.geom);
136 while (git.hasComp()) {
137 long ref = git.nextComp();
138 out.format(" <way id='%d' version='1'>%n", -ref);
139 while (git.hasEdge()) {
140 git.nextEdge();
141 while (git.hasNode()) {
142 ref = git.nextRef();
143 out.format(" <nd ref='%d'/>%n", -ref);
144 }
145 }
146 out.format(" </way>%n");
147 }
148 out.format(" <relation id='%d' version='1'>%n", -map.ref++);
149 out.format(" <tag k='type' v='multipolygon'/>%n");
150 git = map.new GeomIterator(feature.geom);
151 int outers = feature.geom.outers;
152 while (git.hasComp()) {
153 long ref = git.nextComp();
154 if (outers-- > 0) {
155 out.format(" <member type='way' ref='%d' role='outer'/>%n", -ref);
156 } else {
157 out.format(" <member type='way' ref='%d' role='inner'/>%n", -ref);
158 }
159 }
160 out.format(" <tag k='seamark:type' v=\"%s\"/>%n", type);
161 writeAtts(feature, type);
162 out.format(" </relation>%n");
163 }
164 }
165 }
166 }
167 out.println("</osm>\n");
168 }
169
170 static void writeAtts(Feature feature, String type) {
171 for (Map.Entry<Att, AttVal<?>> item : feature.atts.entrySet()) {
172 String attstr = S57att.stringAttribute(item.getKey());
173 String valstr = S57val.stringValue(item.getValue());
174 if (!attstr.isEmpty() && !valstr.isEmpty())
175 out.format(" <tag k='seamark:%s:%s' v=\"%s\"/>%n", type, attstr, valstr);
176 }
177 for (Obj obj : feature.objs.keySet()) {
178 ObjTab tab = feature.objs.get(obj);
179 for (int ix : tab.keySet()) {
180 type = S57obj.stringType(obj);
181 AttMap atts = tab.get(ix);
182 for (Map.Entry<Att, AttVal<?>> item : atts.entrySet()) {
183 String attstr = S57att.stringAttribute(item.getKey());
184 String valstr = S57val.stringValue(item.getValue());
185 if (!attstr.isEmpty() && !valstr.isEmpty()) {
186 if ((ix == 0) && (tab.size() == 1)) {
187 out.format(" <tag k='seamark:%s:%s' v=\"%s\"/>%n", type, attstr, valstr);
188 } else {
189 out.format(" <tag k='seamark:%s:%d:%s' v=\"%s\"/>%n", type, ix + 1, attstr, valstr);
190 }
191 }
192 }
193 }
194 }
195 }
196
197}
Note: See TracBrowser for help on using the repository browser.