Changeset 32394 in osm for applications/editors/josm/plugins/seachart/src/s57/S57dec.java
- Timestamp:
- 2016-06-24T03:48:12+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/seachart/src/s57/S57dec.java
r32380 r32394 13 13 import java.io.IOException; 14 14 15 import s57.S57dat.*; 16 import s57.S57map.*; 15 import s57.S57dat.S57field; 16 import s57.S57dat.S57subf; 17 import s57.S57map.Nflag; 17 18 18 19 public class S57dec { // S57 ENC file input & map conversion 19 20 public static void decodeChart(FileInputStream in, S57map map) throws IOException { 21 S57dat.rnum = 0; 22 byte[] leader = new byte[24]; 23 byte[] record = new byte[0]; 24 boolean ddr = false; 25 int length = 0; 26 int fields = 0;; 27 int mapfl, mapfp, mapts, entry; 28 String tag; 29 int len; 30 int pos; 31 boolean inFeature = false; 32 33 double comf = 1; 34 double somf = 1; 35 long name = 0; 36 S57map.Nflag nflag = Nflag.ANON; 37 S57map.Pflag pflag = S57map.Pflag.NOSP; 38 long objl = 0; 39 40 while (in.read(leader) == 24) { 41 try { 42 length = Integer.parseInt(new String(leader, 0, 5)) - 24; 43 record = new byte[length]; 44 ddr = (leader[6] == 'L'); 45 fields = Integer.parseInt(new String(leader, 12, 5)) - 24; 46 } catch (Exception e) { 47 System.err.println("Invalid file format - Encrypted/compressed ENC file?"); 48 System.exit(-1); 49 } 50 mapfl = leader[20] - '0'; 51 mapfp = leader[21] - '0'; 52 mapts = leader[23] - '0'; 53 entry = mapfl + mapfp + mapts; 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 (tag.toString()) { 62 case "0001": 63 int i8rn = ((Long) S57dat.decSubf(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.decSubf(record, fields + pos, S57field.DSSI, S57subf.AALL); 72 S57dat.decSubf(S57subf.NALL); 73 break; 74 case "DSPM": 75 comf = (double) (Long) S57dat.decSubf(record, fields + pos, S57field.DSPM, S57subf.COMF); 76 somf = (double) (Long) S57dat.decSubf(S57subf.SOMF); 77 break; 78 case "FRID": 79 inFeature = true; 80 switch (((Long)S57dat.decSubf(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.decSubf(S57subf.OBJL); 94 break; 95 case "FOID": 96 name = (long) S57dat.decSubf(record, fields + pos, S57field.LNAM, 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.decSubf(S57subf.ATTL); 103 String atvl = ((String) S57dat.decSubf(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.decSubf(S57subf.LNAM); 113 int rind = ((Long) S57dat.decSubf(S57subf.RIND)).intValue(); 114 S57dat.decSubf(S57subf.COMT); 115 map.refObj(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.decSubf(S57subf.NAME) << 16; 122 map.newPrim(name, (Long) S57dat.decSubf(S57subf.ORNT), (Long) S57dat.decSubf(S57subf.USAG)); 123 S57dat.decSubf(S57subf.MASK); 124 } while (S57dat.more()); 125 break; 126 case "VRID": 127 inFeature = false; 128 name = (long) (Long)S57dat.decSubf(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.decSubf(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.decSubf(S57subf.NAME) << 16; 151 int topi = ((Long) S57dat.decSubf(S57subf.TOPI)).intValue(); 152 map.addConn(conn, topi); 153 S57dat.decSubf(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.decSubf(S57subf.YCOO)) / comf; 160 double lon = (double) ((Long) S57dat.decSubf(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 lat = Math.toRadians(lat); 167 lon = Math.toRadians(lon); 168 if (lat < map.bounds.minlat) 169 map.bounds.minlat = lat; 170 if (lat > map.bounds.maxlat) 171 map.bounds.maxlat = lat; 172 if (lon < map.bounds.minlon) 173 map.bounds.minlon = lon; 174 if (lon > map.bounds.maxlon) 175 map.bounds.maxlon = lon; 176 } while (S57dat.more()); 177 break; 178 case "SG3D": 179 S57dat.setField(record, fields + pos, S57field.SG3D, len); 180 do { 181 double lat = (double) ((Long) S57dat.decSubf(S57subf.YCOO)) / comf; 182 double lon = (double) ((Long) S57dat.decSubf(S57subf.XCOO)) / comf; 183 double depth = (double) ((Long) S57dat.decSubf(S57subf.VE3D)) / somf; 184 map.newNode(name++, lat, lon, depth); 185 lat = Math.toRadians(lat); 186 lon = Math.toRadians(lon); 187 if (lat < map.bounds.minlat) 188 map.bounds.minlat = lat; 189 if (lat > map.bounds.maxlat) 190 map.bounds.maxlat = lat; 191 if (lon < map.bounds.minlon) 192 map.bounds.minlon = lon; 193 if (lon > map.bounds.maxlon) 194 map.bounds.maxlon = lon; 195 } while (S57dat.more()); 196 break; 197 default: 198 break; 199 } 200 } 201 if (inFeature) { 202 map.endFeature(); 203 inFeature = false; 204 } 205 } 206 } 207 map.endFile(); 208 in.close(); 209 210 return; 211 } 212 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 213 215 }
Note:
See TracChangeset
for help on using the changeset viewer.
