| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package s57;
|
|---|
| 3 |
|
|---|
| 4 | import java.io.FileInputStream;
|
|---|
| 5 | import java.io.IOException;
|
|---|
| 6 |
|
|---|
| 7 | import s57.S57dat.S57field;
|
|---|
| 8 | import s57.S57dat.S57subf;
|
|---|
| 9 | import s57.S57map.Nflag;
|
|---|
| 10 |
|
|---|
| 11 | /**
|
|---|
| 12 | * @author Malcolm Herring
|
|---|
| 13 | */
|
|---|
| 14 | public final class S57dec { // S57 ENC file input & map conversion
|
|---|
| 15 | private S57dec() {
|
|---|
| 16 | // Hide default constructor for utilities classes
|
|---|
| 17 | }
|
|---|
| 18 | // CHECKSTYLE.OFF: LineLength
|
|---|
| 19 |
|
|---|
| 20 | public static void decodeChart(FileInputStream in, S57map map) throws IOException {
|
|---|
| 21 | S57dat.rnum = 0;
|
|---|
| 22 | byte[] leader = new byte[24];
|
|---|
| 23 | byte[] record = new byte[0];
|
|---|
| 24 | boolean ddr = false;
|
|---|
| 25 | int length = 0;
|
|---|
| 26 | int fields = 0;;
|
|---|
| 27 | int mapfl, mapfp, mapts, entry;
|
|---|
| 28 | String tag;
|
|---|
| 29 | int len;
|
|---|
| 30 | int pos;
|
|---|
| 31 | boolean inFeature = false;
|
|---|
| 32 |
|
|---|
| 33 | double comf = 1;
|
|---|
| 34 | double somf = 1;
|
|---|
| 35 | long name = 0;
|
|---|
| 36 | S57map.Nflag nflag = Nflag.ANON;
|
|---|
| 37 | S57map.Pflag pflag = S57map.Pflag.NOSP;
|
|---|
| 38 | long objl = 0;
|
|---|
| 39 |
|
|---|
| 40 | while (in.read(leader) == 24) {
|
|---|
| 41 | try {
|
|---|
| 42 | length = Integer.parseInt(new String(leader, 0, 5)) - 24;
|
|---|
| 43 | record = new byte[length];
|
|---|
| 44 | ddr = (leader[6] == 'L');
|
|---|
| 45 | fields = Integer.parseInt(new String(leader, 12, 5)) - 24;
|
|---|
| 46 | } catch (Exception e) {
|
|---|
| 47 | System.err.println("Invalid file format - Encrypted/compressed ENC file?");
|
|---|
| 48 | System.exit(-1);
|
|---|
| 49 | }
|
|---|
| 50 | mapfl = leader[20] - '0';
|
|---|
| 51 | mapfp = leader[21] - '0';
|
|---|
| 52 | mapts = leader[23] - '0';
|
|---|
| 53 | entry = mapfl + mapfp + mapts;
|
|---|
| 54 | if (in.read(record) != length)
|
|---|
| 55 | break;
|
|---|
| 56 | for (int idx = 0; idx < fields-1; idx += entry) {
|
|---|
| 57 | tag = new String(record, idx, mapts);
|
|---|
| 58 | len = Integer.parseInt(new String(record, idx+mapts, mapfl));
|
|---|
| 59 | pos = Integer.parseInt(new String(record, idx+mapts+mapfl, mapfp));
|
|---|
| 60 | if (!ddr) {
|
|---|
| 61 | switch (tag.toString()) {
|
|---|
| 62 | case "0001":
|
|---|
| 63 | int i8rn = ((Long) S57dat.decSubf(record, fields + pos, S57field.I8RI, S57subf.I8RN)).intValue();
|
|---|
| 64 | // if (i8rn != ++S57dat.rnum) {
|
|---|
| 65 | // System.err.println("Out of order record ID");
|
|---|
| 66 | // in.close();
|
|---|
| 67 | // System.exit(-1);
|
|---|
| 68 | // }
|
|---|
| 69 | break;
|
|---|
| 70 | case "DSSI":
|
|---|
| 71 | S57dat.decSubf(record, fields + pos, S57field.DSSI, S57subf.AALL);
|
|---|
| 72 | S57dat.decSubf(S57subf.NALL);
|
|---|
| 73 | break;
|
|---|
| 74 | case "DSPM":
|
|---|
| 75 | comf = (Long) S57dat.decSubf(record, fields + pos, S57field.DSPM, S57subf.COMF);
|
|---|
| 76 | somf = (Long) S57dat.decSubf(S57subf.SOMF);
|
|---|
| 77 | break;
|
|---|
| 78 | case "FRID":
|
|---|
| 79 | inFeature = true;
|
|---|
| 80 | switch (((Long) S57dat.decSubf(record, fields + pos, S57field.FRID, S57subf.PRIM)).intValue()) {
|
|---|
| 81 | case 1:
|
|---|
| 82 | pflag = S57map.Pflag.POINT;
|
|---|
| 83 | break;
|
|---|
| 84 | case 2:
|
|---|
| 85 | pflag = S57map.Pflag.LINE;
|
|---|
| 86 | break;
|
|---|
| 87 | case 3:
|
|---|
| 88 | pflag = S57map.Pflag.AREA;
|
|---|
| 89 | break;
|
|---|
| 90 | default:
|
|---|
| 91 | pflag = S57map.Pflag.NOSP;
|
|---|
| 92 | }
|
|---|
| 93 | objl = (Long) S57dat.decSubf(S57subf.OBJL);
|
|---|
| 94 | break;
|
|---|
| 95 | case "FOID":
|
|---|
| 96 | name = (long) S57dat.decSubf(record, fields + pos, S57field.LNAM, S57subf.LNAM);
|
|---|
| 97 | map.newFeature(name, pflag, objl);
|
|---|
| 98 | break;
|
|---|
| 99 | case "ATTF":
|
|---|
| 100 | S57dat.setField(record, fields + pos, S57field.ATTF, len);
|
|---|
| 101 | do {
|
|---|
| 102 | long attl = (Long) S57dat.decSubf(S57subf.ATTL);
|
|---|
| 103 | String atvl = ((String) S57dat.decSubf(S57subf.ATVL)).trim();
|
|---|
| 104 | if (!atvl.isEmpty()) {
|
|---|
| 105 | map.newAtt(attl, atvl);
|
|---|
| 106 | }
|
|---|
| 107 | } while (S57dat.more());
|
|---|
| 108 | break;
|
|---|
| 109 | case "FFPT":
|
|---|
| 110 | S57dat.setField(record, fields + pos, S57field.FFPT, len);
|
|---|
| 111 | do {
|
|---|
| 112 | name = (long) S57dat.decSubf(S57subf.LNAM);
|
|---|
| 113 | int rind = ((Long) S57dat.decSubf(S57subf.RIND)).intValue();
|
|---|
| 114 | S57dat.decSubf(S57subf.COMT);
|
|---|
| 115 | map.refObj(name, rind);
|
|---|
| 116 | } while (S57dat.more());
|
|---|
| 117 | break;
|
|---|
| 118 | case "FSPT":
|
|---|
| 119 | S57dat.setField(record, fields + pos, S57field.FSPT, len);
|
|---|
| 120 | do {
|
|---|
| 121 | name = (long) S57dat.decSubf(S57subf.NAME) << 16;
|
|---|
| 122 | map.newPrim(name, (Long) S57dat.decSubf(S57subf.ORNT), (Long) S57dat.decSubf(S57subf.USAG));
|
|---|
| 123 | S57dat.decSubf(S57subf.MASK);
|
|---|
| 124 | } while (S57dat.more());
|
|---|
| 125 | break;
|
|---|
| 126 | case "VRID":
|
|---|
| 127 | inFeature = false;
|
|---|
| 128 | name = (Long) S57dat.decSubf(record, fields + pos, S57field.VRID, S57subf.RCNM);
|
|---|
| 129 | switch ((int) name) {
|
|---|
| 130 | case 110:
|
|---|
| 131 | nflag = Nflag.ISOL;
|
|---|
| 132 | break;
|
|---|
| 133 | case 120:
|
|---|
| 134 | nflag = Nflag.CONN;
|
|---|
| 135 | break;
|
|---|
| 136 | default:
|
|---|
| 137 | nflag = Nflag.ANON;
|
|---|
| 138 | break;
|
|---|
| 139 | }
|
|---|
| 140 | name <<= 32;
|
|---|
| 141 | name += (long) S57dat.decSubf(S57subf.RCID);
|
|---|
| 142 | name <<= 16;
|
|---|
| 143 | if (nflag == Nflag.ANON) {
|
|---|
| 144 | map.newEdge(name);
|
|---|
| 145 | }
|
|---|
| 146 | break;
|
|---|
| 147 | case "VRPT":
|
|---|
| 148 | S57dat.setField(record, fields + pos, S57field.VRPT, len);
|
|---|
| 149 | do {
|
|---|
| 150 | long conn = (Long) S57dat.decSubf(S57subf.NAME) << 16;
|
|---|
| 151 | int topi = ((Long) S57dat.decSubf(S57subf.TOPI)).intValue();
|
|---|
| 152 | map.addConn(conn, topi);
|
|---|
| 153 | S57dat.decSubf(S57subf.MASK);
|
|---|
| 154 | } while (S57dat.more());
|
|---|
| 155 | break;
|
|---|
| 156 | case "SG2D":
|
|---|
| 157 | S57dat.setField(record, fields + pos, S57field.SG2D, len);
|
|---|
| 158 | do {
|
|---|
| 159 | double lat = (double) ((Long) S57dat.decSubf(S57subf.YCOO)) / comf;
|
|---|
| 160 | double lon = (double) ((Long) S57dat.decSubf(S57subf.XCOO)) / comf;
|
|---|
| 161 | if (nflag == Nflag.ANON) {
|
|---|
| 162 | map.newNode(++name, lat, lon, nflag);
|
|---|
| 163 | } else {
|
|---|
| 164 | map.newNode(name, lat, lon, nflag);
|
|---|
| 165 | }
|
|---|
| 166 | lat = Math.toRadians(lat);
|
|---|
| 167 | lon = Math.toRadians(lon);
|
|---|
| 168 | if (lat < map.bounds.minlat)
|
|---|
| 169 | map.bounds.minlat = lat;
|
|---|
| 170 | if (lat > map.bounds.maxlat)
|
|---|
| 171 | map.bounds.maxlat = lat;
|
|---|
| 172 | if (lon < map.bounds.minlon)
|
|---|
| 173 | map.bounds.minlon = lon;
|
|---|
| 174 | if (lon > map.bounds.maxlon)
|
|---|
| 175 | map.bounds.maxlon = lon;
|
|---|
| 176 | } while (S57dat.more());
|
|---|
| 177 | break;
|
|---|
| 178 | case "SG3D":
|
|---|
| 179 | S57dat.setField(record, fields + pos, S57field.SG3D, len);
|
|---|
| 180 | do {
|
|---|
| 181 | double lat = (double) ((Long) S57dat.decSubf(S57subf.YCOO)) / comf;
|
|---|
| 182 | double lon = (double) ((Long) S57dat.decSubf(S57subf.XCOO)) / comf;
|
|---|
| 183 | double depth = (double) ((Long) S57dat.decSubf(S57subf.VE3D)) / somf;
|
|---|
| 184 | map.newNode(name++, lat, lon, depth);
|
|---|
| 185 | lat = Math.toRadians(lat);
|
|---|
| 186 | lon = Math.toRadians(lon);
|
|---|
| 187 | if (lat < map.bounds.minlat)
|
|---|
| 188 | map.bounds.minlat = lat;
|
|---|
| 189 | if (lat > map.bounds.maxlat)
|
|---|
| 190 | map.bounds.maxlat = lat;
|
|---|
| 191 | if (lon < map.bounds.minlon)
|
|---|
| 192 | map.bounds.minlon = lon;
|
|---|
| 193 | if (lon > map.bounds.maxlon)
|
|---|
| 194 | map.bounds.maxlon = lon;
|
|---|
| 195 | } while (S57dat.more());
|
|---|
| 196 | break;
|
|---|
| 197 | default:
|
|---|
| 198 | break;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | if (inFeature) {
|
|---|
| 202 | map.endFeature();
|
|---|
| 203 | inFeature = false;
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | }
|
|---|
| 207 | map.endFile();
|
|---|
| 208 | in.close();
|
|---|
| 209 |
|
|---|
| 210 | return;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | }
|
|---|