comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/DA66Parser.java @ 4681:aa718770308e

Less than sceleton for DA66-CrossSection parser added.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 14 Dec 2012 10:49:58 +0100
parents
children 70842db72ee4
comparison
equal deleted inserted replaced
4680:e8381265871f 4681:aa718770308e
1 package de.intevation.flys.importer.parsers;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.List;
7 import java.util.TreeMap;
8
9 import java.util.regex.Pattern;
10 import java.util.regex.Matcher;
11
12 import java.io.File;
13 import java.io.IOException;
14
15 import org.apache.log4j.Logger;
16
17 import de.intevation.flys.importer.XY;
18
19 import de.intevation.artifacts.common.utils.FileTools;
20
21
22 /**
23 * To create cross-sections, generate: Map<double,list<xy>> from files
24 * in da66 format.
25 */
26 public class DA66Parser extends LineParser
27 {
28 /** Private logger. */
29 private static Logger logger = Logger.getLogger(DA66Parser.class);
30
31 // TODO: Most of the Point/y/z group matches are optional!
32 public static final Pattern LINE_PATTERN =
33 Pattern.compile("^([0-9 -]{2})" + // Type
34 "([0-9 -]{5})" + // unset
35 "([0-9 -]{2})" + // id
36 "([0-9 -]{9})" + // station
37 "([0-9 -]{2})" + // running number
38 "([0-9 -]{1})" + // point id
39 /*
40 Would be great if we could express the pattern as this:
41 ([0-9 -]{1})([0-9 -JKMLMNOPQR]{7})([0-9 -]{7})+
42 */
43 "([0-9 -JKMLMNOPQR]{7})" + // y
44 "([0-9 -]{7})" + // z
45 "([0-9 -]{1})" + // point id
46 "([0-9 -JKMLMNOPQR]{7})" + // y
47 "([0-9 -]{7})" + // z
48 "([0-9 -]{1})" + // point id
49 "([0-9 -JKMLMNOPQR]{7})" + // y
50 "([0-9 -]{7})" + // z
51 "([0-9 -]{1})" + // point id
52 "([0-9 -JKMLMNOPQR]{7})" + // y
53 "([0-9 -]{7})" // z
54 );
55 //Pattern.compile("^([0-9 -]{2})");
56
57 private static enum Type {
58 DATE ( 0),
59 HEKTOSTONE_LEFT ( 1), //grm. "Standlinie"
60 HEKTOSTONE_RIGHT ( 2),
61 CHANNEL_LEFT ( 3), //grm. "Fahrrinne"
62 CHANNEL_RIGHT ( 4),
63 CHANNEL_2_LEFT ( 5),
64 CHANNEL_2_RIGHT ( 6),
65 GIW_1972 ( 7),
66 GROIN_DIST_LEFT ( 8), //grm. "Buhnenkopfabstand links"
67 GROIN_HEIGHT_LEFT ( 9),
68 GROIN_SLOPE_LEFT (10),
69 GROIN_DIST_RIGHT (11),
70 GROIN_HEIGHT_RIGHT (12),
71 GROIN_SLOPE_RIGHT (13),
72 STRIKE_LEFT (14), //grm. "Streichlinie links"
73 AXIS (15),
74 STRIKE_RIGHT (16),
75 GROIN_BACK_SLOPE_LEFT (17), //grm. "Buhnenrueckenneigung"
76 GROIN_BACK_SLOPE_RIGHT (18),
77 GIW_1932 (19),
78 GIW_1982 (20),
79 STAND_ISLAND_1 (21),
80 STAND_ISLAND_2 (22),
81 STAND_ISLAND_3 (23),
82 STAND_ISLAND_4 (24),
83 UNSPECIFIED_1 (25),
84 UNSPECIFIED_2 (26),
85 HHW (27),
86 OLD_PROFILE_NULL (28),
87 AW_1978 (29),
88 SIGN_LEFT (30),
89 SIGN_RIGHT (31),
90 DIST_SIGNAL_CHANNEL_LEFT (32),
91 DIST_SIGNAL_CHANNEL_RIGHT(33),
92 UNSPECIFIED_3 (34),
93 UNSPECIFIED_4 (35),
94 UNSPECIFIED_5 (36),
95 UNSPECIFIED_6 (37),
96 SHORE_LEFT (38),
97 SHORE_RIGHT (39),
98 UNSPECIFIED_7 (40);
99
100 private final int id;
101 Type(int id) {
102 this.id = id;
103 }
104 public int getId() {
105 return id;
106 }
107 }
108
109 /** Available types. */
110 private static HashMap<Integer, Type> typeMap;
111
112 /** Types we can deal with. */
113 private static List<Type> implementedTypes;
114
115 static {
116 typeMap = new HashMap<Integer, Type>();
117 for (Type t: Type.values()) {
118 typeMap.put(new Integer(t.getId()), t);
119 }
120 implementedTypes = new ArrayList<Type>();
121 //implementedTypes.add(..);
122 }
123
124 public interface Callback {
125 boolean da60Accept(File file);
126 void da60Parsed(DA66Parser parser);
127 } // interface Parser
128
129 protected Map<Double, List<XY>> data;
130
131
132 public DA66Parser() {
133 data = new TreeMap<Double, List<XY>>();
134 }
135
136 public Map<Double, List<XY>> getData() {
137 return data;
138 }
139
140 public void setData(Map<Double, List<XY>> data) {
141 this.data = data;
142 }
143
144 private static final String removeExtension(String name) {
145 int index = name.lastIndexOf('.');
146 return index == -1
147 ? name
148 : name.substring(0, index);
149 }
150
151 public void reset() {
152 data.clear();
153 }
154
155 public void parseDA66s(File root, final Callback callback) {
156
157 FileTools.walkTree(root, new FileTools.FileVisitor() {
158 @Override
159 public boolean visit(File file) {
160 if (file.isFile() && file.canRead()
161 && file.getName().toLowerCase().endsWith(".d66")
162 && (callback == null || callback.da60Accept(file))) {
163 reset();
164 try {
165 parse(file);
166 logger.info("parsing done");
167 if (callback != null) {
168 callback.da60Parsed(DA66Parser.this);
169 }
170 }
171 catch (IOException ioe) {
172 logger.error("IOException while parsing file");
173 return false;
174 }
175 }
176 return true;
177 }
178 });
179 }
180
181 // LineParser
182 @Override
183 protected void finish() {}
184
185 /** Called for each line. Try to extract info from a da66 line. */
186 @Override
187 protected void handleLine(int lineNum, String line) {
188 if (line.substring(0,2).equals("00")) {
189 logger.warn("Hit a 00");
190 }
191 else if (line.substring(0,2).equals("66")) {
192 String station = line.substring(10,18);
193 logger.info(station);
194 Matcher m = LINE_PATTERN.matcher(line);
195 if(m.find())
196 logger.warn("Group1: " + m.group(1));
197 else
198 logger.warn("no match in " + line);
199 //logger.warn("Hit a 66");
200 }
201 else if (line.substring(0,2).equals("88"))
202 logger.warn("Hit a 88");
203 else
204 logger.error("Do not know how to treat da66 line.");
205 }
206
207
208 /** Parses files given as arguments. */
209 public static void main(String [] args) {
210
211 DA66Parser parser = new DA66Parser();
212
213 logger.warn("Start Parsing files.");
214 for (String arg: args) {
215 parser.parseDA66s(new File(arg), null);
216 logger.warn("Parsing a file.");
217 }
218 logger.error("Stopped Parsing files.");
219 }
220 }
221 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org