changeset 339:ef1cac2854e3

* src/java/de/intevation/mxd/writer/MapScriptWriter.java: Use Relative Paths in Mapfile
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 08 Nov 2012 12:46:16 +0100
parents b3048df06b66
children dbfcb0b69a63
files src/java/de/intevation/mxd/writer/MapScriptWriter.java
diffstat 1 files changed, 57 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Tue Nov 06 12:08:15 2012 +0100
+++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Thu Nov 08 12:46:16 2012 +0100
@@ -75,6 +75,7 @@
     private String mapFilename;
     private String MS_BINDIR = "c:/mapserver/bin";
     private String prefix = "";
+    private boolean useRelativePaths = true;
     private MS_UNITS units = MS_UNITS.MS_METERS;
 
     /**
@@ -139,6 +140,22 @@
         writeMap();
         //Write the layers.
         boolean ret = writeLayer();
+
+        // Replace the filenames if necessary
+        if (useRelativePaths) {
+            try {
+                map.setFontSet(pathForMapfile(map.getFontset().getFilename()));
+            } catch (Exception e) {
+                // Exception is expected because our working dir is not
+                // necessarily the same as the one of the Mapfile
+            }
+            try {
+                map.setSymbolSet(pathForMapfile(map.getSymbolset().getFilename()));
+            } catch (Exception e) {
+            }
+
+        }
+
         //Save the map.
         if (ret) {
             mapObj cloneMap = map.cloneMap();
@@ -423,7 +440,7 @@
                 }
                 datasource += layerElement.getAttribute("data_source");
                 datasource = datasource.replaceAll("\\\\", "/");
-                layer.setData(datasource);
+                layer.setData(pathForMapfile(datasource));
             }
             else if(con_type.equals("SDE") && !type.equals("raster")) {
                 // The data source is a sde database.
@@ -923,4 +940,43 @@
         }
         return expression;
     }
+
+    /**
+     * Get a Path that should be used in a mapfile.
+     * Depending on the value of useRelativePaths this returns either
+     * an absolute path to a file or a relative location
+     *
+     * @param path The Path that you want to use
+     * @return The Path that should be written to the mapfile
+     */
+    public String pathForMapfile(String path) {
+        if (useRelativePaths) {
+            logger.debug(new File(mapFilename).toURI());
+            logger.debug(new File(path).toURI().getPath());
+            // Find the lowest common directory.
+            // where relativize returns a relative part
+            File candidate = new File(path);
+            File commonPath = new File(mapFilename).getParentFile();
+            File pathfile = new File(path);
+            String dirUps = "";
+            int count = 0;
+            while (commonPath != null && commonPath.getParentFile() != null 
+                    && candidate.isAbsolute()) {
+                candidate = new File(commonPath.toURI().relativize(
+                            pathfile.toURI()).getPath());
+                commonPath = commonPath.getParentFile();
+                count++;
+            }
+            for (int i=0; i < count - 1; i++) {
+                /* The last loop already was correct*/
+                dirUps += "../";
+            }
+            if (candidate.isAbsolute()) {
+                return path;
+            }
+            return dirUps + candidate.getPath();
+        } else {
+            return path;
+        }
+    }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)