Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HWSBarriersState.java @ 5379:61bf64b102bc mapgenfix
Merge with default branch
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Fri, 22 Mar 2013 11:25:54 +0100 |
parents | a1cb9a734cc5 |
children | 9207142726b3 |
comparison
equal
deleted
inserted
replaced
5175:cfc5540a4eec | 5379:61bf64b102bc |
---|---|
1 package de.intevation.flys.artifacts.states; | |
2 | |
3 import java.io.File; | |
4 import java.io.FileNotFoundException; | |
5 import java.io.IOException; | |
6 | |
7 import java.util.ArrayList; | |
8 import java.util.Arrays; | |
9 import java.util.List; | |
10 | |
11 import org.apache.log4j.Logger; | |
12 | |
13 import org.apache.velocity.Template; | |
14 | |
15 import org.geotools.data.shapefile.ShapefileDataStore; | |
16 | |
17 import org.geotools.feature.FeatureCollection; | |
18 import org.geotools.feature.FeatureCollections; | |
19 | |
20 import org.opengis.feature.simple.SimpleFeatureType; | |
21 | |
22 import org.opengis.feature.type.GeometryDescriptor; | |
23 | |
24 import org.w3c.dom.Element; | |
25 | |
26 import de.intevation.artifactdatabase.state.Facet; | |
27 | |
28 import de.intevation.artifacts.Artifact; | |
29 import de.intevation.artifacts.CallContext; | |
30 | |
31 import de.intevation.artifacts.common.utils.FileTools; | |
32 | |
33 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | |
34 | |
35 import de.intevation.flys.artifacts.FLYSArtifact; | |
36 | |
37 import de.intevation.flys.artifacts.access.MapAccess; | |
38 | |
39 import de.intevation.flys.artifacts.model.LayerInfo; | |
40 | |
41 import de.intevation.flys.artifacts.model.map.HWS; | |
42 import de.intevation.flys.artifacts.model.map.HWSContainer; | |
43 import de.intevation.flys.artifacts.model.map.HWSFactory; | |
44 | |
45 import de.intevation.flys.utils.ArtifactMapfileGenerator; | |
46 import de.intevation.flys.utils.FLYSUtils; | |
47 import de.intevation.flys.utils.GeometryUtils; | |
48 import de.intevation.flys.utils.MapfileGenerator; | |
49 | |
50 public class HWSBarriersState | |
51 extends DefaultState | |
52 { | |
53 | |
54 /** The logger that is used in this class.*/ | |
55 private static Logger logger = Logger.getLogger(HWSBarriersState.class); | |
56 private static final String HWS_SHAPEFILE_LINES = "hws-lines.shp"; | |
57 private static final String HWS_SHAPEFILE_POINTS = "hws-points.shp"; | |
58 private static final String USER_RGD_SHAPE = "user-rgd.shp"; | |
59 private static final String USER_RGD_ZIP = "user-rgd.zip"; | |
60 private static final String USER_RGD_FILENAME = "user-rgd"; | |
61 @Override | |
62 protected String getUIProvider() { | |
63 return "map_digitize"; | |
64 } | |
65 | |
66 | |
67 @Override | |
68 protected Element createStaticData( | |
69 FLYSArtifact flys, | |
70 ElementCreator creator, | |
71 CallContext cc, | |
72 String name, | |
73 String value, | |
74 String type | |
75 ) { | |
76 Element dataElement = creator.create("data"); | |
77 creator.addAttr(dataElement, "name", name, true); | |
78 creator.addAttr(dataElement, "type", type, true); | |
79 | |
80 Element itemElement = creator.create("item"); | |
81 creator.addAttr(itemElement, "value", value, true); | |
82 | |
83 creator.addAttr(itemElement, "label", "", true); | |
84 dataElement.appendChild(itemElement); | |
85 | |
86 return dataElement; | |
87 } | |
88 | |
89 | |
90 @Override | |
91 public Object computeAdvance( | |
92 FLYSArtifact artifact, | |
93 String hash, | |
94 CallContext context, | |
95 List<Facet> facets, | |
96 Object old | |
97 ) { | |
98 File artifactDir = getDirectory(artifact); | |
99 | |
100 if (artifactDir == null) { | |
101 logger.error("Could not create directory for HWS shapefile!"); | |
102 return null; | |
103 } | |
104 | |
105 MapAccess access = new MapAccess(artifact, context); | |
106 String river = access.getRiver(); | |
107 HWSContainer hwsLines = HWSFactory.getHWSLines(river); | |
108 HWSContainer hwsPoints = HWSFactory.getHWSPoints(river); | |
109 List<String> selected = access.getHWS(); | |
110 | |
111 List<HWS> selectedLines = hwsLines.getHws(selected); | |
112 List<HWS> selectedPoints = hwsPoints.getHws(selected); | |
113 | |
114 FeatureCollection collectionLines = FeatureCollections.newCollection(); | |
115 SimpleFeatureType lineType = null; | |
116 for (HWS h : selectedLines) { | |
117 lineType = h.getFeatureType(); | |
118 collectionLines.add(h.getFeature()); | |
119 } | |
120 boolean successLines = false; | |
121 if (lineType != null && collectionLines.size() > 0) { | |
122 File shapeLines = new File(artifactDir, HWS_SHAPEFILE_LINES); | |
123 successLines = GeometryUtils.writeShapefile( | |
124 shapeLines, lineType, collectionLines); | |
125 } | |
126 | |
127 FeatureCollection collectionPoints = FeatureCollections.newCollection(); | |
128 SimpleFeatureType pointType = null; | |
129 for (HWS h : selectedPoints) { | |
130 pointType = h.getFeatureType(); | |
131 collectionPoints.add(h.getFeature()); | |
132 } | |
133 boolean successPoints = false; | |
134 if (pointType != null && collectionPoints.size() > 0) { | |
135 File shapePoints = new File(artifactDir, HWS_SHAPEFILE_POINTS); | |
136 successPoints =GeometryUtils.writeShapefile( | |
137 shapePoints, pointType, collectionPoints); | |
138 } | |
139 | |
140 if (successLines) { | |
141 createMapfile( | |
142 artifact, | |
143 artifactDir, | |
144 MapfileGenerator.MS_LAYER_PREFIX + "hws-lines", | |
145 HWS_SHAPEFILE_LINES, | |
146 "LINE", | |
147 "31467", | |
148 "hws"); | |
149 } | |
150 if (successPoints) { | |
151 createMapfile( | |
152 artifact, | |
153 artifactDir, | |
154 MapfileGenerator.MS_LAYER_PREFIX + "hws-points", | |
155 HWS_SHAPEFILE_POINTS, | |
156 "POINT", | |
157 "31467", | |
158 "hws"); | |
159 } | |
160 | |
161 String userRgd = artifact.getDataAsString("uesk.user-rgd"); | |
162 if (!userRgd.equals("none")) { | |
163 if (extractUserShp(artifactDir)) { | |
164 try { | |
165 ShapefileDataStore store = new ShapefileDataStore( | |
166 new File(artifactDir.getCanonicalPath() + | |
167 "/" + USER_RGD_SHAPE) | |
168 .toURI().toURL()); | |
169 GeometryDescriptor desc = | |
170 store.getSchema().getGeometryDescriptor(); | |
171 String type = desc.getType().getName().toString(); | |
172 String proj = | |
173 desc.getCoordinateReferenceSystem(). | |
174 getCoordinateSystem().toString(); | |
175 int pos1 = proj.indexOf("EPSG\",\""); | |
176 int pos2 = proj.indexOf("\"]]"); | |
177 String epsg = ""; | |
178 if (pos1 >= 0 && pos2 >= 0) { | |
179 epsg = | |
180 proj.substring(proj.indexOf("EPSG\",\"") + 7, | |
181 proj.indexOf("\"]]")); | |
182 } | |
183 else { | |
184 logger.warn("Could not read EPSG code from shapefile."); | |
185 return null; | |
186 } | |
187 if (type.contains("Line")) { | |
188 type = "LINE"; | |
189 } | |
190 else if (type.contains("Poly")) { | |
191 type = "POLYON"; | |
192 } | |
193 else { | |
194 type = "POINT"; | |
195 } | |
196 createMapfile( | |
197 artifact, | |
198 artifactDir, | |
199 MapfileGenerator.MS_LAYER_PREFIX + USER_RGD_FILENAME, | |
200 USER_RGD_SHAPE, | |
201 type, | |
202 epsg, | |
203 "user-rgd"); | |
204 } | |
205 catch (IOException e) { | |
206 logger.warn("No mapfile for user-rgd created!"); | |
207 } | |
208 } | |
209 } | |
210 return null; | |
211 } | |
212 | |
213 private boolean extractUserShp(File dir) { | |
214 // TODO Auto-generated method stub | |
215 File archive = new File(dir, USER_RGD_ZIP); | |
216 boolean exists = archive.exists(); | |
217 logger.debug("Zip file exists: " + exists); | |
218 if (exists) { | |
219 try { | |
220 File tmpDir = new File(dir, "usr_tmp"); | |
221 FileTools.extractArchive(archive, tmpDir); | |
222 moveFiles(tmpDir, dir); | |
223 return true; | |
224 } | |
225 catch (IOException ioe) { | |
226 logger.warn("Zip archive " + dir + "/" | |
227 + USER_RGD_ZIP + " could not be extracted."); | |
228 return false; | |
229 } | |
230 } | |
231 return false; | |
232 } | |
233 | |
234 private void moveFiles(File source, final File target) | |
235 throws IOException | |
236 { | |
237 if (!source.exists()) { | |
238 return; | |
239 } | |
240 if (!target.exists()) { | |
241 target.mkdir(); | |
242 } | |
243 FileTools.walkTree(source, new FileTools.FileVisitor() { | |
244 @Override | |
245 public boolean visit(File file) { | |
246 if (!file.isDirectory()) { | |
247 String name = file.getName(); | |
248 String suffix = ""; | |
249 int pos = name.lastIndexOf('.'); | |
250 if (pos > 0 && pos < name.length() - 1) { | |
251 suffix = name.substring(pos + 1); | |
252 } | |
253 else { | |
254 return true; | |
255 } | |
256 try { | |
257 FileTools.copyFile(file, new File(target, USER_RGD_FILENAME + "." + suffix)); | |
258 } | |
259 catch (IOException ioe) { | |
260 logger.warn ("Error while copying file " + file.getName()); | |
261 return true; | |
262 } | |
263 } | |
264 return true; | |
265 } | |
266 }); | |
267 | |
268 FileTools.deleteRecursive(source); | |
269 } | |
270 | |
271 private void createMapfile( | |
272 FLYSArtifact artifact, | |
273 File artifactDir, | |
274 String name, | |
275 String hwsShapefile, | |
276 String type, | |
277 String srid, | |
278 String group | |
279 ) { | |
280 LayerInfo info = new LayerInfo(); | |
281 info.setName(name + artifact.identifier()); | |
282 info.setType(type); | |
283 info.setDirectory(artifact.identifier()); | |
284 info.setTitle(name); | |
285 info.setData(hwsShapefile); | |
286 info.setSrid(srid); | |
287 info.setGroupTitle(group); | |
288 MapfileGenerator generator = new ArtifactMapfileGenerator(); | |
289 Template tpl = generator.getTemplateByName(MapfileGenerator.SHP_LAYER_TEMPLATE); | |
290 try { | |
291 File layer = new File(artifactDir.getCanonicalPath() + "/" + name); | |
292 generator.writeLayer(info, layer, tpl); | |
293 List<String> layers = new ArrayList<String>(); | |
294 layers.add(layer.getAbsolutePath()); | |
295 generator.generate(); | |
296 } | |
297 catch(FileNotFoundException fnfe) { | |
298 logger.warn("Could not find mapfile for hws layer"); | |
299 } | |
300 catch (Exception ioe) { | |
301 logger.warn("Could not create mapfile for hws layer"); | |
302 logger.warn(Arrays.toString(ioe.getStackTrace())); | |
303 } | |
304 } | |
305 | |
306 | |
307 @Override | |
308 public void endOfLife(Artifact artifact, Object callContext) { | |
309 super.endOfLife(artifact, callContext); | |
310 logger.info("ScenarioSelect.endOfLife: " + artifact.identifier()); | |
311 | |
312 FLYSArtifact flys = (FLYSArtifact) artifact; | |
313 removeDirectory(flys); | |
314 } | |
315 | |
316 | |
317 /** | |
318 * Removes the directory and all its content where the required data and the | |
319 * results of WSPLGEN are stored. Should be called in endOfLife(). | |
320 */ | |
321 // FIXME: I've seen this code somewhere else... | |
322 protected void removeDirectory(FLYSArtifact artifact) { | |
323 String shapePath = FLYSUtils.getXPathString( | |
324 FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR); | |
325 | |
326 File artifactDir = new File(shapePath, artifact.identifier()); | |
327 | |
328 if (artifactDir.exists()) { | |
329 logger.debug("Delete directory: " + artifactDir.getAbsolutePath()); | |
330 boolean success = FileTools.deleteRecursive(artifactDir); | |
331 if (!success) { | |
332 logger.warn("could not remove dir '" + artifactDir + "'"); | |
333 } | |
334 } | |
335 else { | |
336 logger.debug("There is no directory to remove."); | |
337 } | |
338 } | |
339 | |
340 /** | |
341 * Returns (and creates if not existing) the directory for storing WSPLEN | |
342 * data for the owner artifact. | |
343 * | |
344 * @param artifact The owner Artifact. | |
345 * | |
346 * @return the directory for WSPLEN data. | |
347 */ | |
348 protected File getDirectory(FLYSArtifact artifact) { | |
349 String shapePath = FLYSUtils.getXPathString( | |
350 FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR); | |
351 | |
352 File artifactDir = FileTools.getDirectory( | |
353 shapePath, artifact.identifier()); | |
354 | |
355 return artifactDir; | |
356 } | |
357 | |
358 } | |
359 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |