[Schmitzm-commits] r1700 - in trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools: io styling

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Aug 31 16:01:25 CEST 2011


Author: alfonx
Date: 2011-08-31 16:01:24 +0200 (Wed, 31 Aug 2011)
New Revision: 1700

Modified:
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/io/GeoImportUtil.java
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StylingUtil.java
Log:
Stylingutil in schmitzm can now "optimize" Styles, which removes and always-true filters and cleans the XML code. 

Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/io/GeoImportUtil.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/io/GeoImportUtil.java	2011-08-30 19:12:51 UTC (rev 1699)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/io/GeoImportUtil.java	2011-08-31 14:01:24 UTC (rev 1700)
@@ -102,6 +102,8 @@
  */
 public class GeoImportUtil {
 
+	public static final String UNZIPPED_SHAPE_FILE_FOLDER_PREFIX = "unzipped_shape_file_by_geopublisher";
+
 	/**
 	 * This type specifies how this class can proceed the import of ASCII
 	 * rasters.
@@ -1550,7 +1552,7 @@
 
 		File tempDir = IOUtil.getTempDir();
 
-		File tempFolder = new File(tempDir, "unzipped"
+		File tempFolder = new File(tempDir, UNZIPPED_SHAPE_FILE_FOLDER_PREFIX
 				+ String.valueOf(System.nanoTime()));
 		tempFolder.mkdir();
 

Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java	2011-08-30 19:12:51 UTC (rev 1699)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java	2011-08-31 14:01:24 UTC (rev 1700)
@@ -87,12 +87,10 @@
 import org.opengis.feature.type.AttributeDescriptor;
 import org.opengis.feature.type.GeometryDescriptor;
 import org.opengis.feature.type.Name;
-import org.opengis.filter.FilterFactory2;
 import org.opengis.parameter.GeneralParameterValue;
 
 import com.vividsolutions.jts.geom.Geometry;
 
-import de.schmitzm.geotools.FilterUtil;
 import de.schmitzm.geotools.GTUtil;
 import de.schmitzm.geotools.JTSUtil;
 import de.schmitzm.geotools.LegendIconFeatureRenderer;
@@ -163,7 +161,6 @@
 	public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem";
 	/** Name of the XML Element for a translation */
 	public static final String ELEM_NAME_TRANSLATION = "translation";
-	private static FilterFactory2 ff = FilterUtil.FILTER_FAC2;
 
 	/**
 	 * Creates a Geotools {@link MapLayer} from an object. If the object is a
@@ -1128,7 +1125,7 @@
 		style.featureTypeStyles().clear();
 		style.featureTypeStyles().addAll(fts);
 		
-		style = StylingUtil.optimizeStyle(style);
+		style = StylingUtil.optimizeStyle(style, null);
 
 		fts = style.featureTypeStyles();
 		if (fts.size() == 0)

Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StylingUtil.java	2011-08-30 19:12:51 UTC (rev 1699)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StylingUtil.java	2011-08-31 14:01:24 UTC (rev 1700)
@@ -172,7 +172,7 @@
 
 	/** Standard-Instanz eines {@link SLDTransformer}. */
 	public final static SLDTransformer SLDTRANSFORMER = new SLDTransformer();
-	
+
 	static {
 		// @since 1.7: All SLD files are by default created with UTF-8, no
 		// matter what the systems default it.
@@ -192,9 +192,18 @@
 
 	public static final FilterFactory2 ff = FeatureUtil.FILTER_FACTORY2;
 
+	/** A Filter to mark a {@link TextSymbolizer} class as disabled **/
+	public final static PropertyIsEqualTo LABEL_CLASS_DISABLED_FILTER = ff
+			.equals(ff.literal("LABEL_CLASS_DISABLED"), ff.literal("YES"));
+
+	/** A Filter to mark a {@link TextSymbolizer} class as enabled **/
+	public final static PropertyIsEqualTo LABEL_CLASS_ENABLED_FILTER = ff
+			.equals(ff.literal("LABEL_CLASS_ENABLED"),
+					ff.literal("LABEL_CLASS_ENABLED"));
+
 	public static final DuplicatingStyleVisitor DUPLICATINGSTYLEVISITOR = new DuplicatingStyleVisitor(
 			STYLE_FACTORY);
-	
+
 	// ** Do not change the value, it is needed to recognize SLD **//
 	static final String ALL_LABEL_CLASSES_ENABLED = "ALL_LABEL_CLASSES_ENABLED";
 	/**
@@ -220,7 +229,6 @@
 			ff.literal(RL_FILTER_APPLIED_STR),
 			ff.literal(RL_FILTER_APPLIED_STR));
 
-
 	/**
 	 * Lists a few predefined {@link Style}s usefull to mark selected features.
 	 */
@@ -2882,6 +2890,8 @@
 
 	}
 
+	final static Parser parser = new Parser(SLD_CONFIGURATION);
+
 	/**
 	 * Saves the {@link Style} to OGC SLD. Overwrites any existing file. If a
 	 * FeatureTypeStyle for selection is used, it is automatically removed. This
@@ -2903,7 +2913,7 @@
 	 */
 	public static final boolean saveStyleToSld(Style origStyle, File exportFile)
 			throws IOException {
-		return saveStyleToSld(origStyle, exportFile, false);
+		return saveStyleToSld(origStyle, exportFile, false, null);
 	}
 
 	/**
@@ -2923,6 +2933,10 @@
 	 *            by AtlasStyler for GUI state and internal information) is
 	 *            thrown away.
 	 * 
+	 * @param title
+	 *            when using optimized, a new title may be defined the the
+	 *            {@link Style} or <code>null</code>.
+	 * 
 	 * @author <a href="mailto:tzeggai at wikisquare.de">Stefan Alfons Tzeggai</a>
 	 * 
 	 * @return <code>true</code> if the file was really written.
@@ -2931,7 +2945,8 @@
 	 * @throws IOException
 	 */
 	public static final boolean saveStyleToSld(Style origStyle,
-			File exportFile, boolean optimized) throws IOException {
+			File exportFile, boolean optimized, String title)
+			throws IOException {
 		try {
 
 			// Wenn Datei nicht mit .sld endet, die Dateierweiterung
@@ -2942,7 +2957,7 @@
 			Style exportStyle = removeSelectionFeatureTypeStyle(origStyle);
 
 			if (optimized) {
-				exportStyle = optimizeStyle(exportStyle);
+				exportStyle = optimizeStyle(exportStyle, null);
 			}
 
 			// Nur in Datei speichern, wenn auch eine Änderung vorliegt.
@@ -2989,7 +3004,7 @@
 	/**
 	 * United multiple feature type styles that work on the same feature type
 	 */
-	public static Style optimizeStyle(Style style) {
+	public static Style optimizeStyle(Style style, String title) {
 
 		Style oStyle = STYLE_BUILDER.createStyle();
 
@@ -3013,39 +3028,87 @@
 		// In jedem FTS alle Rules durchgehen und Filter vereinfachen
 		for (FeatureTypeStyle fts : oStyle.featureTypeStyles()) {
 			for (Rule r : new ArrayList<Rule>(fts.rules())) {
-				// TODO
-				/*
-				 * Wenn ich ein "AND (A,B)" finde, wo A eines meiner Immertrue
-				 * ist, dann mache daraus B Wenn ich ein "AND (A,B)" finde, wo A
-				 * eines meiner Immerfalse ist, dann schmeisse die ganze Rule
-				 * weg?!?! Wenn ich ein "ODER(A,A)" finde, dann mache daraus A
-				 */
-
 				Filter f = r.getFilter();
-				/*
-				 * <pre> <ogc:And> <ogc:PropertyIsEqualTo>
-				 * <ogc:Literal>ALL_LABEL_CLASSES_DISABLED</ogc:Literal>
-				 * <ogc:Literal>YES</ogc:Literal> </ogc:PropertyIsEqualTo>
-				 * <ogc:PropertyIsEqualTo> <ogc:Literal>1</ogc:Literal>
-				 * <ogc:Literal>1</ogc:Literal> </ogc:PropertyIsEqualTo>
-				 * </ogc:And></pre>
-				 */
-				if (f instanceof AndImpl) {
-					AndImpl and = (AndImpl) f;
-					if (and.getChildren().get(0).equals(RL_DISABLED_FILTER)) {
-						fts.rules().remove(r);
-						continue;
-					}
-				}
 
+				f = simpliyfyFilter(f);
+
+				if (f.equals(Filter.EXCLUDE))
+					fts.rules().remove(r);
+				else
+					r.setFilter(f);
 			}
 		}
 
+		if (title != null)
+			oStyle.setTitle(title);
+
 		return oStyle;
 	}
 
-	final static Parser parser = new Parser(SLD_CONFIGURATION);
+	/**
+	 * @return a Simplified filter, free of any filters used as markers by
+	 *         AtlasStyler.
+	 */
+	public static Filter simpliyfyFilter(Filter f) {
+		if (f instanceof AndImpl) {
+			AndImpl and = (AndImpl) f;
+			Filter p1 = (Filter) and.getChildren().get(0);
+			Filter p2 = (Filter) and.getChildren().get(1);
 
+			p1 = simpliyfyFilter(p1);
+			if (p1.equals(Filter.EXCLUDE))
+				return Filter.EXCLUDE;
+
+			p2 = simpliyfyFilter(p2);
+			if (p2.equals(Filter.EXCLUDE))
+				return Filter.EXCLUDE;
+
+			if (p1.equals(Filter.INCLUDE)) {
+				return p2;
+			}
+			if (p2.equals(Filter.INCLUDE)) {
+				return p1;
+			}
+
+		}
+
+		if (f instanceof OrImpl) {
+			OrImpl or = (OrImpl) f;
+			Filter p1 = (Filter) or.getChildren().get(0);
+			Filter p2 = (Filter) or.getChildren().get(1);
+
+			p1 = simpliyfyFilter(p1);
+			if (p1.equals(Filter.INCLUDE))
+				return Filter.INCLUDE;
+
+			p2 = simpliyfyFilter(p2);
+			if (p2.equals(Filter.INCLUDE))
+				return Filter.INCLUDE;
+
+			if (p1.equals(Filter.EXCLUDE))
+				return p2;
+			if (p2.equals(Filter.EXCLUDE))
+				return p1;
+		}
+
+		if (f.equals(RL_ENABLED_FILTER))
+			return Filter.INCLUDE;
+		if (f.equals(RL_FILTER_APPLIED_FILTER))
+			return Filter.INCLUDE;
+		if (f.equals(RL_DISABLED_FILTER))
+			return Filter.EXCLUDE;
+		if (f.equals(FilterUtil.ALLWAYS_TRUE_FILTER))
+			return Filter.INCLUDE;
+		if (f.equals(FilterUtil.NEVER_TRUE_FILTER))
+			return Filter.EXCLUDE;
+		if (f.equals(LABEL_CLASS_ENABLED_FILTER))
+			return Filter.INCLUDE;
+		if (f.equals(LABEL_CLASS_DISABLED_FILTER))
+			return Filter.EXCLUDE;
+
+		return f;
+	}
+
 	/**
 	 * XMl coming from the {@link InputStream} validates against SLD 1.0
 	 */
@@ -3092,17 +3155,23 @@
 	 *            by AtlasStyler for GUI state and internal information) is
 	 *            thrown away.
 	 * 
+	 * @param title
+	 *            when using optimized, a new title may be defined the the
+	 *            {@link Style} or <code>null</code>.
+	 * 
+	 * 
+	 * 
 	 * @author <a href="mailto:tzeggai at wikisquare.de">Stefan Alfons Tzeggai</a>
 	 * @throws TransformerException
 	 */
 	public static final void saveStyleToSLD(Style exportStyle,
-			OutputStream exportStream, boolean optimized)
+			OutputStream exportStream, boolean optimized, String title)
 			throws TransformerException {
 
 		exportStyle = removeSelectionFeatureTypeStyle(exportStyle);
 
 		if (optimized) {
-			exportStyle = optimizeStyle(exportStyle);
+			exportStyle = optimizeStyle(exportStyle, title);
 		}
 
 		SLDTRANSFORMER.transform(exportStyle, exportStream);
@@ -3233,7 +3302,7 @@
 		// Bytestream not file!
 		File tempFile = File.createTempFile("testing", ".sld");
 		try {
-			saveStyleToSld(style, tempFile, false);
+			saveStyleToSld(style, tempFile, false, null);
 			return validates(new FileInputStream(tempFile));
 		} finally {
 			tempFile.delete();



More information about the Schmitzm-commits mailing list