comparison src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 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 c8b8f887ef80
children 1d77ea6a915d
comparison
equal deleted inserted replaced
313:9bb629743d2c 314:cd3cb1a7f35a
476 layerElement.getAttribute("join_table") + "." + 476 layerElement.getAttribute("join_table") + "." +
477 layerElement.getAttribute("join_field")); 477 layerElement.getAttribute("join_field"));
478 } 478 }
479 else { 479 else {
480 prefix = layerElement.getAttribute("data_source"); 480 prefix = layerElement.getAttribute("data_source");
481 if (!prefix.isEmpty()) {
482 prefix += ".";
483 }
481 if(layerElement.hasAttribute("definition_query") && 484 if(layerElement.hasAttribute("definition_query") &&
482 !layerElement.getAttribute("definition_query") 485 !layerElement.getAttribute("definition_query")
483 .equals("")) { 486 .equals("")) {
484 layer.setFilter("where " + 487 layer.setFilter("where " +
485 layerElement.getAttribute("definition_query")); 488 layerElement.getAttribute("definition_query"));
552 // ogr expression fields come prefixed from arcgis 555 // ogr expression fields come prefixed from arcgis
553 // the prefix has to be removed in order to work with 556 // the prefix has to be removed in order to work with
554 // mapserver 557 // mapserver
555 String unwantedPrefix = layerElement.getAttribute("data") + "."; 558 String unwantedPrefix = layerElement.getAttribute("data") + ".";
556 logger.debug("Removing expression Prefix: " + unwantedPrefix); 559 logger.debug("Removing expression Prefix: " + unwantedPrefix);
557 expr = expr.replace(unwantedPrefix, ""); 560 boolean isShapeFile = false;
561 if (layerElement.getAttribute("connection_type") == "local") {
562 isShapeFile = true;
563 }
564 expr = sanitizeAttribute(expr,
565 unwantedPrefix, prefix, isShapeFile);
558 } 566 }
559 if(!prefix.equals("")) { 567 if(!prefix.equals("")) {
560 expr = prefix + "." + expr; 568 expr = prefix + "." + expr;
561 } 569 }
562 layer.setLabelitem(expr.trim()); 570 layer.setLabelitem(expr.trim());
564 572
565 //Write classes. 573 //Write classes.
566 writeClass(layer, layerElement); 574 writeClass(layer, layerElement);
567 } 575 }
568 return true; 576 return true;
577 }
578
579 /**
580 * Adds the elements (Features) to the layer.
581 * @param layer Mapscript layer object.
582 * @param layerElement Dom element containing the feature attributes.
583 */
584 private void writeFeatures(layerObj layer, Element layerElement) {
585 NodeList list = layerElement.getElementsByTagName("feature");
586 for(int i = 0; i < list.getLength(); i++) {
587 logger.debug("Writing an element");
588 Element feature = (Element)list.item(i);
589 pointObj poi = new pointObj(
590 Double.parseDouble(feature.getAttribute("X")),
591 Double.parseDouble(feature.getAttribute("Y")),
592 0.0, -2e38);
593 lineObj line = new lineObj();
594 line.add(poi);
595 logger.debug("Set the line");
596
597 shapeObj shape = new shapeObj(MS_SHAPE_TYPE.MS_SHAPE_POINT.swigValue());
598 shape.add(line);
599 shape.setText(feature.getAttribute("text"));
600
601 // Write the Style / Symbol once
602 if ( i == 0 ) {
603 classObj co = new classObj(layer);
604 writeLabel(co, feature, layerElement.getAttribute("type"));
605 MarkerStyleWriter swriter = new MarkerStyleWriter (this.map, co);
606 swriter.write((Element)feature.getFirstChild());
607 }
608 layer.addFeature(shape);
609 }
569 } 610 }
570 611
571 /** 612 /**
572 * Adds the classes to the layer. 613 * Adds the classes to the layer.
573 * @param layer Mapscript layer object. 614 * @param layer Mapscript layer object.
604 } 645 }
605 646
606 //Create definition expression. 647 //Create definition expression.
607 if(classElement.hasAttribute("field_count")) { 648 if(classElement.hasAttribute("field_count")) {
608 String unwantedPrefix = layerElement.getAttribute("data") + "."; 649 String unwantedPrefix = layerElement.getAttribute("data") + ".";
609 co.setExpression(createExpression(classElement, i, unwantedPrefix)); 650 boolean isShapeFile = false;
651 if (layerElement.getAttribute("connection_type") == "local") {
652 isShapeFile = true;
653 }
654 co.setExpression(createExpression(classElement, i, unwantedPrefix, isShapeFile));
610 } 655 }
611 656
612 //Write symbols and styles. 657 //Write symbols and styles.
613 NodeList l = classElement.getChildNodes(); 658 NodeList l = classElement.getChildNodes();
614 for (int j = l.getLength() - 1; j >= 0; j--) { 659 for (int j = l.getLength() - 1; j >= 0; j--) {
707 label.setPosition(MS_POSITIONS_ENUM.MS_UC.swigValue()); 752 label.setPosition(MS_POSITIONS_ENUM.MS_UC.swigValue());
708 } 753 }
709 } 754 }
710 } 755 }
711 756
757 /** Sanitze an attribute
758 *
759 * Fixes invalid Field names and/or adds and removes a prefix
760 *
761 * @param attribute The Attribute to sanitize
762 * @param prefixToRemove A Prefix that should be removed (empty otherwise)
763 * @param prefixToAdd A Prefix that should be added (empty otherwise)
764 * @param enforceShpLimit True if the attribute should follow shapefile
765 * format restrictions.
766 */
767 private String sanitizeAttribute(String attribute, String prefixToRemove,
768 String prefixToAdd, boolean enforceShpLimit) {
769 if (enforceShpLimit) {
770 /* Workaround for non standard conform shapefile implementations
771 * like the one used by ArcGis that appends the field type after the
772 * eleventh character. Although the standard actually defines 10
773 * as the maximum length of Field Names GDAL handles the case where
774 * the terminating 0 is actually a Character allowing for Field
775 * names of up to 11 characters.
776 */
777 if (attribute.length() > 10) {
778 // Just remove the Type Delimiter
779 if (attribute.endsWith("C") ||
780 attribute.endsWith("D") ||
781 attribute.endsWith("N") ||
782 attribute.endsWith("L") ||
783 attribute.endsWith("M")) {
784 attribute = attribute.substring(0, attribute.length() - 1);
785 }
786 }
787 }
788 if (!prefixToRemove.isEmpty() && attribute.startsWith(prefixToRemove)) {
789 attribute = attribute.replace(prefixToRemove, "");
790 }
791 if (!prefixToAdd.isEmpty() && !attribute.startsWith(prefixToAdd)) {
792 attribute = prefixToAdd + attribute;
793 }
794 return attribute;
795 }
796
712 /** 797 /**
713 * Create definition expression. 798 * Create definition expression.
714 * 799 *
715 * @param ce DOM elementcontaining the class expression information. 800 * @param ce DOM elementcontaining the class expression information.
716 * @param index Index to determine the correct expression operator. 801 * @param index Index to determine the correct expression operator.
717 * @param prefixToRemove A string that will be removed from the start of 802 * @param prefixToRemove A string that will be removed from the start of
718 * expression fields. 803 * expression fields.
719 */ 804 * @param isShapeFile true if shapefile attribute limits should be enforced
720 private String createExpression(Element ce, int index, String prefixToRemove) { 805 */
806 private String createExpression(Element ce, int index, String prefixToRemove,
807 boolean isShapeFile) {
721 String expression = "("; 808 String expression = "(";
722 int count = 0; 809 int count = 0;
723 try { 810 try {
724 count = Integer.parseInt(ce.getAttribute("field_count")); 811 count = Integer.parseInt(ce.getAttribute("field_count"));
725 } 812 }
730 if(count == 1) { 817 if(count == 1) {
731 //Create single field expression. 818 //Create single field expression.
732 try { 819 try {
733 //If no exception is thrown, the expression value is a number. 820 //If no exception is thrown, the expression value is a number.
734 Double.parseDouble(ce.getAttribute("value")); 821 Double.parseDouble(ce.getAttribute("value"));
735 String exp = ce.getAttribute("expression_field_0"); 822 expression += "[" +
736 String pre = ""; 823 sanitizeAttribute(ce.getAttribute("expression_field_0"),
737 if (!prefix.equals("") && !exp.startsWith(prefix)) { 824 prefixToRemove, prefix, isShapeFile) + "]";
738 pre = prefix + ".";
739 }
740 expression += "[" + pre;
741 expression += ce.getAttribute("expression_field_0").replace(
742 prefixToRemove, "") + "]";
743 if(ce.hasAttribute("min_value")) { 825 if(ce.hasAttribute("min_value")) {
744 if(index == 0) { 826 if(index == 0) {
745 expression += " >= " + ce.getAttribute("min_value"); 827 expression += " >= " + ce.getAttribute("min_value");
746 } 828 }
747 else { 829 else {
748 expression += " > " + ce.getAttribute("min_value"); 830 expression += " > " + ce.getAttribute("min_value");
749 } 831 }
750 expression += " AND [" + pre; 832 expression += " AND [" +
751 expression += ce.getAttribute("expression_field_0").replace( 833 sanitizeAttribute(ce.getAttribute("expression_field_0"),
752 prefixToRemove, ""); 834 prefixToRemove, prefix, isShapeFile) + "]";
753 expression += "]"; 835 expression += "]";
754 } 836 }
755 expression += " " + ce.getAttribute("expression_operator"); 837 expression += " " + ce.getAttribute("expression_operator");
756 expression += " " + ce.getAttribute("value") + ")"; 838 expression += " " + ce.getAttribute("value") + ")";
757 } 839 }
758 catch(NumberFormatException nfe) { 840 catch(NumberFormatException nfe) {
759 //The expression value is a strings. 841 //The expression value is a strings.
760 String exp = ce.getAttribute("expression_field_0").replace( 842 expression += "\"[";
761 prefixToRemove, ""); 843 expression += sanitizeAttribute(ce.getAttribute("expression_field_0"),
762 String pre = ""; 844 prefixToRemove, prefix, isShapeFile) + "]";
763 if (!prefix.equals("") && !exp.startsWith(prefix)) {
764 pre = prefix + ".";
765 }
766 expression += "\"[" + pre;
767 expression += ce.getAttribute("expression_field_0").replace(
768 prefixToRemove, "") + "]\"";
769 expression += " " + ce.getAttribute("expression_operator"); 845 expression += " " + ce.getAttribute("expression_operator");
770 expression += " \"" + ce.getAttribute("value") + "\")"; 846 expression += " \"" + ce.getAttribute("value") + "\")";
771 } 847 }
772 catch(Exception e) { 848 catch(Exception e) {
773 e.printStackTrace(); 849 e.printStackTrace();
778 for (int i = 0; i < count; i++) { 854 for (int i = 0; i < count; i++) {
779 try { 855 try {
780 //If no exception is thrown, the expression values are 856 //If no exception is thrown, the expression values are
781 //numbers. 857 //numbers.
782 Double.parseDouble(ce.getAttribute("value_" + i)); 858 Double.parseDouble(ce.getAttribute("value_" + i));
783 String exp = ce.getAttribute("expression_field_" + i).replace( 859 expression += "[";
784 prefixToRemove, ""); 860 expression += sanitizeAttribute(ce.getAttribute("expression_field_" + i),
785 String pre = ""; 861 prefixToRemove, prefix, isShapeFile);
786 if (!prefix.equals("") && !exp.startsWith(prefix)) {
787 pre = prefix + ".";
788 }
789 expression += "[" + pre;
790 expression += ce.getAttribute("expression_field_" + i).replace(
791 prefixToRemove, "");
792 expression += "]"; 862 expression += "]";
793 expression += " " + ce.getAttribute("expression_operator"); 863 expression += " " + ce.getAttribute("expression_operator");
794 expression += " " + ce.getAttribute("value_" + i); 864 expression += " " + ce.getAttribute("value_" + i);
795 if (i < count - 1) { 865 if (i < count - 1) {
796 expression += " AND "; 866 expression += " AND ";
799 expression += ")"; 869 expression += ")";
800 } 870 }
801 } 871 }
802 catch (NumberFormatException nfe) { 872 catch (NumberFormatException nfe) {
803 //The expression values are strings. 873 //The expression values are strings.
804 String exp = ce.getAttribute("expression_field_" + i).replace( 874 expression += "\"[";
805 prefixToRemove, ""); 875 expression += sanitizeAttribute(ce.getAttribute("expression_field_" + i),
806 String pre = ""; 876 prefixToRemove, prefix, isShapeFile);
807 if (!prefix.equals("") && !exp.startsWith(prefix)) {
808 pre = prefix + ".";
809 }
810 expression += "\"[" + pre;
811 expression += ce.getAttribute("expression_field_" + i).replace(
812 prefixToRemove, "");
813 expression += "]\""; 877 expression += "]\"";
814 expression += " " + ce.getAttribute("expression_operator"); 878 expression += " " + ce.getAttribute("expression_operator");
815 expression += " \"" + ce.getAttribute("value_" + i); 879 expression += " \"" + ce.getAttribute("value_" + i);
816 if (i < count - 1) { 880 if (i < count - 1) {
817 expression += "\" AND "; 881 expression += "\" AND ";
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)