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