[Schmitzm-commits] r515 - in branches/1.0-gt2-2.6/src: gtmig/org/geotools/swing schmitzm/geotools/styling

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Nov 9 19:51:55 CET 2009


Author: alfonx
Date: 2009-11-09 19:51:54 +0100 (Mon, 09 Nov 2009)
New Revision: 515

Modified:
   branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java
   branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java
Log:
Styles with wronlgy spelled property names are now automatically corrected when loaded. This helps when upgrading atlases that have been created with GeoPublisher/AtlasStyler 1.2



Modified: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java	2009-11-09 15:05:41 UTC (rev 514)
+++ branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java	2009-11-09 18:51:54 UTC (rev 515)
@@ -1061,9 +1061,7 @@
 
 		oldMapArea = mapArea;
 
-		System.out.println("requested setting to \\t" + newMapArea);
 		this.mapArea = bestAllowedMapArea(newMapArea);
-		System.out.println("set to \\t\\ŧ" + newMapArea);
 
 		if (localContext != null) {
 			localContext.setAreaOfInterest(mapArea, localContext
@@ -1078,7 +1076,7 @@
 		mapAreaChanged = true;
 		repaint();
 
-		LOGGER.debug("New maparea = " + mapArea);
+//		LOGGER.debug("New maparea = " + mapArea);
 		return true;
 	}
 

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java	2009-11-09 15:05:41 UTC (rev 514)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java	2009-11-09 18:51:54 UTC (rev 515)
@@ -32,6 +32,7 @@
 import gtmig.org.geotools.swing.XMapPane;
 
 import java.awt.Color;
+import java.awt.Component;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -106,6 +107,8 @@
 import org.opengis.coverage.grid.GridCoverage;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
+import org.opengis.feature.type.AttributeDescriptor;
+import org.opengis.feature.type.Name;
 import org.opengis.filter.And;
 import org.opengis.filter.Filter;
 import org.opengis.filter.FilterFactory;
@@ -113,6 +116,7 @@
 import org.opengis.filter.PropertyIsEqualTo;
 import org.opengis.filter.expression.Expression;
 import org.opengis.filter.expression.Literal;
+import org.opengis.filter.expression.PropertyName;
 
 import schmitzm.geotools.feature.FeatureUtil;
 import schmitzm.geotools.feature.FeatureUtil.GeometryForm;
@@ -257,9 +261,10 @@
 			style = FeatureUtil
 					.createDefaultStyle(((StyledFeaturesInterface<?>) object)
 							.getSchema().getGeometryDescriptor());
-		
+
 		if (object instanceof GridCoverage2D
-				|| object instanceof AbstractGridCoverage2DReader || object instanceof StyledRasterInterface)
+				|| object instanceof AbstractGridCoverage2DReader
+				|| object instanceof StyledRasterInterface)
 			style = GridUtil.createDefaultStyle();
 
 		if (object instanceof FeatureCollection)
@@ -1044,17 +1049,19 @@
 	 */
 	public static Style[] loadSLD(InputStream inputStream) {
 
-		SLDParser stylereader;
-		stylereader = new SLDParser(StylingUtil.STYLE_FACTORY, inputStream);
-		Style[] styles = stylereader.readXML();
-
-		if (styles == null || styles[0] == null) {
-			LOGGER
-					.warn(" ... no styles recognized. Return 'new Style[] { null }' ");
-			return new Style[] { null };
+		Style[] styles = null;
+		try {
+			SLDParser stylereader = new SLDParser(StylingUtil.STYLE_FACTORY,
+					inputStream);
+			styles = stylereader.readXML();
+			return styles;
+		} catch (Exception e) {
 		}
 
-		return styles;
+		LOGGER
+				.warn(" ... no styles recognized. Return 'new Style[] { null }' ");
+		return new Style[] { null };
+
 	}
 
 	/**
@@ -1069,7 +1076,6 @@
 	 *         Kr&uuml;ger</a>
 	 */
 	public static Style[] loadSLD(File sldFile) throws FileNotFoundException {
-//		LOGGER.debug("Loading styles from File...");
 
 		FileInputStream inputStream = null;
 		try {
@@ -1100,27 +1106,29 @@
 	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
 	 *         Kr&uuml;ger</a>
 	 */
-	public static final boolean saveStyleToSLD(Style origStyle, File exportFile,
-			Charset charset) throws TransformerException, IOException {
+	public static final boolean saveStyleToSLD(Style origStyle,
+			File exportFile, Charset charset) throws TransformerException,
+			IOException {
 		SLDTRANSFORMER.setIndentation(2);
-		
+
 		Style exportStyle = removeSelectionFeatureTypeStyle(origStyle);
 
 		if (!isStyleDifferent(exportStyle, exportFile)) {
-			LOGGER.debug("Style "+exportFile+" didn't change, not saving.");
+			LOGGER.debug("Style " + exportFile + " didn't change, not saving.");
 			return false;
 		}
 
 		SLDTRANSFORMER.transform(exportStyle, new FileWriter(exportFile));
 
-		LOGGER.info("Saved a Style with charset "
-				+ SLDTRANSFORMER.getEncoding().name() + " to " + exportFile);
-		
+		// LOGGER.info("Saved a Style with charset "
+		// + SLDTRANSFORMER.getEncoding().name() + " to " + exportFile);
+
 		return true;
 	}
 
 	/**
-	 * Creates a copy of the given {@link Style}, removing any {@link FeatureTypeStyle}s that are only SELECTION related.
+	 * Creates a copy of the given {@link Style}, removing any
+	 * {@link FeatureTypeStyle}s that are only SELECTION related.
 	 */
 	public static Style removeSelectionFeatureTypeStyle(final Style style) {
 
@@ -1153,18 +1161,19 @@
 	 */
 	public static boolean isStyleDifferent(Style style1, File style2file) {
 		SLDTRANSFORMER.setIndentation(2);
-		
+
 		try {
 
 			if (!style2file.exists())
 				return true;
 
 			Style style2 = loadSLD(style2file)[0];
-			
+
 			return isStyleDifferent(style1, style2);
-			
+
 		} catch (Exception e) {
-			LOGGER.debug("Comparing styles "+style1+" and "+style2file+" failed. So we assume they are different.",e);
+			LOGGER.debug("Comparing styles " + style1 + " and " + style2file
+					+ " failed. So we assume they are different.", e);
 			return true;
 		}
 	}
@@ -1181,14 +1190,15 @@
 	 */
 	public static boolean isStyleDifferent(Style style1, Style style2) {
 		try {
-		// Transforming style2 to an XML String
-		String style1string = SLDTRANSFORMER.transform(style1);
-		String style2string = SLDTRANSFORMER.transform(style2);
-		
-		return !style1string.equals(style2string);
-		
+			// Transforming style2 to an XML String
+			String style1string = SLDTRANSFORMER.transform(style1);
+			String style2string = SLDTRANSFORMER.transform(style2);
+
+			return !style1string.equals(style2string);
+
 		} catch (Exception e) {
-			LOGGER.debug("Compating styles "+style1+" and "+style2+" failed. So we assume they are different.",e);
+			LOGGER.debug("Compating styles " + style1 + " and " + style2
+					+ " failed. So we assume they are different.", e);
 			return true;
 		}
 
@@ -2395,4 +2405,109 @@
 		return filterSLDVisibleOnly(fc, style, scaleDenominator);
 	}
 
+	/**
+	 * Since GT2.6, the AttributeNames are case sensitive. Also the raster
+	 * Styles need GeometryProperty set to "geom" to work. This method checks
+	 * all referenced AttributeNames and checks them against the schema.
+	 * 
+	 * @retun <code>true</code> if something has been change, otherwise
+	 *        <code>false</code>.
+	 */
+	public static Style correctPropertyNames(Style style,
+			final SimpleFeatureType schema) {
+
+		DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor() {
+			public void visit(RasterSymbolizer sym) {
+				if (!sym.getGeometryPropertyName().equals("geom")) {
+					LOGGER
+							.debug("Corrected one RasterSymbolizer with GeometryPropertyName '"
+									+ sym.getGeometryPropertyName()
+									+ "' to 'geom'.");
+					sym.setGeometryPropertyName("geom");
+				}
+				super.visit(sym);
+			};
+
+			public void visit(LineSymbolizer sym) {
+				sym.setGeometryPropertyName(findBestMatchingAttribute(schema,
+						sym.getGeometryPropertyName()).getLocalPart());
+				super.visit(sym);
+			}
+
+			public void visit(PolygonSymbolizer sym) {
+				sym.setGeometryPropertyName(findBestMatchingAttribute(schema,
+						sym.getGeometryPropertyName()).getLocalPart());
+				super.visit(sym);
+			}
+
+			public void visit(PointSymbolizer sym) {
+				sym.setGeometryPropertyName(findBestMatchingAttribute(schema,
+						sym.getGeometryPropertyName()).getLocalPart());
+				super.visit(sym);
+			}
+
+			public void visit(TextSymbolizer sym) {
+				sym.setGeometryPropertyName(findBestMatchingAttribute(schema,
+						sym.getGeometryPropertyName()).getLocalPart());
+
+				if (sym.getLabel() instanceof PropertyName) {
+					PropertyName propertyname = (PropertyName) sym.getLabel();
+					sym.setLabel(ff.property(findBestMatchingAttribute(schema,
+							propertyname.getPropertyName())));
+				}
+
+				if (sym.getPriority() instanceof PropertyName) {
+					PropertyName propertyname = (PropertyName) sym
+							.getPriority();
+					sym.setPriority(ff.property(findBestMatchingAttribute(
+							schema, propertyname.getPropertyName())));
+				}
+
+				super.visit(sym);
+			}
+
+		};
+
+		dsv.visit(style);
+
+		return (Style) dsv.getCopy();
+	}
+
+	/**
+	 * Checks whether the given attribute local name exists in the schema. If no
+	 * exact match is found, an ingnoreCase match is performed. If no match is
+	 * found, the first attribute is returned. If the schema has no attributes,
+	 * an exception is thrown.
+	 * 
+	 * @param schema
+	 *            SimpleFeatureType to check against
+	 * @param name
+	 *            localname of an attribte to look for
+	 */
+	public static Name findBestMatchingAttribute(SimpleFeatureType schema,
+			String name) {
+
+		List<AttributeDescriptor> attributeDescriptors = schema
+				.getAttributeDescriptors();
+		if (attributeDescriptors.size() == 0) {
+			LOGGER.error("The schmema has no attributes!");
+		}
+
+		// Checking for exact match
+		for (AttributeDescriptor d : attributeDescriptors) {
+			if (d.getName().getLocalPart().equals(name))
+				return d.getName();
+		}
+		// Checking for irgnoreCase match
+		for (AttributeDescriptor d : attributeDescriptors) {
+			if (d.getName().getLocalPart().equalsIgnoreCase(name)) {
+				LOGGER.error("Corrected attributeName '" + name + "' to "
+						+ d.getName().getLocalPart());
+				return d.getName();
+			}
+		}
+
+		return attributeDescriptors.get(0).getName();
+	}
+
 }



More information about the Schmitzm-commits mailing list