Ignore:
Timestamp:
2015-10-29T23:01:53+01:00 (10 years ago)
Author:
malcolmh
Message:

[seachart] update

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/seachart/src/s57/S57dat.java

    r31704 r31722  
    3232        static {
    3333                convs.put(S57subf.I8RN, new S57conv(5,2));
    34                 convs.put(S57subf.RCNM, new S57conv(2,1)); convs.put(S57subf.RCID, new S57conv(10,32)); convs.put(S57subf.EXPP, new S57conv(1,1));
     34                convs.put(S57subf.RCNM, new S57conv(2,1)); convs.put(S57subf.RCID, new S57conv(10,4)); convs.put(S57subf.EXPP, new S57conv(1,1));
    3535                convs.put(S57subf.INTU, new S57conv(1,1)); convs.put(S57subf.DSNM, new S57conv(0,0)); convs.put(S57subf.EDTN, new S57conv(0,0));
    3636                convs.put(S57subf.UPDN, new S57conv(0,0)); convs.put(S57subf.UADT, new S57conv(8,0)); convs.put(S57subf.ISDT, new S57conv(8,0));
     
    7878       
    7979        public enum S57field { I8RI, DSID, DSSI, DSPM, DSPR, DSRC, DSHT, DSAC, CATD, CATX, DDDF, DDDR, DDDI, DDOM, DDRF, DDSI, DDSC,
    80                 FRID, FOID, ATTF, NATF, FFPC, FFPT, FSPC, FSPT, VRID, ATTV, VRPC, VRPT, SGCC, SG2D, SG3D, ARCC, AR2D, EL2D, CT2D }
     80                FRID, FOID, LNAM, ATTF, NATF, FFPC, FFPT, FSPC, FSPT, VRID, ATTV, VRPC, VRPT, SGCC, SG2D, SG3D, ARCC, AR2D, EL2D, CT2D }
    8181       
    8282        private static ArrayList<S57subf> S57i8ri = new ArrayList<S57subf>(Arrays.asList(S57subf.I8RN));
     
    105105        private static ArrayList<S57subf> S57ddsc = new ArrayList<S57subf>(Arrays.asList(S57subf.ATLB, S57subf.ASET, S57subf.AUTH ));
    106106        private static ArrayList<S57subf> S57frid = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.PRIM, S57subf.GRUP, S57subf.OBJL, S57subf.RVER, S57subf.RUIN ));
    107 //      private static ArrayList<S57subf> S57foid = new ArrayList<S57subf>(Arrays.asList(S57subf.AGEN, S57subf.FIDN, S57subf.FIDS ));
    108         private static ArrayList<S57subf> S57foid = new ArrayList<S57subf>(Arrays.asList(S57subf.LNAM));
     107        private static ArrayList<S57subf> S57foid = new ArrayList<S57subf>(Arrays.asList(S57subf.AGEN, S57subf.FIDN, S57subf.FIDS ));
     108        private static ArrayList<S57subf> S57lnam = new ArrayList<S57subf>(Arrays.asList(S57subf.LNAM));
    109109        private static ArrayList<S57subf> S57attf = new ArrayList<S57subf>(Arrays.asList(S57subf.ATTL, S57subf.ATVL ));
    110110        private static ArrayList<S57subf> S57natf = new ArrayList<S57subf>(Arrays.asList(S57subf.ATTL, S57subf.ATVL ));
     
    132132                fields.put(S57field.CATX, S57catx); fields.put(S57field.DDDF, S57dddf); fields.put(S57field.DDDR, S57dddr); fields.put(S57field.DDDI, S57dddi);
    133133                fields.put(S57field.DDOM, S57ddom); fields.put(S57field.DDRF, S57ddrf); fields.put(S57field.DDSI, S57ddsi); fields.put(S57field.DDSC, S57ddsc);
    134                 fields.put(S57field.FRID, S57frid); fields.put(S57field.FOID, S57foid); fields.put(S57field.ATTF, S57attf); fields.put(S57field.NATF, S57natf);
    135                 fields.put(S57field.FFPC, S57ffpc); fields.put(S57field.FFPT, S57ffpt); fields.put(S57field.FFPC, S57fspc); fields.put(S57field.FSPT, S57fspt);
    136                 fields.put(S57field.VRID, S57vrid); fields.put(S57field.ATTV, S57attv); fields.put(S57field.VRPC, S57vrpc); fields.put(S57field.VRPT, S57vrpt);
    137                 fields.put(S57field.SGCC, S57sgcc); fields.put(S57field.SG2D, S57sg2d); fields.put(S57field.SG3D, S57sg3d); fields.put(S57field.ARCC, S57arcc);
    138                 fields.put(S57field.AR2D, S57ar2d); fields.put(S57field.EL2D, S57el2d); fields.put(S57field.CT2D, S57ct2d);
    139         }
    140 
     134                fields.put(S57field.FRID, S57frid); fields.put(S57field.FOID, S57foid); fields.put(S57field.LNAM, S57lnam); fields.put(S57field.ATTF, S57attf);
     135                fields.put(S57field.NATF, S57natf); fields.put(S57field.FFPC, S57ffpc); fields.put(S57field.FFPT, S57ffpt); fields.put(S57field.FFPC, S57fspc);
     136                fields.put(S57field.FSPT, S57fspt); fields.put(S57field.VRID, S57vrid); fields.put(S57field.ATTV, S57attv); fields.put(S57field.VRPC, S57vrpc);
     137                fields.put(S57field.VRPT, S57vrpt); fields.put(S57field.SGCC, S57sgcc); fields.put(S57field.SG2D, S57sg2d); fields.put(S57field.SG3D, S57sg3d);
     138                fields.put(S57field.ARCC, S57arcc); fields.put(S57field.AR2D, S57ar2d); fields.put(S57field.EL2D, S57el2d); fields.put(S57field.CT2D, S57ct2d);
     139        }
     140
     141        public enum S57record { DS, DP, DH, DA, CD, CR, ID, IO, IS, FE, VS, VI, VC, VE }
     142       
     143        private static ArrayList<S57field> S57ds = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.DSID, S57field.DSSI ));
     144        private static ArrayList<S57field> S57dp = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.DSPM, S57field.DSPR, S57field.DSRC ));
     145        private static ArrayList<S57field> S57dh = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.DSHT ));
     146        private static ArrayList<S57field> S57da = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.DSAC ));
     147        private static ArrayList<S57field> S57cd = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.CATD ));
     148        private static ArrayList<S57field> S57cr = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.CATX ));
     149        private static ArrayList<S57field> S57id = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.DDDF, S57field.DDDR ));
     150        private static ArrayList<S57field> S57io = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.DDDI, null, S57field.DDOM, S57field.DDRF, null ));
     151        private static ArrayList<S57field> S57is = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.DDSI, S57field.DDSC ));
     152        private static ArrayList<S57field> S57fe = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.FRID, S57field.FOID, null, S57field.ATTF, null,
     153                        null, S57field.NATF, null, null, S57field.FFPT, null, null, S57field.FSPT, null ));
     154        private static ArrayList<S57field> S57vs = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.VRID, null, S57field.SG3D, null));
     155        private static ArrayList<S57field> S57vi = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.VRID, S57field.SG2D));
     156        private static ArrayList<S57field> S57vc = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.VRID, S57field.SG2D));
     157        private static ArrayList<S57field> S57ve = new ArrayList<S57field>(Arrays.asList(S57field.I8RI, S57field.VRID, null, S57field.VRPT, null, null, S57field.SG3D, null));
     158       
     159        private static final EnumMap<S57record, ArrayList<S57field>> records = new EnumMap<S57record, ArrayList<S57field>>(S57record.class);
     160        static {
     161                records.put(S57record.DS, S57ds); records.put(S57record.DP, S57dp); records.put(S57record.DH, S57dh); records.put(S57record.DA, S57da); records.put(S57record.CD, S57cd);
     162                records.put(S57record.CR, S57cr); records.put(S57record.ID, S57id); records.put(S57record.IO, S57io); records.put(S57record.IS, S57is); records.put(S57record.FE, S57fe);
     163                records.put(S57record.VS, S57vs); records.put(S57record.VI, S57vi); records.put(S57record.VC, S57vc); records.put(S57record.VE, S57ve);
     164        }
     165       
     166        private static final EnumMap<S57record, Integer> rcnms = new EnumMap<S57record, Integer>(S57record.class);
     167        static {
     168                rcnms.put(S57record.DS, 10); rcnms.put(S57record.DP, 20); rcnms.put(S57record.DH, 30); rcnms.put(S57record.DA, 40); rcnms.put(S57record.CD, 50);
     169                rcnms.put(S57record.CR, 60); rcnms.put(S57record.ID, 70); rcnms.put(S57record.IO, 80); rcnms.put(S57record.IS, 90); rcnms.put(S57record.FE, 100);
     170                rcnms.put(S57record.VS, 110); rcnms.put(S57record.VI, 110); rcnms.put(S57record.VC, 120); rcnms.put(S57record.VE, 130);
     171        }
     172       
     173        private static byte[] leader = {'0', '0', '0', '0', '0', ' ', 'D', ' ', ' ', ' ', ' ', ' ', '0', '0', '0', '0', '0', ' ', ' ', ' ', '0', '0', '0', '4'};
    141174        private static byte[] buffer;
    142175        private static int offset;
     
    259292        }
    260293
    261         public static byte[] encSubf(S57subf subf, Object val) throws UnsupportedEncodingException {
    262                 S57conv conv = findSubf(subf);
    263                 if ((conv.bin == 0) && (val instanceof String)) {
    264                         index = ((String)val).length();
    265                         buffer = ((String)val + " ").getBytes("ISO-8859-1");
     294        public static byte[] encSubf(S57subf subf, Object val) {
     295                if (val instanceof S57record) val = rcnms.get(val);
     296                S57conv conv = convs.get(subf);
     297                if (conv.bin == 0) {
     298                        String sval = "";
     299                        if (val instanceof String) {
     300                                sval = (String) val;
     301                        } else if (val instanceof Long) {
     302                                sval = ((Long)val).toString();
     303                        } else if (val instanceof Double) {
     304                                sval = ((Double)val).toString();
     305                        }
     306                        index = sval.length();
     307                        try {
     308                                buffer = ((String) sval + " ").getBytes("ISO-8859-1");
     309                        } catch (Exception e) {
     310                                System.err.println(e.getMessage());
     311                                System.exit(-1);
     312                        }
    266313                        if (conv.asc == 0) {
    267314                                buffer[index] = 0x01f;
     
    274321                } else {
    275322                        int f = Math.abs(conv.bin);
    276                         long lval = (long) val;
     323                        long lval;
     324                        if (val instanceof String) {
     325                                lval = Long.parseLong((String)val);
     326                        } else  if (val instanceof Double) {
     327                                double dval = (double) val;
     328                                lval = (long) dval;
     329                        } else  if (val instanceof Integer) {
     330                                lval = (int) val;
     331                        } else {
     332                                lval = (long) val;
     333                        }
    277334                        if (f < 8) {
    278335                                buffer = new byte[f];
     
    284341                                f /= 8;
    285342                                buffer = new byte[f];
    286                                 for (int i = f-1; i <= 0; i++) {
     343                                for (int i = f-1; i >= 0; i--) {
    287344                                        buffer[i] = (byte) (lval & 0xff);
    288345                                        lval >>= 8;
     
    292349                return buffer;
    293350        }
    294        
     351
     352        static class Index {
     353                byte[] field;
     354                int length;
     355                int offset;
     356                Index (byte[] id, int l, int o) {
     357                        field = id;
     358                        length = l;
     359                        offset = o;
     360                }
     361        }
     362       
     363        public static byte[] encRecord(Object...params) {
     364                ArrayList<Index> index = new ArrayList<Index>();
     365                offset = 0;
     366                int maxlen = 0;
     367                ArrayList<S57field> record = records.get(params[1]);
     368                byte[] buf = new byte[0];
     369                int ip = 0;
     370                for (S57field field : record) {
     371                        for (S57subf subf : fields.get(field)) {
     372                                byte[] next = encSubf(subf, params[ip++]);
     373                                buf = Arrays.copyOf(buf, (buf.length + next.length));
     374                                System.arraycopy(next, 0, buf, (buf.length - next.length), next.length);
     375                                if (ip >= params.length)
     376                                        break;
     377                        }
     378                        buf = Arrays.copyOf(buf, (buf.length + 1));
     379                        buf[buf.length-1] = 0x1e;
     380                        int flen = buf.length - offset;
     381                        if (field == S57field.I8RI) {
     382                                index.add(new Index("0001".getBytes(), flen, offset));
     383                        } else {
     384                                index.add(new Index(field.toString().getBytes(), flen, offset));
     385                        }
     386                        maxlen = (flen > maxlen) ? flen : maxlen;
     387                        offset += flen;
     388                }
     389                int mlen = String.valueOf(maxlen).length();
     390                String ffmt = "%0" + mlen + "d";
     391                int olen = String.valueOf(offset).length();
     392                String ofmt = "%0" + olen + "d";
     393                int ilen = 4 + mlen + olen;
     394                int isiz = (ilen * index.size()) + 1;
     395                byte[] ibuf = new byte[isiz];
     396                int i = 0;
     397                for (Index item : index) {
     398                        for (byte ch : item.field) {
     399                                ibuf[i++] = ch;
     400                        }
     401                        byte[] digits = String.format(ffmt, item.length).getBytes();
     402                        for (byte ch : digits) {
     403                                ibuf[i++] = ch;
     404                        }
     405                        digits = String.format(ofmt, item.offset).getBytes();
     406                        for (byte ch : digits) {
     407                                ibuf[i++] = ch;
     408                        }
     409                }
     410                ibuf[i] = 0x1e;
     411                byte[] fbuf = Arrays.copyOf(leader, (leader.length + ibuf.length + buf.length));
     412                System.arraycopy(ibuf, 0, fbuf, leader.length, ibuf.length);
     413                System.arraycopy(buf, 0, fbuf, (leader.length + ibuf.length), buf.length);
     414                fbuf[20] = (byte)(mlen + 0x30);
     415                fbuf[21] = (byte)(olen + 0x30);
     416                System.arraycopy(String.format("%05d", fbuf.length).getBytes(), 0, fbuf, 0, 5);
     417                System.arraycopy(String.format("%05d", (leader.length + ibuf.length)).getBytes(), 0, fbuf, 12, 5);
     418                return fbuf;
     419        }
     420
    295421}
Note: See TracChangeset for help on using the changeset viewer.