1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package josmtos57;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileInputStream;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.FileReader;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.UnsupportedEncodingException;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.Scanner;
|
---|
15 | import java.util.zip.CRC32;
|
---|
16 |
|
---|
17 | import s57.S57dat;
|
---|
18 | import s57.S57dat.Fparams;
|
---|
19 | import s57.S57dat.S57field;
|
---|
20 | import s57.S57enc;
|
---|
21 | import s57.S57map;
|
---|
22 | import s57.S57osm;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @author Malcolm Herring
|
---|
26 | */
|
---|
27 | public final class Josmtos57 {
|
---|
28 | private Josmtos57() {
|
---|
29 | // Hide default constructor for utilities classes
|
---|
30 | }
|
---|
31 |
|
---|
32 | // http://opendatacommons.org/licenses/odbl/1-0/
|
---|
33 |
|
---|
34 | /*
|
---|
35 | URL website = new URL("http://www.website.com/information.asp");
|
---|
36 | try (InputStream in = website.openStream()) { Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING); }
|
---|
37 | */
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * To do:
|
---|
41 | * 1. Geometry truncation at cell boundary.
|
---|
42 | * 2. Geometry validation/correction to comply with S57 limitations.
|
---|
43 | * 3. Improvements in mapping of OSM features to S57 objects.
|
---|
44 | */
|
---|
45 |
|
---|
46 | static byte[] header = {
|
---|
47 | // CHECKSTYLE.OFF: LineLength
|
---|
48 | '0', '0', '2', '6', '2', '3', 'L', 'E', '1', ' ', '0', '9', '0', '0', '0', '7', '3', ' ', ' ', ' ', '6', '6', '0', '4', '0', '0', '0', '0', '0', '0', '0', '0',
|
---|
49 | '1', '9', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '4', '8', '0', '0', '0', '0', '1', '9', 'C', 'A', 'T', 'D', '0', '0', '0', '1',
|
---|
50 | '2', '2', '0', '0', '0', '0', '6', '7', 0x1e, '0', '0', '0', '0', ';', '&', ' ', ' ', ' ', 0x1f, '0', '0', '0', '1', 'C', 'A', 'T', 'D', 0x1e, '0', '1', '0', '0',
|
---|
51 | ';', '&', ' ', ' ', ' ', 'I', 'S', 'O', '/', 'I', 'E', 'C', ' ', '8', '2', '1', '1', ' ', 'R', 'e', 'c', 'o', 'r', 'd', ' ', 'I', 'd', 'e', 'n', 't', 'i', 'f',
|
---|
52 | 'i', 'e', 'r', 0x1f, 0x1f, '(', 'I', '(', '5', ')', ')', 0x1e, '1', '6', '0', '0', ';', '&', ' ', ' ', ' ', 'C', 'a', 't', 'a', 'l', 'o', 'g', 'u', 'e', ' ', 'D',
|
---|
53 | 'i', 'r', 'e', 'c', 't', 'o', 'r', 'y', ' ', 'F', 'i', 'e', 'l', 'd', 0x1f, 'R', 'C', 'N', 'M', '!', 'R', 'C', 'I', 'D', '!', 'F', 'I', 'L', 'E', '!', 'L', 'F',
|
---|
54 | 'I', 'L', '!', 'V', 'O', 'L', 'M', '!', 'I', 'M', 'P', 'L', '!', 'S', 'L', 'A', 'T', '!', 'W', 'L', 'O', 'N', '!', 'N', 'L', 'A', 'T', '!', 'E', 'L', 'O', 'N',
|
---|
55 | '!', 'C', 'R', 'C', 'S', '!', 'C', 'O', 'M', 'T', 0x1f, '(', 'A', '(', '2', ')', ',', 'I', '(', '1', '0', ')', ',', '3', 'A', ',', 'A', '(', '3', ')', ',', '4',
|
---|
56 | 'R', ',', '2', 'A', ')', 0x1e,
|
---|
57 | '0', '0', '1', '0', '1', ' ', 'D', ' ', ' ', ' ', ' ', ' ', '0', '0', '0', '5', '3', ' ', ' ', ' ', '5', '5', '0', '4',
|
---|
58 | '0', '0', '0', '1', '0', '0', '0', '0', '6', '0', '0', '0', '0', '0', 'C', 'A', 'T', 'D', '0', '0', '0', '4', '2', '0', '0', '0', '0', '6', 0x1e,
|
---|
59 | '0', '0', '0', '0', '0', 0x1e, 'C', 'D', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', 'C', 'A', 'T', 'A', 'L', 'O', 'G', '.', '0', '3', '1', 0x1f,
|
---|
60 | 0x1f, 'V', '0', '1', 'X', '0', '1', 0x1f, 'A', 'S', 'C', 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1e
|
---|
61 | // CHECKSTYLE.ON: LineLength
|
---|
62 | };
|
---|
63 |
|
---|
64 | static BufferedReader in;
|
---|
65 | static FileOutputStream out;
|
---|
66 | static S57map map;
|
---|
67 | static byte[] buf;
|
---|
68 | static HashMap<String, String> meta;
|
---|
69 | static ArrayList<Fparams> fields;
|
---|
70 | static byte[] record;
|
---|
71 |
|
---|
72 | public static void main(String[] args) throws IOException {
|
---|
73 |
|
---|
74 | map = new S57map(false);
|
---|
75 | int idx = 0;
|
---|
76 |
|
---|
77 | if (args.length < 4) {
|
---|
78 | System.err.println("Usage: java -jar josmtos57.jar OSM_filename meta_data_filename S57_ENC_ROOT_directory S57_filename");
|
---|
79 | System.exit(-1);
|
---|
80 | }
|
---|
81 | try {
|
---|
82 | Scanner min = new Scanner(new FileInputStream(args[1]));
|
---|
83 | meta = new HashMap<>();
|
---|
84 | meta.put("FILE", args[3]);
|
---|
85 | while (min.hasNext()) {
|
---|
86 | String[] tokens = min.next().split("=");
|
---|
87 | if (tokens.length >= 2)
|
---|
88 | meta.put(tokens[0], tokens[1].split("[ #]")[0]);
|
---|
89 | }
|
---|
90 | min.close();
|
---|
91 | } catch (IOException e) {
|
---|
92 | System.err.println("Meta data file: " + e.getMessage());
|
---|
93 | System.exit(-1);
|
---|
94 | }
|
---|
95 | try {
|
---|
96 | in = new BufferedReader(new FileReader(new File(args[0])));
|
---|
97 | try {
|
---|
98 | S57osm.OSMmap(in, map, false);
|
---|
99 | } catch (Exception e) {
|
---|
100 | System.err.println("Input data error");
|
---|
101 | System.exit(-1);
|
---|
102 | }
|
---|
103 | in.close();
|
---|
104 | } catch (IOException e) {
|
---|
105 | System.err.println("Input file: " + e.getMessage());
|
---|
106 | System.exit(-1);
|
---|
107 | }
|
---|
108 |
|
---|
109 | try {
|
---|
110 | buf = new byte[5242880];
|
---|
111 | idx = S57enc.encodeChart(map, meta, buf);
|
---|
112 | } catch (IndexOutOfBoundsException e) {
|
---|
113 | System.err.println("Output file too big (limit 5 MB) - try smaller areas");
|
---|
114 | System.exit(-1);
|
---|
115 | } catch (UnsupportedEncodingException e) {
|
---|
116 | System.err.println("Input data error" + e.getMessage());
|
---|
117 | System.exit(-1);
|
---|
118 | }
|
---|
119 |
|
---|
120 | CRC32 crc = new CRC32();
|
---|
121 | crc.update(buf, 0, idx);
|
---|
122 | try {
|
---|
123 | File file = new File(args[2] + args[3]);
|
---|
124 | if (file.exists()) file.delete();
|
---|
125 | out = new FileOutputStream(file, false);
|
---|
126 | out.write(buf, 0, idx);
|
---|
127 | } catch (IOException e) {
|
---|
128 | System.err.println("Output file: " + e.getMessage());
|
---|
129 | System.exit(-1);
|
---|
130 | }
|
---|
131 | out.close();
|
---|
132 |
|
---|
133 | buf = new byte[header.length];
|
---|
134 | System.arraycopy(header, 0, buf, 0, header.length);
|
---|
135 | idx = header.length;
|
---|
136 | int recs = 2;
|
---|
137 | fields = new ArrayList<>();
|
---|
138 | fields.add(new Fparams(S57field.CATD, new Object[]{"CD", recs, args[3], "", "V01X01", "BIN", Math.toDegrees(map.bounds.minlat),
|
---|
139 | Math.toDegrees(map.bounds.minlon), Math.toDegrees(map.bounds.maxlat), Math.toDegrees(map.bounds.maxlon),
|
---|
140 | String.format("%08X", crc.getValue()), ""}));
|
---|
141 | record = S57dat.encRecord(String.valueOf(recs++), fields);
|
---|
142 | buf = Arrays.copyOf(buf, (buf.length + record.length));
|
---|
143 | System.arraycopy(record, 0, buf, idx, record.length);
|
---|
144 | idx += record.length;
|
---|
145 |
|
---|
146 | try {
|
---|
147 | File file = new File(args[2] + "CATALOG.031");
|
---|
148 | if (file.exists()) file.delete();
|
---|
149 | out = new FileOutputStream(file, false);
|
---|
150 | out.write(buf, 0, idx);
|
---|
151 | } catch (IOException e) {
|
---|
152 | System.err.println("Catalogue file: " + e.getMessage());
|
---|
153 | System.exit(-1);
|
---|
154 | }
|
---|
155 | out.close();
|
---|
156 |
|
---|
157 | // String[] dir = (new File(args[2]).list());
|
---|
158 | // for (String item : dir) {
|
---|
159 | // System.err.println(item);
|
---|
160 | // }
|
---|
161 |
|
---|
162 | System.err.println("Finished");
|
---|
163 | }
|
---|
164 |
|
---|
165 | }
|
---|