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 = 0;
|
---|
25 | int fields = 0;;
|
---|
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 | try {
|
---|
42 | length = Integer.parseInt(new String(leader, 0, 5)) - 24;
|
---|
43 | ddr = (leader[6] == 'L');
|
---|
44 | fields = Integer.parseInt(new String(leader, 12, 5)) - 24;
|
---|
45 | } catch (Exception e) {
|
---|
46 | System.err.println("Invalid file format - Encrypted/compressed ENC file?");
|
---|
47 | System.exit(-1);
|
---|
48 | }
|
---|
49 | mapfl = leader[20] - '0';
|
---|
50 | mapfp = leader[21] - '0';
|
---|
51 | mapts = leader[23] - '0';
|
---|
52 | entry = mapfl + mapfp + mapts;
|
---|
53 | byte[] record = new byte[length];
|
---|
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 (S57dat.enumField(tag)) {
|
---|
62 | case I8RI:
|
---|
63 | int i8rn = ((Long) S57dat.getSubf(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.getSubf(record, fields + pos, S57field.DSSI, S57subf.AALL);
|
---|
72 | S57dat.getSubf(S57subf.NALL);
|
---|
73 | break;
|
---|
74 | case DSPM:
|
---|
75 | comf = (double) (Long) S57dat.getSubf(record, fields + pos, S57field.DSPM, S57subf.COMF);
|
---|
76 | somf = (double) (Long) S57dat.getSubf(S57subf.SOMF);
|
---|
77 | break;
|
---|
78 | case FRID:
|
---|
79 | inFeature = true;
|
---|
80 | switch (((Long)S57dat.getSubf(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.getSubf(S57subf.OBJL);
|
---|
94 | break;
|
---|
95 | case FOID:
|
---|
96 | name = (Long) S57dat.getSubf(record, fields + pos, S57field.FOID, 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.getSubf(S57subf.ATTL);
|
---|
103 | String atvl = ((String) S57dat.getSubf(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.getSubf(S57subf.LNAM);
|
---|
113 | int rind = ((Long) S57dat.getSubf(S57subf.RIND)).intValue();
|
---|
114 | S57dat.getSubf(S57subf.COMT);
|
---|
115 | map.newObj(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.getSubf(S57subf.NAME) << 16;
|
---|
122 | map.newPrim(name, (Long) S57dat.getSubf(S57subf.ORNT), (Long) S57dat.getSubf(S57subf.USAG));
|
---|
123 | S57dat.getSubf(S57subf.MASK);
|
---|
124 | } while (S57dat.more());
|
---|
125 | break;
|
---|
126 | case VRID:
|
---|
127 | inFeature = false;
|
---|
128 | name = (long) (Long)S57dat.getSubf(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.getSubf(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.getSubf(S57subf.NAME) << 16;
|
---|
151 | int topi = ((Long) S57dat.getSubf(S57subf.TOPI)).intValue();
|
---|
152 | map.addConn(conn, topi);
|
---|
153 | S57dat.getSubf(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.getSubf(S57subf.YCOO)) / comf;
|
---|
160 | double lon = (double) ((Long) S57dat.getSubf(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 | if (lat < bounds.minlat)
|
---|
167 | bounds.minlat = lat;
|
---|
168 | if (lat > bounds.maxlat)
|
---|
169 | bounds.maxlat = lat;
|
---|
170 | if (lon < bounds.minlon)
|
---|
171 | bounds.minlon = lon;
|
---|
172 | if (lon > bounds.maxlon)
|
---|
173 | bounds.maxlon = lon;
|
---|
174 | } while (S57dat.more());
|
---|
175 | break;
|
---|
176 | case SG3D:
|
---|
177 | S57dat.setField(record, fields + pos, S57field.SG3D, len);
|
---|
178 | do {
|
---|
179 | double lat = (double) ((Long) S57dat.getSubf(S57subf.YCOO)) / comf;
|
---|
180 | double lon = (double) ((Long) S57dat.getSubf(S57subf.XCOO)) / comf;
|
---|
181 | double depth = (double) ((Long) S57dat.getSubf(S57subf.VE3D)) / somf;
|
---|
182 | map.newNode(name++, lat, lon, depth);
|
---|
183 | if (lat < bounds.minlat)
|
---|
184 | bounds.minlat = lat;
|
---|
185 | if (lat > bounds.maxlat)
|
---|
186 | bounds.maxlat = lat;
|
---|
187 | if (lon < bounds.minlon)
|
---|
188 | bounds.minlon = lon;
|
---|
189 | if (lon > bounds.maxlon)
|
---|
190 | bounds.maxlon = lon;
|
---|
191 | } while (S57dat.more());
|
---|
192 | break;
|
---|
193 | default:
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | if (inFeature) {
|
---|
198 | map.endFeature();
|
---|
199 | inFeature = false;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 | map.endFile();
|
---|
204 | in.close();
|
---|
205 |
|
---|
206 | return bounds;
|
---|
207 | }
|
---|
208 |
|
---|
209 | }
|
---|