comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/HWSBarriersState.java @ 6710:ffc9cb39cea7

Remove duplicate code for mapfile generation. ID problem still not fixed (#657).
author Christian Lins <christian.lins@intevation.de>
date Tue, 30 Jul 2013 16:09:40 +0200
parents d79bfbe55417
children a56fe3bc6700
comparison
equal deleted inserted replaced
6709:fe496c477173 6710:ffc9cb39cea7
7 */ 7 */
8 8
9 package org.dive4elements.river.artifacts.states; 9 package org.dive4elements.river.artifacts.states;
10 10
11 import java.io.File; 11 import java.io.File;
12 import java.io.FileNotFoundException;
13 import java.io.IOException; 12 import java.io.IOException;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List; 13 import java.util.List;
18 14
19 import org.apache.log4j.Logger; 15 import org.apache.log4j.Logger;
20
21 import org.apache.velocity.Template;
22
23 import org.geotools.data.shapefile.ShapefileDataStore;
24
25 import org.geotools.feature.FeatureCollection;
26 import org.geotools.feature.FeatureCollections;
27
28 import org.opengis.feature.simple.SimpleFeatureType;
29
30 import org.opengis.feature.type.GeometryDescriptor;
31
32 import org.w3c.dom.Element;
33
34 import org.dive4elements.artifactdatabase.state.Facet; 16 import org.dive4elements.artifactdatabase.state.Facet;
35
36 import org.dive4elements.artifacts.Artifact; 17 import org.dive4elements.artifacts.Artifact;
37 import org.dive4elements.artifacts.CallContext; 18 import org.dive4elements.artifacts.CallContext;
38
39 import org.dive4elements.artifacts.common.utils.FileTools; 19 import org.dive4elements.artifacts.common.utils.FileTools;
40
41 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; 20 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
42
43 import org.dive4elements.river.artifacts.D4EArtifact; 21 import org.dive4elements.river.artifacts.D4EArtifact;
44
45 import org.dive4elements.river.artifacts.access.MapAccess; 22 import org.dive4elements.river.artifacts.access.MapAccess;
46
47 import org.dive4elements.river.artifacts.model.LayerInfo;
48
49 import org.dive4elements.river.artifacts.model.map.HWS; 23 import org.dive4elements.river.artifacts.model.map.HWS;
50 import org.dive4elements.river.artifacts.model.map.HWSContainer; 24 import org.dive4elements.river.artifacts.model.map.HWSContainer;
51 import org.dive4elements.river.artifacts.model.map.HWSFactory; 25 import org.dive4elements.river.artifacts.model.map.HWSFactory;
52
53 import org.dive4elements.river.utils.ArtifactMapfileGenerator;
54 import org.dive4elements.river.utils.RiverUtils;
55 import org.dive4elements.river.utils.GeometryUtils; 26 import org.dive4elements.river.utils.GeometryUtils;
56 import org.dive4elements.river.utils.MapfileGenerator; 27 import org.dive4elements.river.utils.MapfileGenerator;
28 import org.dive4elements.river.utils.RiverUtils;
29 import org.geotools.data.shapefile.ShapefileDataStore;
30 import org.geotools.feature.FeatureCollection;
31 import org.geotools.feature.FeatureCollections;
32 import org.opengis.feature.simple.SimpleFeatureType;
33 import org.opengis.feature.type.GeometryDescriptor;
34 import org.w3c.dom.Element;
57 35
58 public class HWSBarriersState 36 public class HWSBarriersState
59 extends DefaultState 37 extends DefaultState
60 { 38 {
61 39
62 /** The logger that is used in this class.*/ 40 /** The logger that is used in this class.*/
63 private static Logger logger = Logger.getLogger(HWSBarriersState.class); 41 private static Logger logger = Logger.getLogger(HWSBarriersState.class);
64 private static final String HWS_SHAPEFILE_LINES = "hws-lines.shp"; 42 private static final String HWS_SHAPEFILE_LINES = "hws-lines.shp";
65 private static final String HWS_SHAPEFILE_POINTS = "hws-points.shp"; 43 private static final String HWS_SHAPEFILE_POINTS = "hws-points.shp";
66 private static final String USER_RGD_SHAPE = "user-rgd.shp"; 44
67 private static final String USER_RGD_ZIP = "user-rgd.zip"; 45
68 private static final String USER_RGD_FILENAME = "user-rgd";
69 @Override 46 @Override
70 protected String getUIProvider() { 47 protected String getUIProvider() {
71 return "map_digitize"; 48 return "map_digitize";
72 } 49 }
73 50
144 successPoints =GeometryUtils.writeShapefile( 121 successPoints =GeometryUtils.writeShapefile(
145 shapePoints, pointType, collectionPoints); 122 shapePoints, pointType, collectionPoints);
146 } 123 }
147 124
148 if (successLines) { 125 if (successLines) {
149 createMapfile( 126 FloodMapState.createMapfile(
150 artifact, 127 artifact,
151 artifactDir, 128 artifactDir,
152 MapfileGenerator.MS_LAYER_PREFIX + "hws-lines", 129 MapfileGenerator.MS_LAYER_PREFIX + "hws-lines",
153 HWS_SHAPEFILE_LINES, 130 HWS_SHAPEFILE_LINES,
154 "LINE", 131 "LINE",
155 "31467", 132 "31467",
156 "hws"); 133 "hws");
157 } 134 }
158 if (successPoints) { 135 if (successPoints) {
159 createMapfile( 136 FloodMapState.createMapfile(
160 artifact, 137 artifact,
161 artifactDir, 138 artifactDir,
162 MapfileGenerator.MS_LAYER_PREFIX + "hws-points", 139 MapfileGenerator.MS_LAYER_PREFIX + "hws-points",
163 HWS_SHAPEFILE_POINTS, 140 HWS_SHAPEFILE_POINTS,
164 "POINT", 141 "POINT",
170 if (!userRgd.equals("none")) { 147 if (!userRgd.equals("none")) {
171 if (extractUserShp(artifactDir)) { 148 if (extractUserShp(artifactDir)) {
172 try { 149 try {
173 ShapefileDataStore store = new ShapefileDataStore( 150 ShapefileDataStore store = new ShapefileDataStore(
174 new File(artifactDir.getCanonicalPath() + 151 new File(artifactDir.getCanonicalPath() +
175 "/" + USER_RGD_SHAPE) 152 "/" + FloodMapState.WSPLGEN_USER_RGD_SHAPE)
176 .toURI().toURL()); 153 .toURI().toURL());
177 GeometryDescriptor desc = 154 GeometryDescriptor desc =
178 store.getSchema().getGeometryDescriptor(); 155 store.getSchema().getGeometryDescriptor();
179 String type = desc.getType().getName().toString(); 156 String type = desc.getType().getName().toString();
180 String proj = 157 String proj =
199 type = "POLYGON"; 176 type = "POLYGON";
200 } 177 }
201 else { 178 else {
202 type = "POINT"; 179 type = "POINT";
203 } 180 }
204 createMapfile( 181 FloodMapState.createMapfile(
205 artifact, 182 artifact,
206 artifactDir, 183 artifactDir,
207 MapfileGenerator.MS_LAYER_PREFIX + USER_RGD_FILENAME, 184 MapfileGenerator.MS_LAYER_PREFIX + FloodMapState.WSPLGEN_USER_RGD,
208 USER_RGD_SHAPE, 185 FloodMapState.WSPLGEN_USER_RGD_SHAPE,
209 type, 186 type,
210 epsg, 187 epsg,
211 "user-rgd"); 188 FloodMapState.WSPLGEN_USER_RGD);
212 } 189 }
213 catch (IOException e) { 190 catch (IOException e) {
214 logger.warn("No mapfile for user-rgd created!"); 191 logger.warn("No mapfile for user-rgd created!");
215 } 192 }
216 } 193 }
217 } 194 }
218 return null; 195 return null;
219 } 196 }
220 197
221 private boolean extractUserShp(File dir) { 198 private boolean extractUserShp(File dir) {
222 File archive = new File(dir, USER_RGD_ZIP); 199 File archive = new File(dir, FloodMapState.WSPLGEN_USER_RGD_ZIP);
223 boolean exists = archive.exists(); 200 boolean exists = archive.exists();
224 logger.debug("Zip file exists: " + exists); 201 logger.debug("Zip file exists: " + exists);
225 if (exists) { 202 if (exists) {
226 try { 203 try {
227 File tmpDir = new File(dir, "usr_tmp"); 204 File tmpDir = new File(dir, "usr_tmp");
229 moveFiles(tmpDir, dir); 206 moveFiles(tmpDir, dir);
230 return true; 207 return true;
231 } 208 }
232 catch (IOException ioe) { 209 catch (IOException ioe) {
233 logger.warn("Zip archive " + dir + "/" 210 logger.warn("Zip archive " + dir + "/"
234 + USER_RGD_ZIP + " could not be extracted."); 211 + FloodMapState.WSPLGEN_USER_RGD_ZIP + " could not be extracted.");
235 return false; 212 return false;
236 } 213 }
237 } 214 }
238 return false; 215 return false;
239 } 216 }
259 } 236 }
260 else { 237 else {
261 return true; 238 return true;
262 } 239 }
263 try { 240 try {
264 FileTools.copyFile(file, new File(target, USER_RGD_FILENAME + "." + suffix)); 241 FileTools.copyFile(file, new File(target, FloodMapState.WSPLGEN_USER_RGD + "." + suffix));
265 } 242 }
266 catch (IOException ioe) { 243 catch (IOException ioe) {
267 logger.warn ("Error while copying file " + file.getName()); 244 logger.warn ("Error while copying file " + file.getName());
268 return true; 245 return true;
269 } 246 }
271 return true; 248 return true;
272 } 249 }
273 }); 250 });
274 251
275 FileTools.deleteRecursive(source); 252 FileTools.deleteRecursive(source);
276 }
277
278 private void createMapfile(
279 D4EArtifact artifact,
280 File artifactDir,
281 String name,
282 String hwsShapefile,
283 String type,
284 String srid,
285 String group
286 ) {
287 LayerInfo info = new LayerInfo();
288 info.setName(name + artifact.identifier());
289 info.setType(type);
290 info.setDirectory(artifact.identifier());
291 info.setTitle(name);
292 info.setData(hwsShapefile);
293 info.setSrid(srid);
294 info.setGroupTitle(group);
295 MapfileGenerator generator = new ArtifactMapfileGenerator();
296 Template tpl = generator.getTemplateByName(MapfileGenerator.SHP_LAYER_TEMPLATE);
297 try {
298 File layer = new File(artifactDir.getCanonicalPath() + "/" + name);
299 generator.writeLayer(info, layer, tpl);
300 List<String> layers = new ArrayList<String>();
301 layers.add(layer.getAbsolutePath());
302 generator.generate();
303 }
304 catch(FileNotFoundException fnfe) {
305 logger.warn("Could not find mapfile for hws layer");
306 }
307 catch (Exception ioe) {
308 logger.warn("Could not create mapfile for hws layer");
309 logger.warn(Arrays.toString(ioe.getStackTrace()));
310 }
311 } 253 }
312 254
313 255
314 @Override 256 @Override
315 public void endOfLife(Artifact artifact, Object callContext) { 257 public void endOfLife(Artifact artifact, Object callContext) {

http://dive4elements.wald.intevation.org