christian@4702: package de.intevation.flys.utils; christian@4702: christian@4702: import de.intevation.artifacts.CallContext; christian@4702: import de.intevation.flys.artifacts.FLYSArtifact; christian@4702: import de.intevation.flys.artifacts.model.LayerInfo; christian@4702: import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet; christian@4702: import de.intevation.flys.artifacts.model.map.WMSLayerFacet; christian@4702: import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet; christian@4702: import de.intevation.flys.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() { christian@4702: return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_VELOCITY_LOGFILE); christian@4702: } christian@4702: christian@4702: @Override christian@4702: protected String getMapserverTemplatePath() { christian@4702: return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_MAPSERVER_TEMPLATE_PATH); christian@4702: } christian@4702: christian@4702: @Override christian@4702: protected String getMapserverUrl() { christian@4702: return FLYSUtils.getXPathString(FLYSUtils.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: * christian@4702: * @param flys The FLYSArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ christian@4702: public void createUeskLayer( christian@4702: FLYSArtifact 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); christian@4702: layerinfo.setSrid(wms.getSrid()); 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: * christian@4702: * @param flys The FLYSArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ christian@4702: public void createBarriersLayer(FLYSArtifact 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( christian@4702: FLYSArtifact 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( christian@4702: FLYSArtifact 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: * christian@4702: * @param flys The FLYSArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ christian@4702: public void createUserShapeLayer(FLYSArtifact 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: * christian@4702: * @param flys The FLYSArtifact that owns wms. christian@4702: * @param wms The WMSLayerFacet that contains information for the layer. christian@4702: */ christian@4702: public void createDatabaseLayer( christian@4702: FLYSArtifact 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() { christian@4702: return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_MAPFILE_PATH); christian@4702: } christian@4702: christian@4702: @Override christian@4702: protected String getMapfileTemplate() { christian@4702: return FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_MAPFILE_TEMPLATE); christian@4702: } christian@4702: christian@4702: }