comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HWSBarriersState.java @ 5310:11c853b0854a

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

http://dive4elements.wald.intevation.org