source: osm/applications/editors/josm/plugins/seachart/josmtos57/src/josmtos57/Josmtos57.java

Last change on this file was 35392, checked in by malcolmh, 5 years ago

use XML DOM to parse OSM files

File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package josmtos57;
3
4import java.io.BufferedReader;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileOutputStream;
8import java.io.File;
9import java.io.IOException;
10import java.io.UnsupportedEncodingException;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.HashMap;
14import java.util.Scanner;
15import java.util.zip.CRC32;
16
17import s57.S57dat;
18import s57.S57dat.Fparams;
19import s57.S57dat.S57field;
20import s57.S57enc;
21import s57.S57map;
22import s57.S57osm;
23
24/**
25 * @author Malcolm Herring
26 */
27public 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 File 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 in = new File(args[0]);
96 try {
97 S57osm.OSMmap(in, map, false);
98 } catch (Exception e) {
99 System.err.println("Input data error");
100 System.exit(-1);
101 }
102
103 try {
104 buf = new byte[5242880];
105 idx = S57enc.encodeChart(map, meta, buf);
106 } catch (IndexOutOfBoundsException e) {
107 System.err.println("Output file too big (limit 5 MB) - try smaller areas");
108 System.exit(-1);
109 } catch (UnsupportedEncodingException e) {
110 System.err.println("Input data error" + e.getMessage());
111 System.exit(-1);
112 }
113
114 CRC32 crc = new CRC32();
115 crc.update(buf, 0, idx);
116 try {
117 File file = new File(args[2] + args[3]);
118 if (file.exists()) file.delete();
119 out = new FileOutputStream(file, false);
120 out.write(buf, 0, idx);
121 } catch (IOException e) {
122 System.err.println("Output file: " + e.getMessage());
123 System.exit(-1);
124 }
125 out.close();
126
127 buf = new byte[header.length];
128 System.arraycopy(header, 0, buf, 0, header.length);
129 idx = header.length;
130 int recs = 2;
131 fields = new ArrayList<>();
132 fields.add(new Fparams(S57field.CATD, new Object[]{"CD", recs, args[3], "", "V01X01", "BIN", Math.toDegrees(map.bounds.minlat),
133 Math.toDegrees(map.bounds.minlon), Math.toDegrees(map.bounds.maxlat), Math.toDegrees(map.bounds.maxlon),
134 String.format("%08X", crc.getValue()), ""}));
135 record = S57dat.encRecord(String.valueOf(recs++), fields);
136 buf = Arrays.copyOf(buf, (buf.length + record.length));
137 System.arraycopy(record, 0, buf, idx, record.length);
138 idx += record.length;
139
140 try {
141 File file = new File(args[2] + "CATALOG.031");
142 if (file.exists()) file.delete();
143 out = new FileOutputStream(file, false);
144 out.write(buf, 0, idx);
145 } catch (IOException e) {
146 System.err.println("Catalogue file: " + e.getMessage());
147 System.exit(-1);
148 }
149 out.close();
150
151 // String[] dir = (new File(args[2]).list());
152 // for (String item : dir) {
153 // System.err.println(item);
154 // }
155
156 System.err.println("Finished");
157 }
158
159}
Note: See TracBrowser for help on using the repository browser.