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

Last change on this file since 31722 was 31722, checked in by malcolmh, 9 years ago

[seachart] update

File size: 4.9 KB
Line 
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
10package josmtos57;
11
12import java.io.*;
13import java.util.HashMap;
14import java.util.Scanner;
15import java.util.zip.CRC32;
16
17import s57.S57enc;
18import s57.S57map;
19import s57.S57osm;
20
21public class Josmtos57 {
22
23 // http://opendatacommons.org/licenses/odbl/1-0/
24
25 /*
26 URL website = new URL("http://www.website.com/information.asp");
27 try (InputStream in = website.openStream()) { Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING); }
28 */
29
30 static byte[] header = {
31 '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',
32 '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',
33 '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',
34 ';', '&', ' ', ' ', ' ', 'I', 'S', 'O', '/', 'I', 'E', 'C', ' ', '8', '2', '1', '1', ' ', 'R', 'e', 'c', 'o', 'r', 'd', ' ', 'I', 'd', 'e', 'n', 't', 'i', 'f',
35 'i', 'e', 'r', 0x1f, 0x1f, '(', 'I', '(', '5', ')', ')', 0x1e, '1', '6', '0', '0', ';', '&', ' ', ' ', ' ', 'C', 'a', 't', 'a', 'l', 'o', 'g', 'u', 'e', ' ', 'D',
36 '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',
37 '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',
38 '!', 'C', 'R', 'C', 'S', '!', 'C', 'O', 'M', 'T', 0x1f, '(', 'A', '(', '2', ')', ',', 'I', '(', '1', '0', ')', ',', '3', 'A', ',', 'A', '(', '3', ')', ',', '4',
39 'R', ',', '2', 'A', ')', 0x1e
40 };
41
42 static byte[] entry = {
43 //*** 0
44 '0', '0', '1', '0', '1', ' ', 'D', ' ', ' ', ' ', ' ', ' ', '0', '0', '0', '5', '3', ' ', ' ', ' ', '5', '5', '0', '4', // Leader
45 '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, // Directory
46 '0', '0', '0', '0', '0', 0x1e,
47 //***
48 'C', 'D', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', // Record name+number
49 //***
50 'C', 'A', 'T', 'A', 'L', 'O', 'G', '.', '0', '3', '1', 0x1f, // File name
51 0x1f, // File long name
52 'V', '0', '1', 'X', '0', '1', 0x1f, // Volume
53 'A', 'S', 'C', // Implementation
54 0x1f, 0x1f, 0x1f, 0x1f, // minlat, minlon, maxlat, maxlon
55 0x1f, // CRC32
56 0x1f, 0x1e // Comment
57 };
58
59 static BufferedReader in;
60 static FileOutputStream out;
61 static S57map map;
62 static byte[] buf;
63 static HashMap<String, String> meta;
64
65 public static void main(String[] args) throws IOException {
66
67 map = new S57map(true);
68 int idx = 0;
69
70 if (args.length < 4) {
71 System.err.println("Usage: java -jar josmtos57.jar OSM_filename meta_data_filename S57_ENC_ROOT_directory S57_filename");
72 System.exit(-1);
73 }
74 try {
75 Scanner min = new Scanner(new FileInputStream(args[1]));
76 meta = new HashMap<String, String>();
77 meta.put("FILE", args[3]);
78// while (min.hasNext()) {
79// String[] tokens = min.next().split("=");
80// meta.put(tokens[0], tokens[1]);
81// }
82 min.close();
83 } catch (IOException e) {
84 System.err.println("Meta data file: " + e.getMessage());
85 System.exit(-1);
86 }
87 try {
88 in = new BufferedReader(new FileReader(new File(args[0])));
89 S57osm.OSMmap(in, map);
90 in.close();
91 } catch (IOException e) {
92 System.err.println("Input file: " + e.getMessage());
93 System.exit(-1);
94 }
95
96 try {
97 buf = new byte[5242880];
98 idx = S57enc.encodeChart(map, meta, buf);
99 } catch (IndexOutOfBoundsException e) {
100 System.err.println("Output file too big (limit 5 MB) - try smaller areas");
101 System.exit(-1);
102 } catch (UnsupportedEncodingException e) {
103 System.err.println("Input data error" + e.getMessage());
104 System.exit(-1);
105 }
106
107 CRC32 crc = new CRC32();
108 crc.reset();
109 crc.update(buf, 0, idx);
110 try {
111 File file = new File(args[2] + args[3]);
112 if (file.exists()) file.delete();
113 out = new FileOutputStream(file, false);
114 out.write(buf, 0, idx);
115 } catch (IOException e) {
116 System.err.println("Output file: " + e.getMessage());
117 System.exit(-1);
118 }
119 out.close();
120
121 try {
122 File file = new File(args[2] + "CATALOG.031");
123 if (file.exists()) file.delete();
124 out = new FileOutputStream(file, false);
125 } catch (IOException e) {
126 System.err.println("Catalogue file: " + e.getMessage());
127 System.exit(-1);
128 }
129 out.close();
130
131 String[] dir = (new File(args[2]).list());
132 for (String item : dir) {
133 System.err.println(item);
134 }
135
136 System.err.println("Finished");
137 }
138
139}
Note: See TracBrowser for help on using the repository browser.