Mercurial > mxd2map
changeset 259:8fe9ccc77962
Introduced a MapScript helper class and improved metadata handling.
author | raimund renkert <raimund.renkert@intevation.de> |
---|---|
date | Tue, 16 Aug 2011 13:09:57 +0200 |
parents | 4bae15d560d3 |
children | 10e0aa283217 |
files | ChangeLog src/java/de/intevation/mxd/utils/MapScriptUtils.java src/java/de/intevation/mxd/writer/MapScriptWriter.java |
diffstat | 3 files changed, 197 insertions(+), 66 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Mon Aug 15 15:52:30 2011 +0200 +++ b/ChangeLog Tue Aug 16 13:09:57 2011 +0200 @@ -1,3 +1,14 @@ +2011-08-16 Raimund Renkert <raimund.renkert@intevation.de> + + * src/java/de/intevation/mxd/writer/MapScriptWriter.java: + Improved metadata handling and moved the string manipulation methods + to new class MapScriptUtils. + + * src/java/de/intevation/mxd/utils/MapScriptUtils.java: + New. Moved string manipulation methods to this class and implemented + a method to read metadata from map objects without running into not + catchable errors. + 2011-08-15 Raimund Renkert <raimund.renkert@intevation.de> * src/java/de/intevation/mxd/writer/MapScriptWriter.java:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/java/de/intevation/mxd/utils/MapScriptUtils.java Tue Aug 16 13:09:57 2011 +0200 @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de> + * + * This file is part of MXD2map. + * + * This program is free software under the LGPL (>=v2.1) + * Read the file LICENCE.txt coming with the software for details + * or visit http://www.gnu.org/licenses/ if it does not exist. + * + * MXD2map has been developed on behalf of the + * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg + * by Intevation GmbH. + * + * Authors: + * Raimund Renkert <raimund.renkert@intevation.de> + * Bjoern Schilberg <bjoern.schilberg@intevation.de> + * Stephan Holl <stephan.holl@intevation.de> + */ + +package de.intevation.mxd.utils; + +import org.apache.log4j.Logger; + +import edu.umn.gis.mapscript.mapObj; + +/** + * MapScript utilities. + * This utility class provides some useful functions when working with the + * MapScript Java API. + * + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +public class MapScriptUtils +{ + /** + * The Logger. + */ + private static final Logger logger = Logger.getLogger(MapScriptUtils.class); + + + /** + * This function is a wrapper for mapObj.getMetaData. + * The mapObj.getMetaData raises a not catchable error if the metadata key + * can not be found in the map. To avoid this, use this function instead. + * + * @param map The map object. + * @param key The metadata key. + * + * @return Empty string if the key was not found, else the value. + */ + public String getMetaData (mapObj map, String key) { + logger.debug("getMetaData(mapObj, String)"); + String meta = map.getFirstMetaDataKey(); + if(meta.equals("")) { + return ""; + } + while(meta != null && !meta.equals(key)) { + meta = map.getNextMetaDataKey(meta); + } + if(meta == null || meta.equals("")) { + return ""; + } + else { + return map.getMetaData(meta); + } + } + + + /** + * Removes special characters and whitespaces. + * + * @param s The input string. + * + * @return The input string without special chars. + */ + public String removeSpecialChars (String s) { + if(s.equals("")) { + return ""; + } + + String tmp = s.trim(); + tmp = tmp.replace(" ", ""); + tmp = tmp.replace("/", ""); + tmp = tmp.replace(",", ""); + tmp = tmp.replace("#", ""); + tmp = tmp.replace("+", ""); + tmp = tmp.replace("~", ""); + tmp = tmp.replace("(", ""); + tmp = tmp.replace(")", ""); + tmp = tmp.replace("{", ""); + tmp = tmp.replace("}", ""); + tmp = tmp.replace("[", ""); + tmp = tmp.replace("]", ""); + tmp = tmp.replace("<", ""); + tmp = tmp.replace(">", ""); + tmp = tmp.replace("*", ""); + tmp = tmp.replace("\\\\", ""); + tmp = tmp.replace("^", ""); + if(!s.equals(tmp)) { + logger.info("Renamed \"" + s +"\" to \"" + tmp +"\"."); + } + return tmp; + } + + + /** + * Replaces german umlauts and removes leading and trailing whitespaces. + * + * @param s The input string. + * + * @return The string without german umlauts. + */ + public String replaceUmlauts (String s) { + if (s.equals("")) { + return ""; + } + String tmp = s.trim(); + tmp = tmp.replace ("ö", "oe"); + tmp = tmp.replace ("ä", "ae"); + tmp = tmp.replace ("ü", "ue"); + tmp = tmp.replace ("ß", "ss"); + if(!s.equals(tmp)) { + logger.info("Renamed \"" + s + "\" to \"" + tmp + "\"."); + } + return tmp; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java Mon Aug 15 15:52:30 2011 +0200 +++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java Tue Aug 16 13:09:57 2011 +0200 @@ -46,6 +46,7 @@ import edu.umn.gis.mapscript.MS_POSITIONS_ENUM; import de.intevation.mxd.utils.XMLUtils; +import de.intevation.mxd.utils.MapScriptUtils; /** * The Mapfile Writer. @@ -66,6 +67,7 @@ */ private Document root; private mapObj map; + private MapScriptUtils msutils; private String mapFilename; private String MS_BINDIR = "c:/ms_6.1-dev/bin"; private String prefix = ""; @@ -77,6 +79,7 @@ */ public MapScriptWriter() { map = new mapObj(""); + msutils = new MapScriptUtils(); mapFilename = ""; } @@ -91,6 +94,7 @@ public MapScriptWriter(String templ, String filename) { String path = System.getProperty("user.dir"); map = new mapObj(templ); + msutils = new MapScriptUtils(); File f = new File(filename); mapFilename = filename; if(f.isAbsolute()) { @@ -206,11 +210,31 @@ } map.setUnits(units); - String srs = map.getMetaData("ows_srs"); String mproj = mapNode.getAttribute("projection"); - if(srs.indexOf(mproj) < 0) { - srs += " EPSG:" + mproj; - map.setMetaData("ows_srs", srs); + if(mproj != null && !mproj.equals("") && ! mproj.equals("0")) { + MapScriptUtils msu = new MapScriptUtils(); + String wmssrs = msu.getMetaData(map, "wms_srs"); + String owssrs = msu.getMetaData(map, "ows_srs"); + if(wmssrs.indexOf(mproj) < 0) { + if(wmssrs.equals("")) { + wmssrs = "EPSG:"; + } + else { + wmssrs += " EPSG:"; + } + wmssrs += mproj; + } + if(owssrs.indexOf(mproj) < 0) { + if(owssrs.equals("")) { + owssrs = "EPSG:"; + } + else { + owssrs += " EPSG:"; + } + owssrs += mproj; + } + map.setMetaData("ows_srs", owssrs); + map.setMetaData("wms_srs", wmssrs); } } @@ -244,14 +268,15 @@ String cleangroup = ""; for(int j = 0; j < splitted.length; j++) { if(!splitted[j].equals("")) { - splitted[j] = validateString(splitted[j]); - cleangroup += "/" + validateLayerName(splitted[j]); + splitted[j] = msutils.replaceUmlauts(splitted[j]); + cleangroup += "/" + + msutils.removeSpecialChars(splitted[j]); } } group = cleangroup; } String lname = layerElement.getAttribute("name"); - lname = validateLayerName(lname); + lname = msutils.removeSpecialChars(lname); String ulname = group.replaceAll("/", ".") + "." + lname; if(ulname.startsWith(".")) { ulname = ulname.substring(1); @@ -263,6 +288,30 @@ } layer.setMetaData("wms_title", ulname); + // Projection metadata. + String mproj = mapNode.getAttribute("projection"); + if(mproj != null && !mproj.equals("") && !mproj.equals("0")) { + String wmssrs = layer.getMetaData("wms_srs"); + String owssrs = layer.getMetaData("ows_srs"); + if(wmssrs == null) { + wmssrs = "EPSG:"; + } + else { + wmssrs += " EPSG:"; + } + if(owssrs == null) { + owssrs = "EPSG:"; + } + else { + owssrs += " EPSG:"; + } + wmssrs += mproj; + owssrs += mproj; + layer.setMetaData("wms_srs", wmssrs); + layer.setMetaData("ows_srs", owssrs); + } + + // The layer extent metadata. if(layerElement.hasAttribute("extent_min_x") && layerElement.hasAttribute("extent_max_x") && layerElement.hasAttribute("extent_min_y") && @@ -475,10 +524,10 @@ Element classElement = (Element)list.item(i); classObj co = new classObj(layer); String name = classElement.getAttribute("label"); - name = validateString(name); + name = msutils.replaceUmlauts(name); if (name.equals("")) { name = layerElement.getAttribute("name"); - name = validateString(name); + name = msutils.replaceUmlauts(name); if (list.getLength() > 1) { name += "-" + i; } @@ -703,61 +752,4 @@ } return expression; } - - /** - * Replaces german umlauts and removes leading and trailing whitespaces. - * - * @param s String - */ - private String validateString (String s) { - if (s.equals("")) { - return ""; - } - String tmp = s.trim(); - tmp = tmp.replace ("ö", "oe"); - tmp = tmp.replace ("ä", "ae"); - tmp = tmp.replace ("ü", "ue"); - tmp = tmp.replace ("ß", "ss"); - if(!s.equals(tmp)) { - logger.info("Renamed \"" + s + "\" to \"" + tmp + "\"."); - } - return tmp; - } - - - /** - * Replaces special characters and removes leading and trailing whitespaces. - * - * @param s String - * - * @return the input string without special chars. - */ - private String validateLayerName (String s) { - if(s.equals("")) { - return ""; - } - - String tmp = s.trim(); - tmp = tmp.replace(" ", ""); - tmp = tmp.replace("/", ""); - tmp = tmp.replace(",", ""); - tmp = tmp.replace("#", ""); - tmp = tmp.replace("+", ""); - tmp = tmp.replace("~", ""); - tmp = tmp.replace("(", ""); - tmp = tmp.replace(")", ""); - tmp = tmp.replace("{", ""); - tmp = tmp.replace("}", ""); - tmp = tmp.replace("[", ""); - tmp = tmp.replace("]", ""); - tmp = tmp.replace("<", ""); - tmp = tmp.replace(">", ""); - tmp = tmp.replace("*", ""); - tmp = tmp.replace("\\\\", ""); - tmp = tmp.replace("^", ""); - if(!s.equals(tmp)) { - logger.info("Renamed \"" + s +"\" to \"" + tmp +"\"."); - } - return tmp; - } }