Changeset 32906 in osm for applications/editors/josm/plugins/seachart/js57toosm/src
- Timestamp:
- 2016-09-03T16:18:15+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/seachart/js57toosm/src/js57toosm/Js57toosm.java
r32393 r32906 26 26 27 27 public class Js57toosm { 28 29 static FileInputStream in;30 static PrintStream out;31 static S57map map;32 static final ArrayList<Att> typatts = new ArrayList<>(); static {33 typatts.add(Att.OBJNAM); typatts.add(Att.NOBJNM); typatts.add(Att.STATUS); typatts.add(Att.INFORM); typatts.add(Att.NINFOM);34 typatts.add(Att.PEREND); typatts.add(Att.PERSTA); typatts.add(Att.CONDTN); typatts.add(Att.CONRAD); typatts.add(Att.CONVIS);35 }36 37 public static void main(String[] args) throws IOException {38 39 ArrayList<Long> done = new ArrayList<>();40 41 if (args.length < 3) {42 System.err.println("Usage: java -jar js57toosm.jar S57_filename types_filename OSM_filename");43 System.exit(-1);44 }45 try {46 in = new FileInputStream(args[0]);47 } catch (IOException e) {48 System.err.println("Input file: " + e.getMessage());49 System.exit(-1);50 }51 try {52 out = new PrintStream(args[2]);53 } catch (IOException e) {54 System.err.println("Output file: " + e.getMessage());55 in.close();56 System.exit(-1);57 }58 ArrayList<Obj> types = new ArrayList<>();59 try {60 Scanner tin = new Scanner(new FileInputStream(args[1]));61 while (tin.hasNext()) {62 Obj type = S57obj.enumType(tin.next());63 if (type != Obj.UNKOBJ)64 types.add(type);65 }66 tin.close();67 } catch (IOException e) {68 System.err.println("Types file: " + e.getMessage());69 in.close();70 out.close();71 System.exit(-1);72 }73 74 map = new S57map(true);75 S57dec.decodeChart(in, map);76 77 out.format("<?xml version='1.0' encoding='UTF-8'?>%n");78 out.format("<osm version='0.6' upload='false' generator='js57toosm'>%n");79 out.format("<bounds minlat='%.8f' minlon='%.8f' maxlat='%.8f' maxlon='%.8f'/>%n",80 Math.toDegrees(map.bounds.minlat), Math.toDegrees(map.bounds.minlon), Math.toDegrees(map.bounds.maxlat), Math.toDegrees(map.bounds.maxlon));81 82 for (long id : map.index.keySet()) {83 Feature feature = map.index.get(id);84 String type = S57obj.stringType(feature.type);85 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {86 if (feature.reln == Rflag.MASTER) {87 if (feature.geom.prim == Pflag.POINT) {88 for (Prim prim : feature.geom.elems) {89 long ref = prim.id;90 Snode node;91 while ((node = map.nodes.get(ref)) != null) {92 if (!done.contains(ref)) {93 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon));94 out.format(" <tag k='seamark:type' v=\"%s\"/>%n", type);95 if ((feature.type == Obj.SOUNDG) && (node.flg == S57map.Nflag.DPTH))96 out.format(" <tag k='seamark:sounding:depth' v='%.1f'/>%n", ((Snode) node).val);97 writeAtts(feature);98 out.format(" </node>%n");99 done.add(ref);100 }101 ref++;102 }103 }104 }105 }106 }107 }108 for (long id : map.index.keySet()) {109 Feature feature = map.index.get(id);110 String type = S57obj.stringType(feature.type);111 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {112 if (feature.reln == Rflag.MASTER) {113 if ((feature.geom.prim == Pflag.LINE) || ((feature.geom.prim == Pflag.AREA) && (feature.geom.outers == 1) && (feature.geom.inners == 0))) {114 GeomIterator git = map.new GeomIterator(feature.geom);115 while (git.hasComp()) {116 git.nextComp();117 while (git.hasEdge()) {118 git.nextEdge();119 while (git.hasNode()) {120 long ref = git.nextRef();121 Snode node = map.nodes.get(ref);122 if (!done.contains(ref)) {123 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'/>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon));124 done.add(ref);125 }126 }127 }128 }129 git = map.new GeomIterator(feature.geom);130 while (git.hasComp()) {131 long edge = git.nextComp();132 out.format(" <way id='%d' version='1'>%n", -edge);133 while (git.hasEdge()) {134 git.nextEdge();135 while (git.hasNode()) {136 long ref = git.nextRef();137 out.format(" <nd ref='%d'/>%n", -ref);138 }139 }140 out.format(" <tag k='seamark:type' v='%s'/>%n", type);141 writeAtts(feature);142 out.format(" </way>%n");143 }144 } else if (feature.geom.prim == Pflag.AREA) {145 GeomIterator git = map.new GeomIterator(feature.geom);146 while (git.hasComp()) {147 git.nextComp();148 while (git.hasEdge()) {149 git.nextEdge();150 while (git.hasNode()) {151 long ref = git.nextRef();152 Snode node = map.nodes.get(ref);153 if (!done.contains(ref)) {154 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'/>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon));155 done.add(ref);156 }157 }158 }159 }160 git = map.new GeomIterator(feature.geom);161 while (git.hasComp()) {162 long ref = git.nextComp();163 out.format(" <way id='%d' version='1'>%n", -ref);164 while (git.hasEdge()) {165 git.nextEdge();166 while (git.hasNode()) {167 ref = git.nextRef();168 out.format(" <nd ref='%d'/>%n", -ref);169 }170 }171 out.format(" </way>%n");172 }173 out.format(" <relation id='%d' version='1'>%n", -map.xref++);174 out.format(" <tag k='type' v='multipolygon'/>%n");175 git = map.new GeomIterator(feature.geom);176 int outers = feature.geom.outers;177 while (git.hasComp()) {178 long ref = git.nextComp();179 if (outers-- > 0) {180 out.format(" <member type='way' ref='%d' role='outer'/>%n", -ref);181 } else {182 out.format(" <member type='way' ref='%d' role='inner'/>%n", -ref);183 }184 }185 out.format(" <tag k='seamark:type' v='%s'/>%n", type);186 writeAtts(feature);187 out.format(" </relation>%n");188 }189 }190 }191 }192 out.println("</osm>\n");193 out.close();194 System.err.println("Finished");195 }196 197 static void writeAtts(Feature feature) {198 for (Map.Entry<Att, AttVal<?>> item : feature.atts.entrySet()) {199 String attstr = S57att.stringAttribute(item.getKey());200 String valstr = S57val.stringValue(item.getValue(), item.getKey());201 if (!attstr.isEmpty() && !valstr.isEmpty()) {202 if (typatts.contains(item.getKey())) {203 out.format(" <tag k='seamark:%s' v='%s'/>%n", attstr, StringEscapeUtils.escapeXml10(valstr));204 } else {205 out.format(" <tag k='seamark:%s:%s' v='%s'/>%n", S57obj.stringType(feature.type), attstr, StringEscapeUtils.escapeXml10(valstr));206 }207 }208 }209 for (Obj obj : feature.objs.keySet()) {210 ObjTab tab = feature.objs.get(obj);211 for (int ix : tab.keySet()) {212 AttMap atts = tab.get(ix);213 for (Map.Entry<Att, AttVal<?>> item : atts.entrySet()) {214 String attstr = S57att.stringAttribute(item.getKey());215 String valstr = S57val.stringValue(item.getValue(), item.getKey());216 if (!attstr.isEmpty() && !valstr.isEmpty()) {217 if ((ix == 0) && (tab.size() == 1)) {218 out.format(" <tag k='seamark:%s:%s' v='%s'/>%n", S57obj.stringType(obj), attstr, StringEscapeUtils.escapeXml10(valstr));219 } else {220 out.format(" <tag k='seamark:%s:%d:%s' v='%s'/>%n", S57obj.stringType(obj), ix + 1, attstr, StringEscapeUtils.escapeXml10(valstr));221 }222 }223 }224 }225 }226 }28 29 static FileInputStream in; 30 static PrintStream out; 31 static S57map map; 32 static final ArrayList<Att> typatts = new ArrayList<>(); static { 33 typatts.add(Att.OBJNAM); typatts.add(Att.NOBJNM); typatts.add(Att.STATUS); typatts.add(Att.INFORM); typatts.add(Att.NINFOM); 34 typatts.add(Att.PEREND); typatts.add(Att.PERSTA); typatts.add(Att.CONDTN); typatts.add(Att.CONRAD); typatts.add(Att.CONVIS); 35 } 36 37 public static void main(String[] args) throws IOException { 38 39 ArrayList<Long> done = new ArrayList<>(); 40 41 if (args.length < 3) { 42 System.err.println("Usage: java -jar js57toosm.jar S57_filename types_filename OSM_filename"); 43 System.exit(-1); 44 } 45 try { 46 in = new FileInputStream(args[0]); 47 } catch (IOException e) { 48 System.err.println("Input file: " + e.getMessage()); 49 System.exit(-1); 50 } 51 try { 52 out = new PrintStream(args[2]); 53 } catch (IOException e) { 54 System.err.println("Output file: " + e.getMessage()); 55 in.close(); 56 System.exit(-1); 57 } 58 ArrayList<Obj> types = new ArrayList<>(); 59 try { 60 Scanner tin = new Scanner(new FileInputStream(args[1])); 61 while (tin.hasNext()) { 62 Obj type = S57obj.enumType(tin.next()); 63 if (type != Obj.UNKOBJ) 64 types.add(type); 65 } 66 tin.close(); 67 } catch (IOException e) { 68 System.err.println("Types file: " + e.getMessage()); 69 in.close(); 70 out.close(); 71 System.exit(-1); 72 } 73 74 map = new S57map(true); 75 S57dec.decodeChart(in, map); 76 77 out.format("<?xml version='1.0' encoding='UTF-8'?>%n"); 78 out.format("<osm version='0.6' upload='false' generator='js57toosm'>%n"); 79 out.format("<bounds minlat='%.8f' minlon='%.8f' maxlat='%.8f' maxlon='%.8f'/>%n", 80 Math.toDegrees(map.bounds.minlat), Math.toDegrees(map.bounds.minlon), Math.toDegrees(map.bounds.maxlat), Math.toDegrees(map.bounds.maxlon)); 81 82 for (long id : map.index.keySet()) { 83 Feature feature = map.index.get(id); 84 String type = S57obj.stringType(feature.type); 85 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) { 86 if (feature.reln == Rflag.MASTER) { 87 if (feature.geom.prim == Pflag.POINT) { 88 for (Prim prim : feature.geom.elems) { 89 long ref = prim.id; 90 Snode node; 91 while ((node = map.nodes.get(ref)) != null) { 92 if (!done.contains(ref)) { 93 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon)); 94 out.format(" <tag k='seamark:type' v=\"%s\"/>%n", type); 95 if ((feature.type == Obj.SOUNDG) && (node.flg == S57map.Nflag.DPTH)) 96 out.format(" <tag k='seamark:sounding:depth' v='%.1f'/>%n", ((Snode) node).val); 97 writeAtts(feature); 98 out.format(" </node>%n"); 99 done.add(ref); 100 } 101 ref++; 102 } 103 } 104 } 105 } 106 } 107 } 108 for (long id : map.index.keySet()) { 109 Feature feature = map.index.get(id); 110 String type = S57obj.stringType(feature.type); 111 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) { 112 if (feature.reln == Rflag.MASTER) { 113 if ((feature.geom.prim == Pflag.LINE) || ((feature.geom.prim == Pflag.AREA) && (feature.geom.outers == 1) && (feature.geom.inners == 0))) { 114 GeomIterator git = map.new GeomIterator(feature.geom); 115 while (git.hasComp()) { 116 git.nextComp(); 117 while (git.hasEdge()) { 118 git.nextEdge(); 119 while (git.hasNode()) { 120 long ref = git.nextRef(); 121 Snode node = map.nodes.get(ref); 122 if (!done.contains(ref)) { 123 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'/>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon)); 124 done.add(ref); 125 } 126 } 127 } 128 } 129 git = map.new GeomIterator(feature.geom); 130 while (git.hasComp()) { 131 long edge = git.nextComp(); 132 out.format(" <way id='%d' version='1'>%n", -edge); 133 while (git.hasEdge()) { 134 git.nextEdge(); 135 while (git.hasNode()) { 136 long ref = git.nextRef(); 137 out.format(" <nd ref='%d'/>%n", -ref); 138 } 139 } 140 out.format(" <tag k='seamark:type' v='%s'/>%n", type); 141 writeAtts(feature); 142 out.format(" </way>%n"); 143 } 144 } else if (feature.geom.prim == Pflag.AREA) { 145 GeomIterator git = map.new GeomIterator(feature.geom); 146 while (git.hasComp()) { 147 git.nextComp(); 148 while (git.hasEdge()) { 149 git.nextEdge(); 150 while (git.hasNode()) { 151 long ref = git.nextRef(); 152 Snode node = map.nodes.get(ref); 153 if (!done.contains(ref)) { 154 out.format(" <node id='%d' lat='%.8f' lon='%.8f' version='1'/>%n", -ref, Math.toDegrees(node.lat), Math.toDegrees(node.lon)); 155 done.add(ref); 156 } 157 } 158 } 159 } 160 git = map.new GeomIterator(feature.geom); 161 while (git.hasComp()) { 162 long ref = git.nextComp(); 163 out.format(" <way id='%d' version='1'>%n", -ref); 164 while (git.hasEdge()) { 165 git.nextEdge(); 166 while (git.hasNode()) { 167 ref = git.nextRef(); 168 out.format(" <nd ref='%d'/>%n", -ref); 169 } 170 } 171 out.format(" </way>%n"); 172 } 173 out.format(" <relation id='%d' version='1'>%n", -map.xref++); 174 out.format(" <tag k='type' v='multipolygon'/>%n"); 175 git = map.new GeomIterator(feature.geom); 176 int outers = feature.geom.outers; 177 while (git.hasComp()) { 178 long ref = git.nextComp(); 179 if (outers-- > 0) { 180 out.format(" <member type='way' ref='%d' role='outer'/>%n", -ref); 181 } else { 182 out.format(" <member type='way' ref='%d' role='inner'/>%n", -ref); 183 } 184 } 185 out.format(" <tag k='seamark:type' v='%s'/>%n", type); 186 writeAtts(feature); 187 out.format(" </relation>%n"); 188 } 189 } 190 } 191 } 192 out.println("</osm>\n"); 193 out.close(); 194 System.err.println("Finished"); 195 } 196 197 static void writeAtts(Feature feature) { 198 for (Map.Entry<Att, AttVal<?>> item : feature.atts.entrySet()) { 199 String attstr = S57att.stringAttribute(item.getKey()); 200 String valstr = S57val.stringValue(item.getValue(), item.getKey()); 201 if (!attstr.isEmpty() && !valstr.isEmpty()) { 202 if (typatts.contains(item.getKey())) { 203 out.format(" <tag k='seamark:%s' v='%s'/>%n", attstr, StringEscapeUtils.escapeXml10(valstr)); 204 } else { 205 out.format(" <tag k='seamark:%s:%s' v='%s'/>%n", S57obj.stringType(feature.type), attstr, StringEscapeUtils.escapeXml10(valstr)); 206 } 207 } 208 } 209 for (Obj obj : feature.objs.keySet()) { 210 ObjTab tab = feature.objs.get(obj); 211 for (int ix : tab.keySet()) { 212 AttMap atts = tab.get(ix); 213 for (Map.Entry<Att, AttVal<?>> item : atts.entrySet()) { 214 String attstr = S57att.stringAttribute(item.getKey()); 215 String valstr = S57val.stringValue(item.getValue(), item.getKey()); 216 if (!attstr.isEmpty() && !valstr.isEmpty()) { 217 if ((ix == 0) && (tab.size() == 1)) { 218 out.format(" <tag k='seamark:%s:%s' v='%s'/>%n", S57obj.stringType(obj), attstr, StringEscapeUtils.escapeXml10(valstr)); 219 } else { 220 out.format(" <tag k='seamark:%s:%d:%s' v='%s'/>%n", S57obj.stringType(obj), ix + 1, attstr, StringEscapeUtils.escapeXml10(valstr)); 221 } 222 } 223 } 224 } 225 } 226 } 227 227 228 228 }
Note:
See TracChangeset
for help on using the changeset viewer.
