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