[MXD2map-commits] r339:ef1cac2854e3

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Nov 8 12:46:20 CET 2012


details:   http://hg.intevation.org/geospatial/mxd2map/rev/ef1cac2854e3

changeset: 339:ef1cac2854e3

user:      Andre Heinecke <aheinecke at intevation.de>

date:      Thu Nov 08 12:46:16 2012 +0100

description:
* src/java/de/intevation/mxd/writer/MapScriptWriter.java:
    Use Relative Paths in Mapfile


diffstat:

 src/java/de/intevation/mxd/writer/MapScriptWriter.java |  58 +++++++++++++++++-
 1 files changed, 57 insertions(+), 1 deletions(-)

diffs (87 lines):

diff -r b3048df06b66 -r ef1cac2854e3 src/java/de/intevation/mxd/writer/MapScriptWriter.java
--- 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;
+        }
+    }
 }


More information about the MXD2map-commits mailing list