| 1 | /* Copyright 2013 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 |
|
|---|
| 10 | package s57;
|
|---|
| 11 |
|
|---|
| 12 | import java.io.*;
|
|---|
| 13 |
|
|---|
| 14 | import s57.S57map.*;
|
|---|
| 15 | import s57.S57dat.*;
|
|---|
| 16 |
|
|---|
| 17 | public class Js57toosm {
|
|---|
| 18 |
|
|---|
| 19 | public static int rnum = 0;
|
|---|
| 20 |
|
|---|
| 21 | public static void main(String[] args) throws IOException {
|
|---|
| 22 |
|
|---|
| 23 | FileInputStream in = new FileInputStream("/Users/mherring/boatsw/oseam/josm/plugins/smed2/js57toosm/tst.000");
|
|---|
| 24 | PrintStream out = System.out;
|
|---|
| 25 |
|
|---|
| 26 | byte[] leader = new byte[24];
|
|---|
| 27 | boolean ddr = false;
|
|---|
| 28 | int length;
|
|---|
| 29 | int fields;
|
|---|
| 30 | int mapfl, mapfp, mapts, entry;
|
|---|
| 31 | String tag;
|
|---|
| 32 | int len;
|
|---|
| 33 | int pos;
|
|---|
| 34 |
|
|---|
| 35 | double comf = 1;
|
|---|
| 36 | double somf = 1;
|
|---|
| 37 | long name = 0;
|
|---|
| 38 | S57map.Nflag nflag = Nflag.ANON;
|
|---|
| 39 | S57map map = new S57map();
|
|---|
| 40 | double minlat = 90, minlon = 180, maxlat = -90, maxlon = -180;
|
|---|
| 41 |
|
|---|
| 42 | while (in.read(leader) == 24) {
|
|---|
| 43 | length = Integer.parseInt(new String(leader, 0, 5)) - 24;
|
|---|
| 44 | ddr = (leader[6] == 'L');
|
|---|
| 45 | fields = Integer.parseInt(new String(leader, 12, 5)) - 24;
|
|---|
| 46 | mapfl = leader[20] - '0';
|
|---|
| 47 | mapfp = leader[21] - '0';
|
|---|
| 48 | mapts = leader[23] - '0';
|
|---|
| 49 | entry = mapfl + mapfp + mapts;
|
|---|
| 50 | byte[] record = new byte[length];
|
|---|
| 51 | if (in.read(record) != length)
|
|---|
| 52 | break;
|
|---|
| 53 | for (int idx = 0; idx < fields-1; idx += entry) {
|
|---|
| 54 | tag = new String(record, idx, mapts);
|
|---|
| 55 | len = Integer.parseInt(new String(record, idx+mapts, mapfl));
|
|---|
| 56 | pos = Integer.parseInt(new String(record, idx+mapts+mapfl, mapfp));
|
|---|
| 57 | if (!ddr) {
|
|---|
| 58 | if ("0001".equals(tag)) {
|
|---|
| 59 | int i8rn = ((Long)S57dat.getSubf(record, fields+pos, S57field.I8RI, S57subf.I8RN)).intValue();
|
|---|
| 60 | if (i8rn != ++rnum) {
|
|---|
| 61 | out.println("Out of order record ID");
|
|---|
| 62 | in.close();
|
|---|
| 63 | System.exit(-1);
|
|---|
| 64 | }
|
|---|
| 65 | } else if ("DSID".equals(tag)) {
|
|---|
| 66 | } else if ("DSSI".equals(tag)) {
|
|---|
| 67 | } else if ("DSPM".equals(tag)) {
|
|---|
| 68 | comf = (double)(Long)S57dat.getSubf(record, fields+pos, S57field.DSPM, S57subf.COMF);
|
|---|
| 69 | somf = (double)(Long)S57dat.getSubf(S57subf.SOMF);
|
|---|
| 70 | } else if ("FRID".equals(tag)) {
|
|---|
| 71 | } else if ("FOID".equals(tag)) {
|
|---|
| 72 | } else if ("ATTF".equals(tag)) {
|
|---|
| 73 | } else if ("NATF".equals(tag)) {
|
|---|
| 74 | } else if ("FFPC".equals(tag)) {
|
|---|
| 75 | } else if ("FFPT".equals(tag)) {
|
|---|
| 76 | } else if ("FSPC".equals(tag)) {
|
|---|
| 77 | } else if ("FSPT".equals(tag)) {
|
|---|
| 78 | } else if ("VRID".equals(tag)) {
|
|---|
| 79 | name = (Long)S57dat.getSubf(record, fields+pos, S57field.VRID, S57subf.RCNM);
|
|---|
| 80 | switch ((int)name) {
|
|---|
| 81 | case 110:
|
|---|
| 82 | nflag = Nflag.ISOL;
|
|---|
| 83 | break;
|
|---|
| 84 | case 120:
|
|---|
| 85 | nflag = Nflag.CONN;
|
|---|
| 86 | break;
|
|---|
| 87 | default:
|
|---|
| 88 | nflag = Nflag.ANON;
|
|---|
| 89 | break;
|
|---|
| 90 | }
|
|---|
| 91 | name <<= 32;
|
|---|
| 92 | name += (Long)S57dat.getSubf(record, fields+pos, S57field.VRID, S57subf.RCID);
|
|---|
| 93 | name <<= 16;
|
|---|
| 94 | if (nflag == Nflag.ANON) {
|
|---|
| 95 | map.newEdge(name);
|
|---|
| 96 | }
|
|---|
| 97 | } else if ("ATTV".equals(tag)) {
|
|---|
| 98 | } else if ("VRPC".equals(tag)) {
|
|---|
| 99 | } else if ("VRPT".equals(tag)) {
|
|---|
| 100 | name = (Long)S57dat.getSubf(record, fields+pos, S57field.VRPT, S57subf.NAME) << 16;
|
|---|
| 101 | int topi = ((Long)S57dat.getSubf(S57subf.TOPI)).intValue();
|
|---|
| 102 | map.addConn(name, topi);
|
|---|
| 103 | name = (Long)S57dat.getSubf(S57subf.NAME) << 16;
|
|---|
| 104 | topi = ((Long)S57dat.getSubf(S57subf.TOPI)).intValue();
|
|---|
| 105 | map.addConn(name, topi);
|
|---|
| 106 | } else if ("SGCC".equals(tag)) {
|
|---|
| 107 | } else if ("SG2D".equals(tag)) {
|
|---|
| 108 | S57dat.setField(record, fields + pos, S57field.SG2D, len);
|
|---|
| 109 | while (S57dat.more()) {
|
|---|
| 110 | double lat = (double) ((Long) S57dat.getSubf(S57subf.YCOO)) / comf;
|
|---|
| 111 | double lon = (double) ((Long) S57dat.getSubf(S57subf.XCOO)) / comf;
|
|---|
| 112 | if (nflag == Nflag.ANON) {
|
|---|
| 113 | map.newNode(++name, lat, lon, nflag);
|
|---|
| 114 | } else {
|
|---|
| 115 | map.newNode(name, lat, lon, nflag);
|
|---|
| 116 | }
|
|---|
| 117 | if (lat < minlat) minlat = lat;
|
|---|
| 118 | if (lat > maxlat) maxlat = lat;
|
|---|
| 119 | if (lon < minlon) minlon = lon;
|
|---|
| 120 | if (lon > maxlon) maxlon = lon;
|
|---|
| 121 | }
|
|---|
| 122 | } else if ("SG3D".equals(tag)) {
|
|---|
| 123 | S57dat.setField(record, fields + pos, S57field.SG3D, len);
|
|---|
| 124 | while (S57dat.more()) {
|
|---|
| 125 | double lat = (double) ((Long) S57dat.getSubf(S57subf.YCOO)) / comf;
|
|---|
| 126 | double lon = (double) ((Long) S57dat.getSubf(S57subf.XCOO)) / comf;
|
|---|
| 127 | double depth = (double) ((Long) S57dat.getSubf(S57subf.VE3D)) / somf;
|
|---|
| 128 | map.newNode(name++, lat, lon, depth);
|
|---|
| 129 | if (lat < minlat) minlat = lat;
|
|---|
| 130 | if (lat > maxlat) maxlat = lat;
|
|---|
| 131 | if (lon < minlon) minlon = lon;
|
|---|
| 132 | if (lon > maxlon) maxlon = lon;
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | in.close();
|
|---|
| 139 |
|
|---|
| 140 | out.println("<?xml version='1.0' encoding='UTF-8'?>");
|
|---|
| 141 | out.println("<osm version='0.6' generator='js57toosm'>");
|
|---|
| 142 | out.println("<bounds minlat='" + minlat +"' minlon='" + minlon + "' maxlat='" + maxlat + "' maxlon='" + maxlon + "'/>");
|
|---|
| 143 |
|
|---|
| 144 | for (long id : map.nodes.keySet()) {
|
|---|
| 145 | Snode node = map.nodes.get(id);
|
|---|
| 146 | if (node.flg == S57map.Nflag.DPTH) {
|
|---|
| 147 | out.format(" <node id='%d' lat='%f' lon='%f' version='1'>%n", -id, Math.toDegrees(node.lat), Math.toDegrees(node.lon));
|
|---|
| 148 | out.format(" <tag k='seamark:type' v='sounding'/>%n");
|
|---|
| 149 | out.format(" <tag k='seamark:sounding:depth' v='%.1f'/>%n", ((Dnode)node).val);
|
|---|
| 150 | out.format(" </node>%n");
|
|---|
| 151 | } else {
|
|---|
| 152 | out.format(" <node id='%d' lat='%f' lon='%f' version='1'/>%n",-id, Math.toDegrees(node.lat), Math.toDegrees(node.lon));
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | for (long id : map.edges.keySet()) {
|
|---|
| 157 | Edge edge = map.edges.get(id);
|
|---|
| 158 | out.format(" <way id='%d' version='1'>%n", -id);
|
|---|
| 159 | out.format(" <nd ref='%d'/>%n", -edge.first);
|
|---|
| 160 | for (long anon : edge.nodes) {
|
|---|
| 161 | out.format(" <nd ref='%d'/>%n", -anon);
|
|---|
| 162 | }
|
|---|
| 163 | out.format(" <nd ref='%d'/>%n", -edge.last);
|
|---|
| 164 | out.format(" </way>%n");
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | out.println("</osm>\n");
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | }
|
|---|