source: osm/applications/editors/josm/plugins/seachart/src/s57/S57dec.java@ 32481

Last change on this file since 32481 was 32394, checked in by donvip, 9 years ago

checkstyle

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