source: osm/applications/editors/josm/plugins/seachart/src/s57/S57dat.java@ 35070

Last change on this file since 35070 was 34921, checked in by malcolmh, 6 years ago

fix exception problem

File size: 32.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package s57;
3
4import java.io.UnsupportedEncodingException;
5import java.nio.charset.StandardCharsets;
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.EnumMap;
9
10import s57.S57map.Feature;
11import s57.S57map.Pflag;
12import s57.S57obj.Obj;
13
14/**
15 * @author Malcolm Herring
16 */
17public final class S57dat { // S57 ENC file fields lookup tables & methods
18 private S57dat() {
19 // Hide default constructor for utilities classes
20 }
21 // CHECKSTYLE.OFF: LineLength
22
23 public static class S57conv {
24 int asc; // 0=A(), 1+=A(n)
25 int bin; // 0=ASCII, +ve=b1n(unsigned LE), -ve=b2n(signed LE), n>8=b1(n/8)(unsigned BE)
26 S57conv(int a, int b) {
27 asc = a; bin = b;
28 }
29 }
30
31 public enum S57subf { I8RN, RCNM, RCID, EXPP, INTU, DSNM, EDTN, UPDN, UADT, ISDT, STED, PRSP, PSDN, PRED, PROF, AGEN, COMT, DSTR, AALL, NALL, NOMR, NOCR, NOGR, NOLR, NOIN, NOCN,
32 NOED, NOFA, HDAT, VDAT, SDAT, CSCL, DUNI, HUNI, PUNI, COUN, COMF, SOMF, PROJ, PRP1, PRP2, PRP3, PRP4, FEAS, FNOR, FPMF, RPID, RYCO, RXCO, CURP, RXVL, RYVL, PRCO, ESDT,
33 LSDT, DCRT, CODT, PACC, HACC, SACC, FILE, LFIL, VOLM, IMPL, SLAT, WLON, NLAT, ELON, CRCS, NAM1, NAM2, OORA, OAAC, OACO, OALL, OATY, DEFN, AUTH, RFTP, RFVL, ATLB, ATDO,
34 ADMU, ADFT, RAVA, DVAL, DVSD, OBLB, ASET, PRIM, GRUP, OBJL, RVER, RUIN, FIDN, FIDS, ATTL, ATVL, FFUI, FFIX, NFPT, LNAM, RIND, FSUI, FSIX, NSPT, NAME, ORNT, USAG, MASK,
35 VPUI, VPIX, NVPT, TOPI, CCUI, CCIX, CCNC, YCOO, XCOO, VE3D, ATYP, SURF, ORDR, RESO, STPT, CTPT, ENPT, CDPM, CDPR }
36
37 private static final EnumMap<S57subf, S57conv> convs = new EnumMap<>(S57subf.class);
38 static {
39 convs.put(S57subf.I8RN, new S57conv(5, 2));
40 convs.put(S57subf.RCNM, new S57conv(2, 1)); convs.put(S57subf.RCID, new S57conv(10, 4)); convs.put(S57subf.EXPP, new S57conv(1, 1));
41 convs.put(S57subf.INTU, new S57conv(1, 1)); convs.put(S57subf.DSNM, new S57conv(0, 0)); convs.put(S57subf.EDTN, new S57conv(0, 0));
42 convs.put(S57subf.UPDN, new S57conv(0, 0)); convs.put(S57subf.UADT, new S57conv(8, 0)); convs.put(S57subf.ISDT, new S57conv(8, 0));
43 convs.put(S57subf.STED, new S57conv(4, 0)); convs.put(S57subf.PRSP, new S57conv(3, 1)); convs.put(S57subf.PSDN, new S57conv(0, 0));
44 convs.put(S57subf.PRED, new S57conv(0, 0)); convs.put(S57subf.PROF, new S57conv(2, 1)); convs.put(S57subf.AGEN, new S57conv(2, 2));
45 convs.put(S57subf.COMT, new S57conv(0, 0)); convs.put(S57subf.DSTR, new S57conv(2, 1)); convs.put(S57subf.AALL, new S57conv(1, 1));
46 convs.put(S57subf.NALL, new S57conv(1, 1)); convs.put(S57subf.NOMR, new S57conv(0, 4)); convs.put(S57subf.NOCR, new S57conv(0, 4));
47 convs.put(S57subf.NOGR, new S57conv(0, 4)); convs.put(S57subf.NOLR, new S57conv(0, 4)); convs.put(S57subf.NOIN, new S57conv(0, 4));
48 convs.put(S57subf.NOCN, new S57conv(0, 4)); convs.put(S57subf.NOED, new S57conv(0, 4)); convs.put(S57subf.NOFA, new S57conv(0, 4));
49 convs.put(S57subf.HDAT, new S57conv(3, 1)); convs.put(S57subf.VDAT, new S57conv(2, 1)); convs.put(S57subf.SDAT, new S57conv(2, 1));
50 convs.put(S57subf.CSCL, new S57conv(0, 4)); convs.put(S57subf.DUNI, new S57conv(2, 1)); convs.put(S57subf.HUNI, new S57conv(2, 1));
51 convs.put(S57subf.PUNI, new S57conv(2, 1)); convs.put(S57subf.COUN, new S57conv(2, 1)); convs.put(S57subf.COMF, new S57conv(0, 4));
52 convs.put(S57subf.SOMF, new S57conv(0, 4)); convs.put(S57subf.PROJ, new S57conv(3, 1)); convs.put(S57subf.PRP1, new S57conv(0, -4));
53 convs.put(S57subf.PRP2, new S57conv(0, -4)); convs.put(S57subf.PRP3, new S57conv(0, -4)); convs.put(S57subf.PRP4, new S57conv(0, -4));
54 convs.put(S57subf.FEAS, new S57conv(0, -4)); convs.put(S57subf.FNOR, new S57conv(0, -4)); convs.put(S57subf.FPMF, new S57conv(0, 4));
55 convs.put(S57subf.RPID, new S57conv(1, 1)); convs.put(S57subf.RYCO, new S57conv(0, -4)); convs.put(S57subf.RXCO, new S57conv(0, -4));
56 convs.put(S57subf.CURP, new S57conv(2, 1)); convs.put(S57subf.RXVL, new S57conv(0, -4)); convs.put(S57subf.RYVL, new S57conv(0, -4));
57 convs.put(S57subf.PRCO, new S57conv(2, 2)); convs.put(S57subf.ESDT, new S57conv(8, 0)); convs.put(S57subf.LSDT, new S57conv(8, 0));
58 convs.put(S57subf.DCRT, new S57conv(0, 0)); convs.put(S57subf.CODT, new S57conv(8, 0)); convs.put(S57subf.PACC, new S57conv(0, 4));
59 convs.put(S57subf.HACC, new S57conv(0, 4)); convs.put(S57subf.SACC, new S57conv(0, 4)); convs.put(S57subf.FILE, new S57conv(0, 0));
60 convs.put(S57subf.LFIL, new S57conv(0, 0)); convs.put(S57subf.VOLM, new S57conv(0, 0)); convs.put(S57subf.IMPL, new S57conv(3, 0));
61 convs.put(S57subf.SLAT, new S57conv(0, 0)); convs.put(S57subf.WLON, new S57conv(0, 0)); convs.put(S57subf.NLAT, new S57conv(0, 0));
62 convs.put(S57subf.ELON, new S57conv(0, 0)); convs.put(S57subf.CRCS, new S57conv(0, 0)); convs.put(S57subf.NAM1, new S57conv(12, 5));
63 convs.put(S57subf.NAM2, new S57conv(12, 5)); convs.put(S57subf.OORA, new S57conv(1, 1)); convs.put(S57subf.OAAC, new S57conv(6, 0));
64 convs.put(S57subf.OACO, new S57conv(5, 2)); convs.put(S57subf.OALL, new S57conv(0, 0)); convs.put(S57subf.OATY, new S57conv(1, 1));
65 convs.put(S57subf.DEFN, new S57conv(0, 0)); convs.put(S57subf.AUTH, new S57conv(2, 2)); convs.put(S57subf.RFTP, new S57conv(2, 1));
66 convs.put(S57subf.RFVL, new S57conv(0, 0)); convs.put(S57subf.ATLB, new S57conv(5, 2)); convs.put(S57subf.ATDO, new S57conv(1, 1));
67 convs.put(S57subf.ADMU, new S57conv(0, 0)); convs.put(S57subf.ADFT, new S57conv(0, 0)); convs.put(S57subf.RAVA, new S57conv(1, 1));
68 convs.put(S57subf.DVAL, new S57conv(0, 0)); convs.put(S57subf.DVSD, new S57conv(0, 0)); convs.put(S57subf.OBLB, new S57conv(5, 2));
69 convs.put(S57subf.ASET, new S57conv(1, 1)); convs.put(S57subf.PRIM, new S57conv(1, 1)); convs.put(S57subf.GRUP, new S57conv(3, 1));
70 convs.put(S57subf.OBJL, new S57conv(5, 2)); convs.put(S57subf.RVER, new S57conv(3, 2)); convs.put(S57subf.RUIN, new S57conv(1, 1));
71 convs.put(S57subf.FIDN, new S57conv(10, 4)); convs.put(S57subf.FIDS, new S57conv(5, 2)); convs.put(S57subf.ATTL, new S57conv(5, 2));
72 convs.put(S57subf.ATVL, new S57conv(0, 0)); convs.put(S57subf.FFUI, new S57conv(1, 1)); convs.put(S57subf.FFIX, new S57conv(0, 2));
73 convs.put(S57subf.NFPT, new S57conv(0, 2)); convs.put(S57subf.LNAM, new S57conv(17, 8)); convs.put(S57subf.RIND, new S57conv(0, 1));
74 convs.put(S57subf.FSUI, new S57conv(1, 1)); convs.put(S57subf.FSIX, new S57conv(0, 2)); convs.put(S57subf.NSPT, new S57conv(0, 2));
75 convs.put(S57subf.NAME, new S57conv(12, 5)); convs.put(S57subf.ORNT, new S57conv(1, 1)); convs.put(S57subf.USAG, new S57conv(1, 1));
76 convs.put(S57subf.MASK, new S57conv(1, 1)); convs.put(S57subf.VPUI, new S57conv(1, 1)); convs.put(S57subf.VPIX, new S57conv(0, 2));
77 convs.put(S57subf.NVPT, new S57conv(0, 2)); convs.put(S57subf.TOPI, new S57conv(1, 1)); convs.put(S57subf.CCUI, new S57conv(1, 1));
78 convs.put(S57subf.CCIX, new S57conv(0, 2)); convs.put(S57subf.CCNC, new S57conv(0, 2)); convs.put(S57subf.YCOO, new S57conv(0, -4));
79 convs.put(S57subf.XCOO, new S57conv(0, -4)); convs.put(S57subf.VE3D, new S57conv(0, -4)); convs.put(S57subf.ATYP, new S57conv(1, 1));
80 convs.put(S57subf.SURF, new S57conv(1, 1)); convs.put(S57subf.ORDR, new S57conv(1, 1)); convs.put(S57subf.RESO, new S57conv(0, 4));
81 convs.put(S57subf.STPT, new S57conv(0, 0)); convs.put(S57subf.CTPT, new S57conv(0, 0)); convs.put(S57subf.ENPT, new S57conv(0, 0));
82 convs.put(S57subf.CDPM, new S57conv(0, 0)); convs.put(S57subf.CDPR, new S57conv(0, 0));
83 }
84
85 public enum S57field { I8RI, DSID, DSSI, DSPM, DSPR, DSRC, DSHT, DSAC, CATD, CATX, DDDF, DDDR, DDDI, DDOM, DDRF, DDSI, DDSC,
86 FRID, FOID, LNAM, ATTF, NATF, FFPC, FFPT, FSPC, FSPT, VRID, ATTV, VRPC, VRPT, SGCC, SG2D, SG3D, ARCC, AR2D, EL2D, CT2D }
87
88 private static ArrayList<S57subf> S57i8ri = new ArrayList<>(Arrays.asList(S57subf.I8RN));
89 private static ArrayList<S57subf> S57dsid = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.EXPP, S57subf.INTU, S57subf.DSNM, S57subf.EDTN, S57subf.UPDN,
90 S57subf.UADT, S57subf.ISDT, S57subf.STED, S57subf.PRSP, S57subf.PSDN, S57subf.PRED, S57subf.PROF, S57subf.AGEN, S57subf.COMT));
91 private static ArrayList<S57subf> S57dssi = new ArrayList<>(Arrays.asList(S57subf.DSTR, S57subf.AALL, S57subf.NALL, S57subf.NOMR, S57subf.NOCR, S57subf.NOGR, S57subf.NOLR,
92 S57subf.NOIN, S57subf.NOCN, S57subf.NOED, S57subf.NOFA));
93 private static ArrayList<S57subf> S57dspm = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.HDAT, S57subf.VDAT, S57subf.SDAT, S57subf.CSCL, S57subf.DUNI,
94 S57subf.HUNI, S57subf.PUNI, S57subf.COUN, S57subf.COMF, S57subf.SOMF, S57subf.COMT));
95 private static ArrayList<S57subf> S57dspr = new ArrayList<>(Arrays.asList(S57subf.PROJ, S57subf.PRP1, S57subf.PRP2, S57subf.PRP3, S57subf.PRP4, S57subf.FEAS, S57subf.FNOR,
96 S57subf.FPMF, S57subf.COMT));
97 private static ArrayList<S57subf> S57dsrc = new ArrayList<>(Arrays.asList(S57subf.RPID, S57subf.RYCO, S57subf.RXCO, S57subf.CURP, S57subf.FPMF, S57subf.RXVL, S57subf.RYVL,
98 S57subf.COMT));
99 private static ArrayList<S57subf> S57dsht = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.PRCO, S57subf.ESDT, S57subf.LSDT, S57subf.DCRT, S57subf.CODT, S57subf.COMT));
100 private static ArrayList<S57subf> S57dsac = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.PACC, S57subf.HACC, S57subf.SACC, S57subf.FPMF, S57subf.COMT));
101 private static ArrayList<S57subf> S57catd = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.FILE, S57subf.LFIL, S57subf.VOLM, S57subf.IMPL, S57subf.SLAT,
102 S57subf.WLON, S57subf.NLAT, S57subf.ELON, S57subf.CRCS, S57subf.COMT));
103 private static ArrayList<S57subf> S57catx = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.NAM1, S57subf.NAM2, S57subf.COMT));
104 private static ArrayList<S57subf> S57dddf = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.OORA, S57subf.OAAC, S57subf.OACO, S57subf.OALL, S57subf.OATY,
105 S57subf.DEFN, S57subf.AUTH, S57subf.COMT));
106 private static ArrayList<S57subf> S57dddr = new ArrayList<>(Arrays.asList(S57subf.RFTP, S57subf.RFVL));
107 private static ArrayList<S57subf> S57dddi = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.ATLB, S57subf.ATDO, S57subf.ADMU, S57subf.ADFT, S57subf.AUTH, S57subf.COMT));
108 private static ArrayList<S57subf> S57ddom = new ArrayList<>(Arrays.asList(S57subf.RAVA, S57subf.DVAL, S57subf.DVSD, S57subf.DEFN, S57subf.AUTH));
109 private static ArrayList<S57subf> S57ddrf = new ArrayList<>(Arrays.asList(S57subf.RFTP, S57subf.RFVL));
110 private static ArrayList<S57subf> S57ddsi = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.OBLB));
111 private static ArrayList<S57subf> S57ddsc = new ArrayList<>(Arrays.asList(S57subf.ATLB, S57subf.ASET, S57subf.AUTH));
112 private static ArrayList<S57subf> S57frid = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.PRIM, S57subf.GRUP, S57subf.OBJL, S57subf.RVER, S57subf.RUIN));
113 private static ArrayList<S57subf> S57foid = new ArrayList<>(Arrays.asList(S57subf.AGEN, S57subf.FIDN, S57subf.FIDS));
114 private static ArrayList<S57subf> S57lnam = new ArrayList<>(Arrays.asList(S57subf.LNAM));
115 private static ArrayList<S57subf> S57attf = new ArrayList<>(Arrays.asList(S57subf.ATTL, S57subf.ATVL));
116 private static ArrayList<S57subf> S57natf = new ArrayList<>(Arrays.asList(S57subf.ATTL, S57subf.ATVL));
117 private static ArrayList<S57subf> S57ffpc = new ArrayList<>(Arrays.asList(S57subf.FFUI, S57subf.FFIX, S57subf.NFPT));
118 private static ArrayList<S57subf> S57ffpt = new ArrayList<>(Arrays.asList(S57subf.LNAM, S57subf.RIND, S57subf.COMT));
119 private static ArrayList<S57subf> S57fspc = new ArrayList<>(Arrays.asList(S57subf.FSUI, S57subf.FSIX, S57subf.NSPT));
120 private static ArrayList<S57subf> S57fspt = new ArrayList<>(Arrays.asList(S57subf.NAME, S57subf.ORNT, S57subf.USAG, S57subf.MASK));
121 private static ArrayList<S57subf> S57vrid = new ArrayList<>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.RVER, S57subf.RUIN));
122 private static ArrayList<S57subf> S57attv = new ArrayList<>(Arrays.asList(S57subf.ATTL, S57subf.ATVL));
123 private static ArrayList<S57subf> S57vrpc = new ArrayList<>(Arrays.asList(S57subf.VPUI, S57subf.VPIX, S57subf.NVPT));
124 private static ArrayList<S57subf> S57vrpt = new ArrayList<>(Arrays.asList(S57subf.NAME, S57subf.ORNT, S57subf.USAG, S57subf.TOPI, S57subf.MASK));
125 private static ArrayList<S57subf> S57sgcc = new ArrayList<>(Arrays.asList(S57subf.CCUI, S57subf.CCIX, S57subf.CCNC));
126 private static ArrayList<S57subf> S57sg2d = new ArrayList<>(Arrays.asList(S57subf.YCOO, S57subf.XCOO));
127 private static ArrayList<S57subf> S57sg3d = new ArrayList<>(Arrays.asList(S57subf.YCOO, S57subf.XCOO, S57subf.VE3D));
128 private static ArrayList<S57subf> S57arcc = new ArrayList<>(Arrays.asList(S57subf.ATYP, S57subf.SURF, S57subf.ORDR, S57subf.RESO, S57subf.FPMF));
129 private static ArrayList<S57subf> S57ar2d = new ArrayList<>(Arrays.asList(S57subf.STPT, S57subf.CTPT, S57subf.ENPT, S57subf.YCOO, S57subf.XCOO));
130 private static ArrayList<S57subf> S57el2d = new ArrayList<>(Arrays.asList(S57subf.STPT, S57subf.CTPT, S57subf.ENPT, S57subf.CDPM, S57subf.CDPR, S57subf.YCOO, S57subf.XCOO));
131 private static ArrayList<S57subf> S57ct2d = new ArrayList<>(Arrays.asList(S57subf.YCOO, S57subf.XCOO));
132
133 private static final EnumMap<S57field, ArrayList<S57subf>> fields = new EnumMap<>(S57field.class);
134 static {
135 fields.put(S57field.I8RI, S57i8ri);
136 fields.put(S57field.DSID, S57dsid); fields.put(S57field.DSSI, S57dssi); fields.put(S57field.DSPM, S57dspm); fields.put(S57field.DSPR, S57dspr);
137 fields.put(S57field.DSRC, S57dsrc); fields.put(S57field.DSHT, S57dsht); fields.put(S57field.DSAC, S57dsac); fields.put(S57field.CATD, S57catd);
138 fields.put(S57field.CATX, S57catx); fields.put(S57field.DDDF, S57dddf); fields.put(S57field.DDDR, S57dddr); fields.put(S57field.DDDI, S57dddi);
139 fields.put(S57field.DDOM, S57ddom); fields.put(S57field.DDRF, S57ddrf); fields.put(S57field.DDSI, S57ddsi); fields.put(S57field.DDSC, S57ddsc);
140 fields.put(S57field.FRID, S57frid); fields.put(S57field.FOID, S57foid); fields.put(S57field.LNAM, S57lnam); fields.put(S57field.ATTF, S57attf);
141 fields.put(S57field.NATF, S57natf); fields.put(S57field.FFPC, S57ffpc); fields.put(S57field.FFPT, S57ffpt); fields.put(S57field.FFPC, S57fspc);
142 fields.put(S57field.FSPT, S57fspt); fields.put(S57field.VRID, S57vrid); fields.put(S57field.ATTV, S57attv); fields.put(S57field.VRPC, S57vrpc);
143 fields.put(S57field.VRPT, S57vrpt); fields.put(S57field.SGCC, S57sgcc); fields.put(S57field.SG2D, S57sg2d); fields.put(S57field.SG3D, S57sg3d);
144 fields.put(S57field.ARCC, S57arcc); fields.put(S57field.AR2D, S57ar2d); fields.put(S57field.EL2D, S57el2d); fields.put(S57field.CT2D, S57ct2d);
145 }
146
147 private static byte[] leader = {'0', '0', '0', '0', '0', ' ', 'D', ' ', ' ', ' ', ' ', ' ', '0', '0', '0', '0', '0', ' ', ' ', ' ', '0', '0', '0', '4'};
148 private static byte[] buffer;
149 private static int offset;
150 private static int maxoff;
151 private static int index;
152 private static S57field field;
153 private static String aall = "US-ASCII";
154 private static String nall = "US-ASCII";
155 public static int rnum;
156
157 private static S57conv findSubf(S57subf subf) {
158 ArrayList<S57subf> subs = fields.get(field);
159 boolean wrap = false;
160 while (true) {
161 if (index == subs.size()) {
162 if (!wrap) {
163 index = 0;
164 wrap = true;
165 } else {
166 System.out.println("ERROR: Subfield not found " + subf.name() + " in " + field.name() + " in record " + rnum);
167 System.exit(-1);
168 }
169 }
170 S57subf sub = subs.get(index++);
171 S57conv conv = convs.get(sub);
172 if (sub == subf) {
173 return conv;
174 } else {
175 offset += (conv.bin != 0) ? ((conv.bin < 8) ? Math.abs(conv.bin) : conv.bin / 8) : conv.asc;
176 }
177 }
178 }
179
180 public static void setField(byte[] buf, int off, S57field fld, int len) {
181 buffer = buf;
182 offset = off;
183 maxoff = off + len - 1;
184 field = fld;
185 index = 0;
186 }
187
188 public static boolean more() {
189 return (offset < maxoff);
190 }
191
192 public static Object decSubf(byte[] buf, int off, S57field fld, S57subf subf) {
193 buffer = buf;
194 offset = off;
195 index = 0;
196 return decSubf(fld, subf);
197 }
198
199 public static Object decSubf(S57field fld, S57subf subf) {
200 field = fld;
201 index = 0;
202 return decSubf(subf);
203 }
204
205 public static Object decSubf(S57subf subf) {
206 S57conv conv = findSubf(subf);
207 if (conv.bin == 0) {
208 String str = "";
209 int i = 0;
210 if (conv.asc == 0) {
211 for (i = 0; buffer[offset+i] != 0x1f; i++) { }
212 try {
213 String charset = "";
214 if (field == S57field.ATTF) charset = aall;
215 else if (field == S57field.NATF) charset = nall;
216 else charset = "US-ASCII";
217 str = new String(buffer, offset, i, charset);
218 } catch (UnsupportedEncodingException e) {
219 System.err.println(e.getMessage());
220 System.exit(-1);
221 }
222 offset += i + 1;
223 } else {
224 str = new String(buffer, offset, conv.asc);
225 offset += conv.asc;
226 }
227 return str;
228 } else {
229 int f = Math.abs(conv.bin);
230 if (f < 5) {
231 long val = buffer[offset + --f];
232 if (conv.bin > 0)
233 val &= 0xff;
234 while (f > 0) {
235 val = (val << 8) + (buffer[offset + --f] & 0xff);
236 }
237 offset += Math.abs(conv.bin);
238 if ((subf == S57subf.AALL) || (subf == S57subf.NALL)) {
239 String charset = "";
240 switch ((int) val) {
241 case 0:
242 charset = "US-ASCII";
243 break;
244 case 1:
245 charset = "ISO-8859-1";
246 break;
247 case 2:
248 charset = "UTF-16LE";
249 break;
250 }
251 if (subf == S57subf.NALL) {
252 nall = charset;
253 } else {
254 aall = charset;
255 }
256 }
257 return val;
258 } else {
259 if (f == 5) {
260 long val = buffer[offset++] & 0xff;
261 f--;
262 while (f > 0) {
263 val = (val << 8) + (buffer[offset + --f] & 0xff);
264 }
265 offset += 4;
266 return val;
267 } else {
268 long val = buffer[offset++] & 0xff;
269 val = (val << 8) + (buffer[offset++] & 0xff);
270 f = 4;
271 while (f > 0) {
272 val = (val << 8) + (buffer[offset + --f] & 0xff);
273 }
274 offset += 4;
275 f = 2;
276 while (f > 0) {
277 val = (val << 8) + (buffer[offset + --f] & 0xff);
278 }
279 offset += 2;
280 return val;
281 }
282 }
283 }
284 }
285
286 public static byte[] encSubf(S57subf subf, Object val) {
287 S57conv conv = convs.get(subf);
288 if ((conv.bin == 0) || asc) {
289 String sval = "";
290 if (val instanceof String) {
291 sval = (String) val;
292 } else if (val instanceof Integer) {
293 sval = ((Integer) val).toString();
294 } else if (val instanceof Long) {
295 sval = ((Long) val).toString();
296 } else if (val instanceof Double) {
297 sval = ((Double) val).toString();
298 }
299 index = sval.length();
300 try {
301 buffer = (sval + " ").getBytes("ISO-8859-1");
302 } catch (Exception e) {
303 System.err.println(e.getMessage());
304 System.exit(-1);
305 }
306 if (conv.asc == 0) {
307 buffer[index] = 0x01f;
308 } else {
309 buffer = Arrays.copyOf(buffer, conv.asc);
310 while (index < buffer.length) {
311 buffer[index++] = ' ';
312 }
313 }
314 } else {
315 int f = Math.abs(conv.bin);
316 long lval;
317 if (val instanceof String) {
318 lval = Long.parseLong((String) val);
319 } else if (val instanceof Double) {
320 double dval = (double) val;
321 lval = (long) dval;
322 } else if (val instanceof Integer) {
323 lval = (int) val;
324 } else {
325 lval = (long) val;
326 }
327 buffer = new byte[f];
328 for (int i = 0; i < f; i++) {
329 buffer[i] = (byte) (lval & 0xff);
330 lval >>= 8;
331 }
332 }
333 return buffer;
334 }
335
336 static class Index {
337 byte[] field;
338 int length;
339 int offset;
340 Index(byte[] id, int l, int o) {
341 field = id;
342 length = l;
343 offset = o;
344 }
345 }
346
347 public static class Fparams {
348 public S57field field;
349 public Object[] params;
350 public Fparams(S57field f, Object[] p) {
351 field = f;
352 params = p;
353 }
354 }
355
356 static boolean asc = false;
357
358 public static byte[] encRecord(String i8rn, ArrayList<Fparams> fparams) {
359 asc = true;
360 return encRecord(Integer.parseInt(i8rn), fparams);
361 }
362
363 public static byte[] encRecord(int i8rn, ArrayList<Fparams> fparams) {
364 ArrayList<Index> index = new ArrayList<>();
365 int offset = 3;
366 int maxlen = 3;
367 byte[] buf = encSubf(S57subf.I8RN, i8rn);
368 buf = Arrays.copyOf(buf, 3);
369 buf[2] = 0x1e;
370 index.add(new Index("0001".getBytes(), 3, 0));
371 for (Fparams sfparams : fparams) {
372 for (int ip = 0; ip < sfparams.params.length;) {
373 for (S57subf subf : fields.get(sfparams.field)) {
374 byte[] next = encSubf(subf, sfparams.params[ip++]);
375 buf = Arrays.copyOf(buf, (buf.length + next.length));
376 System.arraycopy(next, 0, buf, (buf.length - next.length), next.length);
377 }
378 }
379 buf = Arrays.copyOf(buf, (buf.length + 1));
380 buf[buf.length-1] = 0x1e;
381 int flen = buf.length - offset;
382 index.add(new Index(sfparams.field.toString().getBytes(StandardCharsets.UTF_8), flen, offset));
383 maxlen = (flen > maxlen) ? flen : maxlen;
384 offset += flen;
385 }
386 int mlen = String.valueOf(maxlen).length();
387 String ffmt = "%0" + mlen + "d";
388 int olen = String.valueOf(offset).length();
389 String ofmt = "%0" + olen + "d";
390 int ilen = 4 + mlen + olen;
391 int isiz = (ilen * index.size()) + 1;
392 byte[] ibuf = new byte[isiz];
393 int i = 0;
394 for (Index item : index) {
395 for (byte ch : item.field) {
396 ibuf[i++] = ch;
397 }
398 byte[] digits = String.format(ffmt, item.length).getBytes();
399 for (byte ch : digits) {
400 ibuf[i++] = ch;
401 }
402 digits = String.format(ofmt, item.offset).getBytes();
403 for (byte ch : digits) {
404 ibuf[i++] = ch;
405 }
406 }
407 ibuf[i] = 0x1e;
408 byte[] fbuf = Arrays.copyOf(leader, (leader.length + ibuf.length + buf.length));
409 System.arraycopy(ibuf, 0, fbuf, leader.length, ibuf.length);
410 System.arraycopy(buf, 0, fbuf, (leader.length + ibuf.length), buf.length);
411 fbuf[20] = (byte) (mlen + 0x30);
412 fbuf[21] = (byte) (olen + 0x30);
413 System.arraycopy(String.format("%05d", fbuf.length).getBytes(), 0, fbuf, 0, 5);
414 System.arraycopy(String.format("%05d", (leader.length + ibuf.length)).getBytes(), 0, fbuf, 12, 5);
415 asc = false;
416 return fbuf;
417 }
418
419 enum Prims { N, P, L, A, PA, PL, LA, PLA }
420
421 private static final EnumMap<Obj, Prims> S57prims = new EnumMap<>(Obj.class);
422 static {
423 S57prims.put(Obj.UNKOBJ, Prims.PLA); S57prims.put(Obj.M_COVR, Prims.A); S57prims.put(Obj.M_NSYS, Prims.A); S57prims.put(Obj.AIRARE, Prims.PA);
424 S57prims.put(Obj.ACHBRT, Prims.PA); S57prims.put(Obj.ACHARE, Prims.PA); S57prims.put(Obj.BCNCAR, Prims.P); S57prims.put(Obj.BCNISD, Prims.P);
425 S57prims.put(Obj.BCNLAT, Prims.P); S57prims.put(Obj.BCNSAW, Prims.P); S57prims.put(Obj.BCNSPP, Prims.P); S57prims.put(Obj.BERTHS, Prims.PLA);
426 S57prims.put(Obj.BRIDGE, Prims.PLA); S57prims.put(Obj.BUISGL, Prims.PA); S57prims.put(Obj.BUAARE, Prims.PA); S57prims.put(Obj.BOYCAR, Prims.P);
427 S57prims.put(Obj.BOYINB, Prims.P); S57prims.put(Obj.BOYISD, Prims.P); S57prims.put(Obj.BOYLAT, Prims.P); S57prims.put(Obj.BOYSAW, Prims.P);
428 S57prims.put(Obj.BOYSPP, Prims.P); S57prims.put(Obj.CBLARE, Prims.A); S57prims.put(Obj.CBLOHD, Prims.L); S57prims.put(Obj.CBLSUB, Prims.L);
429 S57prims.put(Obj.CANALS, Prims.A); S57prims.put(Obj.CTSARE, Prims.PA); S57prims.put(Obj.CAUSWY, Prims.LA); S57prims.put(Obj.CTNARE, Prims.PA);
430 S57prims.put(Obj.CHKPNT, Prims.PA); S57prims.put(Obj.CGUSTA, Prims.P); S57prims.put(Obj.COALNE, Prims.L); S57prims.put(Obj.CONZNE, Prims.A);
431 S57prims.put(Obj.COSARE, Prims.A); S57prims.put(Obj.CTRPNT, Prims.P); S57prims.put(Obj.CONVYR, Prims.LA); S57prims.put(Obj.CRANES, Prims.PA);
432 S57prims.put(Obj.CURENT, Prims.P); S57prims.put(Obj.CUSZNE, Prims.A); S57prims.put(Obj.DAMCON, Prims.LA); S57prims.put(Obj.DAYMAR, Prims.P);
433 S57prims.put(Obj.DWRTCL, Prims.L); S57prims.put(Obj.DWRTPT, Prims.A); S57prims.put(Obj.DEPARE, Prims.A); S57prims.put(Obj.DEPCNT, Prims.L);
434 S57prims.put(Obj.DISMAR, Prims.P); S57prims.put(Obj.DOCARE, Prims.A); S57prims.put(Obj.DRGARE, Prims.A); S57prims.put(Obj.DRYDOC, Prims.A);
435 S57prims.put(Obj.DMPGRD, Prims.PA); S57prims.put(Obj.DYKCON, Prims.L); S57prims.put(Obj.EXEZNE, Prims.A); S57prims.put(Obj.FAIRWY, Prims.A);
436 S57prims.put(Obj.FNCLNE, Prims.L); S57prims.put(Obj.FERYRT, Prims.LA); S57prims.put(Obj.FSHZNE, Prims.A); S57prims.put(Obj.FSHFAC, Prims.PLA);
437 S57prims.put(Obj.FSHGRD, Prims.A); S57prims.put(Obj.FLODOC, Prims.A); S57prims.put(Obj.FOGSIG, Prims.P); S57prims.put(Obj.FORSTC, Prims.PLA);
438 S57prims.put(Obj.FRPARE, Prims.A); S57prims.put(Obj.GATCON, Prims.PLA); S57prims.put(Obj.GRIDRN, Prims.PA); S57prims.put(Obj.HRBARE, Prims.A);
439 S57prims.put(Obj.HRBFAC, Prims.PA); S57prims.put(Obj.HULKES, Prims.PA); S57prims.put(Obj.ICEARE, Prims.A); S57prims.put(Obj.ICNARE, Prims.PA);
440 S57prims.put(Obj.ISTZNE, Prims.A); S57prims.put(Obj.LAKARE, Prims.A); S57prims.put(Obj.LNDARE, Prims.PLA); S57prims.put(Obj.LNDELV, Prims.PL);
441 S57prims.put(Obj.LNDRGN, Prims.PA); S57prims.put(Obj.LNDMRK, Prims.PLA); S57prims.put(Obj.LIGHTS, Prims.P); S57prims.put(Obj.LITFLT, Prims.P);
442 S57prims.put(Obj.LITVES, Prims.P); S57prims.put(Obj.LOCMAG, Prims.PLA); S57prims.put(Obj.LOKBSN, Prims.A); S57prims.put(Obj.LOGPON, Prims.PA);
443 S57prims.put(Obj.MAGVAR, Prims.PLA); S57prims.put(Obj.MARCUL, Prims.PLA); S57prims.put(Obj.MIPARE, Prims.PA); S57prims.put(Obj.MORFAC, Prims.PLA);
444 S57prims.put(Obj.MPAARE, Prims.PA); S57prims.put(Obj.NAVLNE, Prims.L); S57prims.put(Obj.OBSTRN, Prims.PLA); S57prims.put(Obj.OFSPLF, Prims.PA);
445 S57prims.put(Obj.OSPARE, Prims.A); S57prims.put(Obj.OILBAR, Prims.L); S57prims.put(Obj.PILPNT, Prims.P); S57prims.put(Obj.PILBOP, Prims.PA);
446 S57prims.put(Obj.PIPARE, Prims.PA); S57prims.put(Obj.PIPOHD, Prims.L); S57prims.put(Obj.PIPSOL, Prims.PL); S57prims.put(Obj.PONTON, Prims.LA);
447 S57prims.put(Obj.PRCARE, Prims.PA); S57prims.put(Obj.PRDARE, Prims.PA); S57prims.put(Obj.PYLONS, Prims.PA); S57prims.put(Obj.RADLNE, Prims.L);
448 S57prims.put(Obj.RADRNG, Prims.A); S57prims.put(Obj.RADRFL, Prims.P); S57prims.put(Obj.RADSTA, Prims.P); S57prims.put(Obj.RTPBCN, Prims.P);
449 S57prims.put(Obj.RDOCAL, Prims.PL); S57prims.put(Obj.RDOSTA, Prims.P); S57prims.put(Obj.RAILWY, Prims.L); S57prims.put(Obj.RAPIDS, Prims.PLA);
450 S57prims.put(Obj.RCRTCL, Prims.L); S57prims.put(Obj.RECTRC, Prims.LA); S57prims.put(Obj.RCTLPT, Prims.PA); S57prims.put(Obj.RSCSTA, Prims.P);
451 S57prims.put(Obj.RESARE, Prims.A); S57prims.put(Obj.RETRFL, Prims.P); S57prims.put(Obj.RIVERS, Prims.LA); S57prims.put(Obj.ROADWY, Prims.PLA);
452 S57prims.put(Obj.RUNWAY, Prims.PLA); S57prims.put(Obj.SNDWAV, Prims.PLA); S57prims.put(Obj.SEAARE, Prims.PA); S57prims.put(Obj.SPLARE, Prims.PA);
453 S57prims.put(Obj.SBDARE, Prims.PLA); S57prims.put(Obj.SLCONS, Prims.PLA); S57prims.put(Obj.SISTAT, Prims.P); S57prims.put(Obj.SISTAW, Prims.P);
454 S57prims.put(Obj.SILTNK, Prims.PA); S57prims.put(Obj.SLOTOP, Prims.L); S57prims.put(Obj.SLOGRD, Prims.PA); S57prims.put(Obj.SMCFAC, Prims.PA);
455 S57prims.put(Obj.SOUNDG, Prims.P); S57prims.put(Obj.SPRING, Prims.P); S57prims.put(Obj.STSLNE, Prims.L); S57prims.put(Obj.SUBTLN, Prims.A);
456 S57prims.put(Obj.SWPARE, Prims.A); S57prims.put(Obj.TESARE, Prims.A); S57prims.put(Obj.TS_PRH, Prims.PA); S57prims.put(Obj.TS_PNH, Prims.PA);
457 S57prims.put(Obj.TS_PAD, Prims.PA); S57prims.put(Obj.TS_TIS, Prims.PA); S57prims.put(Obj.T_HMON, Prims.PA); S57prims.put(Obj.T_NHMN, Prims.PA);
458 S57prims.put(Obj.T_TIMS, Prims.PA); S57prims.put(Obj.TIDEWY, Prims.LA); S57prims.put(Obj.TOPMAR, Prims.P); S57prims.put(Obj.TSELNE, Prims.LA);
459 S57prims.put(Obj.TSSBND, Prims.L); S57prims.put(Obj.TSSCRS, Prims.A); S57prims.put(Obj.TSSLPT, Prims.A); S57prims.put(Obj.TSSRON, Prims.A);
460 S57prims.put(Obj.TSEZNE, Prims.A); S57prims.put(Obj.TUNNEL, Prims.LA); S57prims.put(Obj.TWRTPT, Prims.A); S57prims.put(Obj.UWTROC, Prims.P);
461 S57prims.put(Obj.UNSARE, Prims.A); S57prims.put(Obj.VEGATN, Prims.PLA); S57prims.put(Obj.WATTUR, Prims.PLA); S57prims.put(Obj.WATFAL, Prims.PL);
462 S57prims.put(Obj.WEDKLP, Prims.PA); S57prims.put(Obj.WRECKS, Prims.PA); S57prims.put(Obj.TS_FEB, Prims.PA);
463 S57prims.put(Obj.NOTMRK, Prims.P); S57prims.put(Obj.WTWAXS, Prims.L); S57prims.put(Obj.WTWPRF, Prims.L); S57prims.put(Obj.BUNSTA, Prims.PA);
464 S57prims.put(Obj.COMARE, Prims.A); S57prims.put(Obj.HRBBSN, Prims.A); S57prims.put(Obj.LKBSPT, Prims.A); S57prims.put(Obj.PRTARE, Prims.A);
465 S57prims.put(Obj.REFDMP, Prims.P); S57prims.put(Obj.TERMNL, Prims.PA); S57prims.put(Obj.TRNBSN, Prims.PA); S57prims.put(Obj.WTWARE, Prims.A);
466 S57prims.put(Obj.WTWGAG, Prims.PA); S57prims.put(Obj.TISDGE, Prims.N); S57prims.put(Obj.VEHTRF, Prims.PA); S57prims.put(Obj.EXCNST, Prims.PA);
467 S57prims.put(Obj.LG_SDM, Prims.A); S57prims.put(Obj.LG_VSP, Prims.A); S57prims.put(Obj.LITMAJ, Prims.P); S57prims.put(Obj.LITMIN, Prims.P);
468 }
469
470 public static void S57geoms(S57map map) {
471 for (ArrayList<Feature> list : map.features.values()) {
472 for (Feature feature : list) {
473 switch (S57prims.get(feature.type)) {
474 case N:
475 break;
476 case P:
477 if (feature.geom.prim != Pflag.POINT) {
478 // Snode node = feature.geom.centre;
479 // node.flg = Nflag.ISOL;
480 // map.nodes.put(++map.xref, node);
481 // feature.geom = map.new Geom(Pflag.POINT);
482 // feature.geom.centre = node;
483 // feature.geom.elems.add(map.new Prim(map.xref));
484 }
485 break;
486 case L:
487 break;
488 case A:
489 break;
490 case PA:
491 break;
492 case PL:
493 break;
494 case LA:
495 if (feature.geom.prim == Pflag.POINT) {
496 // list.remove(feature);
497 }
498 break;
499 case PLA:
500 // No changes needed
501 break;
502 }
503 }
504 }
505 }
506}
Note: See TracBrowser for help on using the repository browser.