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

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

add josmtos57

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