comparison flys-artifacts/src/main/java/de/intevation/flys/mapserver/ArtifactMapfileGenerator.java @ 5022:a9243df307b1 mapgenfix

Move all classes of mapfile generation to de.intevation.flys.mapserver package.
author Christian Lins <christian.lins@intevation.de>
date Tue, 19 Feb 2013 13:41:20 +0100
parents
children 1da61095040c
comparison
equal deleted inserted replaced
5009:7c8ce0a95a64 5022:a9243df307b1
1 package de.intevation.flys.mapserver;
2
3 import de.intevation.artifacts.CallContext;
4 import de.intevation.flys.artifacts.FLYSArtifact;
5 import de.intevation.flys.artifacts.access.RiverAccess;
6 import de.intevation.flys.artifacts.model.LayerInfo;
7 import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
8 import de.intevation.flys.artifacts.model.map.WMSLayerFacet;
9 import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet;
10 import de.intevation.flys.artifacts.resources.Resources;
11 import de.intevation.flys.utils.FLYSUtils;
12 import de.intevation.flys.utils.GeometryUtils;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.util.List;
18
19 import org.apache.log4j.Logger;
20 import org.apache.velocity.Template;
21 import org.geotools.data.shapefile.ShpFiles;
22 import org.geotools.data.shapefile.shp.ShapefileHeader;
23 import org.geotools.data.shapefile.shp.ShapefileReader;
24
25 public class ArtifactMapfileGenerator extends MapfileGenerator {
26
27 private static Logger logger = Logger.getLogger(ArtifactMapfileGenerator.class);
28
29 @Override
30 protected String getVelocityLogfile() {
31 return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_VELOCITY_LOGFILE);
32 }
33
34 @Override
35 protected String getMapserverTemplatePath() {
36 return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_MAPSERVER_TEMPLATE_PATH);
37 }
38
39 @Override
40 protected String getMapserverUrl() {
41 return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_MAPSERVER_URL);
42 }
43
44 /**
45 * Method which starts searching for meta information file and mapfile
46 * generation.
47 */
48 @Override
49 public void generate() throws IOException
50 {
51 File[] userDirs = getUserDirs();
52 List<String> layers = parseLayers(userDirs);
53 logger.info("Found " + layers.size() + " layers for user mapfile.");
54
55 writeMapfile(layers);
56 }
57
58 /**
59 * Creates a layer file used for Mapserver's mapfile which represents the
60 * floodmap.
61 *
62 * @param flys The FLYSArtifact that owns <i>wms</i>.
63 * @param wms The WMSLayerFacet that contains information for the layer.
64 */
65 public void createUeskLayer(
66 FLYSArtifact flys,
67 WSPLGENLayerFacet wms,
68 String style,
69 CallContext context
70 ) throws FileNotFoundException, IOException
71 {
72 logger.debug("createUeskLayer");
73
74 LayerInfo layerinfo = new LayerInfo();
75 layerinfo.setName(MS_WSPLGEN_PREFIX + flys.identifier());
76 layerinfo.setType("POLYGON");
77 layerinfo.setDirectory(flys.identifier());
78 layerinfo.setData(WSPLGEN_RESULT_SHAPE);
79 layerinfo.setTitle(Resources.getMsg(Resources.getLocale(context.getMeta()),
80 "floodmap.uesk",
81 "Floodmap"));
82 layerinfo.setStyle(style);
83 RiverAccess access = new RiverAccess(flys);
84 String river = access.getRiver();
85 layerinfo.setSrid(FLYSUtils.getRiverDGMSrid(river));
86
87 String name = MS_LAYER_PREFIX + wms.getName();
88
89 Template template = getTemplateByName(WSPLGEN_LAYER_TEMPLATE);
90 if (template == null) {
91 logger.warn("Template '" + WSPLGEN_LAYER_TEMPLATE + "' found.");
92 return;
93 }
94
95 try {
96 File dir = new File(getShapefileBaseDir(), flys.identifier());
97 writeLayer(layerinfo, new File(dir, name), template);
98 }
99 catch (FileNotFoundException fnfe) {
100 logger.error(fnfe, fnfe);
101 logger.warn("Unable to write layer: " + name);
102 }
103 }
104
105
106 /**
107 * Creates a layer file used for Mapserver's mapfile which represents the
108 * user defined barriers.
109 *
110 * @param flys The FLYSArtifact that owns <i>wms</i>.
111 * @param wms The WMSLayerFacet that contains information for the layer.
112 */
113 public void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms)
114 throws FileNotFoundException, IOException
115 {
116 logger.debug("createBarriersLayer");
117
118 //String uuid = flys.identifier();
119 //File dir = new File(getShapefileBaseDir(), uuid);
120
121 createBarriersLineLayer(flys, wms);
122 createBarriersPolygonLayer(flys, wms);
123 }
124
125
126 protected void createBarriersLineLayer(
127 FLYSArtifact flys,
128 WMSLayerFacet wms
129 )
130 throws FileNotFoundException, IOException
131 {
132 String uuid = flys.identifier();
133 String group = MS_BARRIERS_PREFIX + uuid;
134 String groupTitle = "I18N_BARRIERS_TITLE";
135
136 File dir = new File(getShapefileBaseDir(), uuid);
137 File test = new File(dir, WSPLGEN_LINES_SHAPE);
138
139 if (!test.exists() || !test.canRead()) {
140 logger.debug("No barrier line layer existing.");
141 return;
142 }
143
144 LayerInfo lineInfo = new LayerInfo();
145 lineInfo.setName(MS_LINE_PREFIX + uuid);
146 lineInfo.setType("LINE");
147 lineInfo.setDirectory(uuid);
148 lineInfo.setData(WSPLGEN_LINES_SHAPE);
149 lineInfo.setTitle("I18N_LINE_SHAPE");
150 lineInfo.setGroup(group);
151 lineInfo.setGroupTitle(groupTitle);
152 lineInfo.setSrid(wms.getSrid());
153
154 String nameLines = MS_LAYER_PREFIX + wms.getName() + "-lines";
155
156 Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
157 if (tpl == null) {
158 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
159 return;
160 }
161
162 try {
163 writeLayer(lineInfo, new File(dir, nameLines), tpl);
164 }
165 catch (FileNotFoundException fnfe) {
166 logger.error(fnfe, fnfe);
167 logger.warn("Unable to write layer: " + nameLines);
168 }
169 }
170
171 protected void createBarriersPolygonLayer(
172 FLYSArtifact flys,
173 WMSLayerFacet wms
174 )
175 throws FileNotFoundException, IOException
176 {
177 String uuid = flys.identifier();
178 String group = uuid + MS_BARRIERS_PREFIX;
179 String groupTitle = "I18N_BARRIERS_TITLE";
180
181 File dir = new File(getShapefileBaseDir(), uuid);
182 File test = new File(dir, WSPLGEN_POLYGONS_SHAPE);
183
184 if (!test.exists() || !test.canRead()) {
185 logger.debug("No barrier line layer existing.");
186 return;
187 }
188
189 LayerInfo polygonInfo = new LayerInfo();
190 polygonInfo.setName(MS_POLYGONS_PREFIX + uuid);
191 polygonInfo.setType("POLYGON");
192 polygonInfo.setDirectory(uuid);
193 polygonInfo.setData(WSPLGEN_POLYGONS_SHAPE);
194 polygonInfo.setTitle("I18N_POLYGON_SHAPE");
195 polygonInfo.setGroup(group);
196 polygonInfo.setGroupTitle(groupTitle);
197 polygonInfo.setSrid(wms.getSrid());
198
199 String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons";
200
201 Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
202 if (tpl == null) {
203 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
204 return;
205 }
206
207 try {
208 writeLayer(polygonInfo, new File(dir, namePolygons), tpl);
209 }
210 catch (FileNotFoundException fnfe) {
211 logger.error(fnfe, fnfe);
212 logger.warn("Unable to write layer: " + namePolygons);
213 }
214 }
215
216
217 /**
218 * Creates a layer file used for Mapserver's mapfile which represents the
219 * shape files uploaded by the user.
220 *
221 * @param flys The FLYSArtifact that owns <i>wms</i>.
222 * @param wms The WMSLayerFacet that contains information for the layer.
223 */
224 public void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms)
225 throws FileNotFoundException, IOException
226 {
227 logger.debug("createUserShapeLayer");
228
229 String uuid = flys.identifier();
230 File dir = new File(getShapefileBaseDir(), uuid);
231 File test = new File(dir, WSPLGEN_USER_SHAPE);
232
233 if (!test.exists() || !test.canRead()) {
234 logger.debug("No user layer existing.");
235 return;
236 }
237
238 File userShape = new File(dir, WSPLGEN_USER_SHAPE);
239 ShpFiles sf = new ShpFiles(userShape);
240 ShapefileReader sfr = new ShapefileReader(sf, true, false, null);
241 ShapefileHeader sfh = sfr.getHeader();
242
243 String group = uuid + MS_USERSHAPE_PREFIX;
244 String groupTitle = "I18N_USER_SHAPE_TITLE";
245
246 LayerInfo info = new LayerInfo();
247 info.setName(MS_USERSHAPE_PREFIX + uuid);
248 if (sfh.getShapeType().isLineType()) {
249 info.setType("LINE");
250 }
251 else if (sfh.getShapeType().isPolygonType()) {
252 info.setType("POLYGON");
253 }
254 else {
255 return;
256 }
257 info.setDirectory(uuid);
258 info.setData(WSPLGEN_USER_SHAPE);
259 info.setTitle("I18N_USER_SHAPE");
260 info.setGroup(group);
261 info.setGroupTitle(groupTitle);
262 info.setSrid(wms.getSrid());
263
264 String nameUser = MS_LAYER_PREFIX + wms.getName();
265
266 Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
267 if (tpl == null) {
268 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
269 return;
270 }
271
272 try {
273 writeLayer(info, new File(dir, nameUser), tpl);
274 }
275 catch (FileNotFoundException fnfe) {
276 logger.error(fnfe, fnfe);
277 logger.warn("Unable to write layer: " + nameUser);
278 }
279
280 }
281
282
283 /**
284 * Creates a layer file used for Mapserver's mapfile which represents
285 * geometries from database.
286 *
287 * @param flys The FLYSArtifact that owns <i>wms</i>.
288 * @param wms The WMSLayerFacet that contains information for the layer.
289 */
290 public void createDatabaseLayer(
291 FLYSArtifact flys,
292 WMSDBLayerFacet wms,
293 String style
294 )
295 throws FileNotFoundException, IOException
296 {
297 logger.debug("createDatabaseLayer");
298
299 LayerInfo layerinfo = new LayerInfo();
300 layerinfo.setName(wms.getName() + "-" + flys.identifier());
301 layerinfo.setType(wms.getGeometryType());
302 layerinfo.setFilter(wms.getFilter());
303 layerinfo.setData(wms.getData());
304 layerinfo.setTitle(wms.getDescription());
305 layerinfo.setStyle(style);
306 if(wms.getExtent() != null) {
307 layerinfo.setExtent(GeometryUtils.jtsBoundsToOLBounds(wms.getExtent()));
308 }
309 layerinfo.setConnection(wms.getConnection());
310 layerinfo.setConnectionType(wms.getConnectionType());
311 layerinfo.setLabelItem(wms.getLabelItem());
312 layerinfo.setSrid(wms.getSrid());
313
314 String name = MS_LAYER_PREFIX + wms.getName();
315
316 Template template = getTemplateByName(DB_LAYER_TEMPLATE);
317 if (template == null) {
318 logger.warn("Template '" + DB_LAYER_TEMPLATE + "' found.");
319 return;
320 }
321
322 try {
323 File dir = new File(getShapefileBaseDir(), flys.identifier());
324 writeLayer(layerinfo, new File(dir, name), template);
325 }
326 catch (FileNotFoundException fnfe) {
327 logger.error(fnfe, fnfe);
328 logger.warn("Unable to write layer: " + name);
329 }
330 }
331
332 @Override
333 protected String getMapfilePath() {
334 return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_MAPFILE_PATH);
335 }
336
337 @Override
338 protected String getMapfileTemplate() {
339 return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_MAPFILE_TEMPLATE);
340 }
341
342 }

http://dive4elements.wald.intevation.org