source: osm/applications/editors/josm/plugins/smed2/src/s57/S57dec.java@ 30284

Last change on this file since 30284 was 30284, checked in by malcolmh, 11 years ago

save

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