changeset 314:cd3cb1a7f35a

Workaround a bug in ArcGIS causing FieldNames of shape files that are 11 Characters long to be postfixed with their Type Identifier.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 17 Sep 2012 17:02:15 +0200
parents 9bb629743d2c
children 1d77ea6a915d
files ChangeLog src/java/de/intevation/mxd/writer/MapScriptWriter.java
diffstat 2 files changed, 111 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Sep 14 17:03:54 2012 +0200
+++ b/ChangeLog	Mon Sep 17 17:02:15 2012 +0200
@@ -1,3 +1,9 @@
+2012-09-17  Andre Heinecke <aheinecke@intevation.de>
+
+	* src/java/de/intevation/mxd/writer/MapScriptWriter.java
+	(sanitzeAttribute): New. Factored out adding/removing of prefix
+	and added code to workaround a Field name bug in ArcGIS
+
 2012-09-14  Andre Heinecke <aheinecke@intevation.de>
 
 	* src/java/de/intevation/mxd/writer/MapScriptWriter.java:
--- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Fri Sep 14 17:03:54 2012 +0200
+++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Mon Sep 17 17:02:15 2012 +0200
@@ -478,6 +478,9 @@
                 }
                 else {
                     prefix = layerElement.getAttribute("data_source");
+                    if (!prefix.isEmpty()) {
+                        prefix += ".";
+                    }
                     if(layerElement.hasAttribute("definition_query") &&
                        !layerElement.getAttribute("definition_query")
                                      .equals("")) {
@@ -554,7 +557,12 @@
                     // mapserver
                     String unwantedPrefix = layerElement.getAttribute("data") + ".";
                     logger.debug("Removing expression Prefix: " + unwantedPrefix);
-                    expr = expr.replace(unwantedPrefix, "");
+                    boolean isShapeFile = false;
+                    if (layerElement.getAttribute("connection_type") == "local") {
+                        isShapeFile = true;
+                    }
+                    expr = sanitizeAttribute(expr,
+                                      unwantedPrefix, prefix, isShapeFile);
                 }
                 if(!prefix.equals("")) {
                     expr = prefix + "." + expr;
@@ -569,6 +577,39 @@
     }
 
     /**
+     * Adds the elements (Features) to the layer.
+     * @param layer        Mapscript layer object.
+     * @param layerElement Dom element containing the feature attributes.
+     */
+    private void writeFeatures(layerObj layer, Element layerElement) {
+        NodeList list = layerElement.getElementsByTagName("feature");
+        for(int i = 0; i < list.getLength(); i++) {
+            logger.debug("Writing an element");
+            Element feature = (Element)list.item(i);
+            pointObj poi = new pointObj(
+                      Double.parseDouble(feature.getAttribute("X")),
+                      Double.parseDouble(feature.getAttribute("Y")),
+                      0.0, -2e38);
+            lineObj line = new lineObj();
+            line.add(poi);
+            logger.debug("Set the line");
+
+            shapeObj shape = new shapeObj(MS_SHAPE_TYPE.MS_SHAPE_POINT.swigValue());
+            shape.add(line);
+            shape.setText(feature.getAttribute("text"));
+
+            // Write the Style / Symbol once
+            if ( i == 0 ) {
+                classObj co = new classObj(layer);
+                writeLabel(co, feature, layerElement.getAttribute("type"));
+                MarkerStyleWriter swriter = new MarkerStyleWriter (this.map, co);
+                swriter.write((Element)feature.getFirstChild());
+            }
+            layer.addFeature(shape);
+        }
+    }
+
+    /**
      * Adds the classes to the layer.
      * @param layer        Mapscript layer object.
      * @param layerElement Dom element containing the class attributes.
@@ -606,7 +647,11 @@
             //Create definition expression.
             if(classElement.hasAttribute("field_count")) {
                 String unwantedPrefix = layerElement.getAttribute("data") + ".";
-                co.setExpression(createExpression(classElement, i, unwantedPrefix));
+                boolean isShapeFile = false;
+                if (layerElement.getAttribute("connection_type") == "local") {
+                    isShapeFile = true;
+                }
+                co.setExpression(createExpression(classElement, i, unwantedPrefix, isShapeFile));
             }
 
             //Write symbols and styles.
@@ -709,6 +754,46 @@
         }
     }
 
+    /** Sanitze an attribute
+     *
+     * Fixes invalid Field names and/or adds and removes a prefix
+     *
+     * @param attribute The Attribute to sanitize
+     * @param prefixToRemove A Prefix that should be removed (empty otherwise)
+     * @param prefixToAdd A Prefix that should be added (empty otherwise)
+     * @param enforceShpLimit True if the attribute should follow shapefile
+     * format restrictions.
+     */
+    private String sanitizeAttribute(String attribute, String prefixToRemove,
+                                     String prefixToAdd, boolean enforceShpLimit) {
+        if (enforceShpLimit) {
+            /* Workaround for non standard conform shapefile implementations
+             * like the one used by ArcGis that appends the field type after the
+             * eleventh character. Although the standard actually defines 10
+             * as the maximum length of Field Names GDAL handles the case where
+             * the terminating 0 is actually a Character allowing for Field
+             * names of up to 11 characters.
+             */
+            if (attribute.length() > 10) {
+                // Just remove the Type Delimiter
+                if (attribute.endsWith("C") ||
+                    attribute.endsWith("D") ||
+                    attribute.endsWith("N") ||
+                    attribute.endsWith("L") ||
+                    attribute.endsWith("M")) {
+                        attribute = attribute.substring(0, attribute.length() - 1);
+                    }
+            }
+        }
+        if (!prefixToRemove.isEmpty() && attribute.startsWith(prefixToRemove)) {
+            attribute = attribute.replace(prefixToRemove, "");
+        }
+        if (!prefixToAdd.isEmpty() && !attribute.startsWith(prefixToAdd)) {
+            attribute = prefixToAdd + attribute;
+        }
+        return attribute;
+    }
+
     /**
      * Create definition expression.
      *
@@ -716,8 +801,10 @@
      * @param index Index to determine the correct expression operator.
      * @param prefixToRemove A string that will be removed from the start of
      * expression fields.
+     * @param isShapeFile true if shapefile attribute limits should be enforced
      */
-    private String createExpression(Element ce, int index, String prefixToRemove) {
+    private String createExpression(Element ce, int index, String prefixToRemove,
+            boolean isShapeFile) {
         String expression = "(";
         int count = 0;
         try {
@@ -732,14 +819,9 @@
            try {
                 //If no exception is thrown, the expression value is a number.
                 Double.parseDouble(ce.getAttribute("value"));
-                String exp = ce.getAttribute("expression_field_0");
-                String pre = "";
-                if (!prefix.equals("") && !exp.startsWith(prefix)) {
-                    pre = prefix + ".";
-                }
-                expression += "[" + pre;
-                expression += ce.getAttribute("expression_field_0").replace(
-                        prefixToRemove, "") + "]";
+                expression += "[" + 
+                    sanitizeAttribute(ce.getAttribute("expression_field_0"),
+                                      prefixToRemove, prefix, isShapeFile) + "]";
                 if(ce.hasAttribute("min_value")) {
                     if(index == 0) {
                         expression += " >= " + ce.getAttribute("min_value");
@@ -747,9 +829,9 @@
                     else {
                         expression += " > " + ce.getAttribute("min_value");
                     }
-                    expression += " AND [" + pre;
-                    expression += ce.getAttribute("expression_field_0").replace(
-                        prefixToRemove, "");
+                    expression += " AND [" + 
+                    sanitizeAttribute(ce.getAttribute("expression_field_0"),
+                                      prefixToRemove, prefix, isShapeFile) + "]";
                     expression += "]";
                 }
                 expression += " " + ce.getAttribute("expression_operator");
@@ -757,15 +839,9 @@
             }
             catch(NumberFormatException nfe) {
                 //The expression value is a strings.
-                String exp = ce.getAttribute("expression_field_0").replace(
-                        prefixToRemove, "");
-                String pre = "";
-                if (!prefix.equals("") && !exp.startsWith(prefix)) {
-                    pre = prefix + ".";
-                }
-                expression += "\"[" + pre;
-                expression += ce.getAttribute("expression_field_0").replace(
-                        prefixToRemove, "") + "]\"";
+                expression += "\"[";
+                expression += sanitizeAttribute(ce.getAttribute("expression_field_0"),
+                                      prefixToRemove, prefix, isShapeFile) + "]";
                 expression += " " + ce.getAttribute("expression_operator");
                 expression += " \"" + ce.getAttribute("value") + "\")";
             }
@@ -780,15 +856,9 @@
                     //If no exception is thrown, the expression values are
                     //numbers.
                     Double.parseDouble(ce.getAttribute("value_" + i));
-                    String exp = ce.getAttribute("expression_field_" + i).replace(
-                        prefixToRemove, "");
-                    String pre = "";
-                    if (!prefix.equals("") && !exp.startsWith(prefix)) {
-                       pre = prefix + ".";
-                    }
-                    expression += "[" + pre;
-                    expression += ce.getAttribute("expression_field_" + i).replace(
-                        prefixToRemove, "");
+                    expression += "[";
+                    expression += sanitizeAttribute(ce.getAttribute("expression_field_" + i),
+                                      prefixToRemove, prefix, isShapeFile);
                     expression += "]";
                     expression += " " + ce.getAttribute("expression_operator");
                     expression += " " + ce.getAttribute("value_" + i);
@@ -801,15 +871,9 @@
                 }
                 catch (NumberFormatException nfe) {
                     //The expression values are strings.
-                    String exp = ce.getAttribute("expression_field_" + i).replace(
-                        prefixToRemove, "");
-                    String pre = "";
-                    if (!prefix.equals("") && !exp.startsWith(prefix)) {
-                       pre = prefix + ".";
-                    }
-                    expression += "\"[" + pre;
-                    expression += ce.getAttribute("expression_field_" + i).replace(
-                        prefixToRemove, "");
+                    expression += "\"[";
+                    expression += sanitizeAttribute(ce.getAttribute("expression_field_" + i),
+                                      prefixToRemove, prefix, isShapeFile);
                     expression += "]\"";
                     expression += " " + ce.getAttribute("expression_operator");
                     expression += " \"" + ce.getAttribute("value_" + i);
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)