teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.utils; christian@4702: teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5831: import org.dive4elements.river.artifacts.access.RiverAccess; teichmann@5831: import org.dive4elements.river.artifacts.model.LayerInfo; teichmann@5831: import org.dive4elements.river.artifacts.model.map.WMSDBLayerFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.map.WMSLayerFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.map.WSPLGENLayerFacet; teichmann@5831: import org.dive4elements.river.artifacts.resources.Resources; christian@4702: christian@4702: import java.io.File; christian@4702: import java.io.FileNotFoundException; christian@4702: import java.io.IOException; christian@4702: import java.util.List; christian@4702: christian@4702: import org.apache.log4j.Logger; christian@4702: import org.apache.velocity.Template; christian@4702: import org.geotools.data.shapefile.ShpFiles; christian@4702: import org.geotools.data.shapefile.shp.ShapefileHeader; christian@4702: import org.geotools.data.shapefile.shp.ShapefileReader; christian@4702: christian@4702: public class ArtifactMapfileGenerator extends MapfileGenerator { christian@4702: christian@4702: private static Logger logger = Logger.getLogger(ArtifactMapfileGenerator.class); christian@4702: christian@4702: @Override christian@4702: protected String getVelocityLogfile() { teichmann@5865: return RiverUtils.getXPathString(RiverUtils.XPATH_FLOODMAP_VELOCITY_LOGFILE); christian@4702: } christian@4702: christian@4702: @Override christian@4702: protected String getMapserverTemplatePath() { teichmann@5865: return RiverUtils.getXPathString(RiverUtils.XPATH_FLOODMAP_MAPSERVER_TEMPLATE_PATH); christian@4702: } christian@4702: christian@4702: @Override rrenkert@5309: public String getMapserverUrl() { teichmann@5865: return RiverUtils.getXPathString(RiverUtils.XPATH_FLOODMAP_MAPSERVER_URL); christian@4702: } christian@4702: christian@4702: /** christian@4702: * Method which starts searching for meta information file and mapfile christian@4702: * generation. christian@4702: */ christian@4702: @Override christian@4702: public void generate() throws IOException christian@4702: { christian@4702: File[] userDirs = getUserDirs(); christian@4702: List layers = parseLayers(userDirs); christian@4702: logger.info("Found " + layers.size() + " layers for user mapfile."); christian@4702: christian@4702: writeMapfile(layers); christian@4702: } christian@4702: christian@4702: /** christian@4702: * Creates a layer file used for Mapserver's mapfile which represents the christian@4702: * floodmap. christian@4702: * teichmann@5867: * @param flys The D4EArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ christian@4702: public void createUeskLayer( teichmann@5867: D4EArtifact flys, christian@4702: WSPLGENLayerFacet wms, christian@4702: String style, christian@4702: CallContext context christian@4702: ) throws FileNotFoundException, IOException christian@4702: { christian@4702: logger.debug("createUeskLayer"); christian@4702: christian@4702: LayerInfo layerinfo = new LayerInfo(); christian@4702: layerinfo.setName(MS_WSPLGEN_PREFIX + flys.identifier()); christian@4702: layerinfo.setType("POLYGON"); christian@4702: layerinfo.setDirectory(flys.identifier()); christian@4702: layerinfo.setData(WSPLGEN_RESULT_SHAPE); christian@4702: layerinfo.setTitle(Resources.getMsg(Resources.getLocale(context.getMeta()), christian@4702: "floodmap.uesk", christian@4702: "Floodmap")); christian@4702: layerinfo.setStyle(style); rrenkert@4994: RiverAccess access = new RiverAccess(flys); rrenkert@4994: String river = access.getRiver(); teichmann@5865: layerinfo.setSrid(RiverUtils.getRiverDGMSrid(river)); christian@4702: christian@4702: String name = MS_LAYER_PREFIX + wms.getName(); christian@4702: christian@4702: Template template = getTemplateByName(WSPLGEN_LAYER_TEMPLATE); christian@4702: if (template == null) { christian@4702: logger.warn("Template '" + WSPLGEN_LAYER_TEMPLATE + "' found."); christian@4702: return; christian@4702: } christian@4702: christian@4702: try { christian@4702: File dir = new File(getShapefileBaseDir(), flys.identifier()); christian@4702: writeLayer(layerinfo, new File(dir, name), template); christian@4702: } christian@4702: catch (FileNotFoundException fnfe) { christian@4702: logger.error(fnfe, fnfe); christian@4702: logger.warn("Unable to write layer: " + name); christian@4702: } christian@4702: } christian@4702: christian@4702: christian@4702: /** christian@4702: * Creates a layer file used for Mapserver's mapfile which represents the christian@4702: * user defined barriers. christian@4702: * teichmann@5867: * @param flys The D4EArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ teichmann@5867: public void createBarriersLayer(D4EArtifact flys, WMSLayerFacet wms) christian@4702: throws FileNotFoundException, IOException christian@4702: { christian@4702: logger.debug("createBarriersLayer"); christian@4702: christian@4702: //String uuid = flys.identifier(); christian@4702: //File dir = new File(getShapefileBaseDir(), uuid); christian@4702: christian@4702: createBarriersLineLayer(flys, wms); christian@4702: createBarriersPolygonLayer(flys, wms); christian@4702: } christian@4702: christian@4702: christian@4702: protected void createBarriersLineLayer( teichmann@5867: D4EArtifact flys, christian@4702: WMSLayerFacet wms christian@4702: ) christian@4702: throws FileNotFoundException, IOException christian@4702: { christian@4702: String uuid = flys.identifier(); christian@4702: String group = MS_BARRIERS_PREFIX + uuid; christian@4702: String groupTitle = "I18N_BARRIERS_TITLE"; christian@4702: christian@4702: File dir = new File(getShapefileBaseDir(), uuid); christian@4702: File test = new File(dir, WSPLGEN_LINES_SHAPE); christian@4702: christian@4702: if (!test.exists() || !test.canRead()) { christian@4702: logger.debug("No barrier line layer existing."); christian@4702: return; christian@4702: } christian@4702: christian@4702: LayerInfo lineInfo = new LayerInfo(); christian@4702: lineInfo.setName(MS_LINE_PREFIX + uuid); christian@4702: lineInfo.setType("LINE"); christian@4702: lineInfo.setDirectory(uuid); christian@4702: lineInfo.setData(WSPLGEN_LINES_SHAPE); christian@4702: lineInfo.setTitle("I18N_LINE_SHAPE"); christian@4702: lineInfo.setGroup(group); christian@4702: lineInfo.setGroupTitle(groupTitle); christian@4702: lineInfo.setSrid(wms.getSrid()); christian@4702: christian@4702: String nameLines = MS_LAYER_PREFIX + wms.getName() + "-lines"; christian@4702: christian@4702: Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); christian@4702: if (tpl == null) { christian@4702: logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); christian@4702: return; christian@4702: } christian@4702: christian@4702: try { christian@4702: writeLayer(lineInfo, new File(dir, nameLines), tpl); christian@4702: } christian@4702: catch (FileNotFoundException fnfe) { christian@4702: logger.error(fnfe, fnfe); christian@4702: logger.warn("Unable to write layer: " + nameLines); christian@4702: } christian@4702: } christian@4702: christian@4702: protected void createBarriersPolygonLayer( teichmann@5867: D4EArtifact flys, christian@4702: WMSLayerFacet wms christian@4702: ) christian@4702: throws FileNotFoundException, IOException christian@4702: { christian@4702: String uuid = flys.identifier(); christian@4702: String group = uuid + MS_BARRIERS_PREFIX; christian@4702: String groupTitle = "I18N_BARRIERS_TITLE"; christian@4702: christian@4702: File dir = new File(getShapefileBaseDir(), uuid); christian@4702: File test = new File(dir, WSPLGEN_POLYGONS_SHAPE); christian@4702: christian@4702: if (!test.exists() || !test.canRead()) { christian@4702: logger.debug("No barrier line layer existing."); christian@4702: return; christian@4702: } christian@4702: christian@4702: LayerInfo polygonInfo = new LayerInfo(); christian@4702: polygonInfo.setName(MS_POLYGONS_PREFIX + uuid); christian@4702: polygonInfo.setType("POLYGON"); christian@4702: polygonInfo.setDirectory(uuid); christian@4702: polygonInfo.setData(WSPLGEN_POLYGONS_SHAPE); christian@4702: polygonInfo.setTitle("I18N_POLYGON_SHAPE"); christian@4702: polygonInfo.setGroup(group); christian@4702: polygonInfo.setGroupTitle(groupTitle); christian@4702: polygonInfo.setSrid(wms.getSrid()); christian@4702: christian@4702: String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons"; christian@4702: christian@4702: Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); christian@4702: if (tpl == null) { christian@4702: logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); christian@4702: return; christian@4702: } christian@4702: christian@4702: try { christian@4702: writeLayer(polygonInfo, new File(dir, namePolygons), tpl); christian@4702: } christian@4702: catch (FileNotFoundException fnfe) { christian@4702: logger.error(fnfe, fnfe); christian@4702: logger.warn("Unable to write layer: " + namePolygons); christian@4702: } christian@4702: } christian@4702: christian@4702: christian@4702: /** christian@4702: * Creates a layer file used for Mapserver's mapfile which represents the christian@4702: * shape files uploaded by the user. christian@4702: * teichmann@5867: * @param flys The D4EArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ teichmann@5867: public void createUserShapeLayer(D4EArtifact flys, WMSLayerFacet wms) christian@4702: throws FileNotFoundException, IOException christian@4702: { christian@4702: logger.debug("createUserShapeLayer"); christian@4702: christian@4702: String uuid = flys.identifier(); christian@4702: File dir = new File(getShapefileBaseDir(), uuid); christian@4702: File test = new File(dir, WSPLGEN_USER_SHAPE); christian@4702: christian@4702: if (!test.exists() || !test.canRead()) { christian@4702: logger.debug("No user layer existing."); christian@4702: return; christian@4702: } christian@4702: christian@4702: File userShape = new File(dir, WSPLGEN_USER_SHAPE); christian@4702: ShpFiles sf = new ShpFiles(userShape); christian@4702: ShapefileReader sfr = new ShapefileReader(sf, true, false, null); christian@4702: ShapefileHeader sfh = sfr.getHeader(); christian@4702: christian@4702: String group = uuid + MS_USERSHAPE_PREFIX; christian@4702: String groupTitle = "I18N_USER_SHAPE_TITLE"; christian@4702: christian@4702: LayerInfo info = new LayerInfo(); christian@4702: info.setName(MS_USERSHAPE_PREFIX + uuid); christian@4702: if (sfh.getShapeType().isLineType()) { christian@4702: info.setType("LINE"); christian@4702: } christian@4702: else if (sfh.getShapeType().isPolygonType()) { christian@4702: info.setType("POLYGON"); christian@4702: } christian@4702: else { christian@4702: return; christian@4702: } christian@4702: info.setDirectory(uuid); christian@4702: info.setData(WSPLGEN_USER_SHAPE); christian@4702: info.setTitle("I18N_USER_SHAPE"); christian@4702: info.setGroup(group); christian@4702: info.setGroupTitle(groupTitle); christian@4702: info.setSrid(wms.getSrid()); christian@4702: christian@4702: String nameUser = MS_LAYER_PREFIX + wms.getName(); christian@4702: christian@4702: Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); christian@4702: if (tpl == null) { christian@4702: logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); christian@4702: return; christian@4702: } christian@4702: christian@4702: try { christian@4702: writeLayer(info, new File(dir, nameUser), tpl); christian@4702: } christian@4702: catch (FileNotFoundException fnfe) { christian@4702: logger.error(fnfe, fnfe); christian@4702: logger.warn("Unable to write layer: " + nameUser); christian@4702: } christian@4702: christian@4702: } christian@4702: christian@4702: christian@4702: /** christian@4702: * Creates a layer file used for Mapserver's mapfile which represents christian@4702: * geometries from database. christian@4702: * teichmann@5867: * @param flys The D4EArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ christian@4702: public void createDatabaseLayer( teichmann@5867: D4EArtifact flys, christian@4702: WMSDBLayerFacet wms, christian@4702: String style christian@4702: ) christian@4702: throws FileNotFoundException, IOException christian@4702: { christian@4702: logger.debug("createDatabaseLayer"); christian@4702: christian@4702: LayerInfo layerinfo = new LayerInfo(); christian@4702: layerinfo.setName(wms.getName() + "-" + flys.identifier()); christian@4702: layerinfo.setType(wms.getGeometryType()); christian@4702: layerinfo.setFilter(wms.getFilter()); christian@4702: layerinfo.setData(wms.getData()); christian@4702: layerinfo.setTitle(wms.getDescription()); christian@4702: layerinfo.setStyle(style); christian@4702: if(wms.getExtent() != null) { christian@4702: layerinfo.setExtent(GeometryUtils.jtsBoundsToOLBounds(wms.getExtent())); christian@4702: } christian@4702: layerinfo.setConnection(wms.getConnection()); christian@4702: layerinfo.setConnectionType(wms.getConnectionType()); christian@4702: layerinfo.setLabelItem(wms.getLabelItem()); christian@4702: layerinfo.setSrid(wms.getSrid()); christian@4702: christian@4702: String name = MS_LAYER_PREFIX + wms.getName(); christian@4702: christian@4702: Template template = getTemplateByName(DB_LAYER_TEMPLATE); christian@4702: if (template == null) { christian@4702: logger.warn("Template '" + DB_LAYER_TEMPLATE + "' found."); christian@4702: return; christian@4702: } christian@4702: christian@4702: try { christian@4702: File dir = new File(getShapefileBaseDir(), flys.identifier()); christian@4702: writeLayer(layerinfo, new File(dir, name), template); christian@4702: } christian@4702: catch (FileNotFoundException fnfe) { christian@4702: logger.error(fnfe, fnfe); christian@4702: logger.warn("Unable to write layer: " + name); christian@4702: } christian@4702: } christian@4702: christian@4702: @Override christian@4702: protected String getMapfilePath() { teichmann@5865: return RiverUtils.getXPathString(RiverUtils.XPATH_FLOODMAP_MAPFILE_PATH); christian@4702: } christian@4702: christian@4702: @Override christian@4702: protected String getMapfileTemplate() { teichmann@5865: return RiverUtils.getXPathString(RiverUtils.XPATH_FLOODMAP_MAPFILE_TEMPLATE); christian@4702: } christian@4702: christian@4702: }