[Schmitzm-commits] r101 - in trunk/src: schmitzm/geotools/styling skrueger/geotools/selection

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri May 8 15:17:03 CEST 2009


Author: alfonx
Date: 2009-05-08 15:17:02 +0200 (Fri, 08 May 2009)
New Revision: 101

Modified:
   trunk/src/schmitzm/geotools/styling/StylingUtil.java
   trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
Log:
(Hacking Session in action - atomic commits can't be ensured)
- Moved Style-cloning methods fro skrueger.sld.Utilities.java to schmitzm-StylingUtil.java

Modified: trunk/src/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/styling/StylingUtil.java	2009-05-08 12:29:45 UTC (rev 100)
+++ trunk/src/schmitzm/geotools/styling/StylingUtil.java	2009-05-08 13:17:02 UTC (rev 101)
@@ -44,6 +44,7 @@
 import org.geotools.feature.Feature;
 import org.geotools.feature.FeatureCollection;
 import org.geotools.feature.GeometryAttributeType;
+import org.geotools.gui.swing.ExceptionMonitor;
 import org.geotools.renderer.lite.RendererUtilities;
 import org.geotools.resources.i18n.Vocabulary;
 import org.geotools.resources.i18n.VocabularyKeys;
@@ -53,6 +54,7 @@
 import org.geotools.styling.ColorMapImpl;
 import org.geotools.styling.FeatureTypeStyle;
 import org.geotools.styling.FeatureTypeStyleImpl;
+import org.geotools.styling.Graphic;
 import org.geotools.styling.LineSymbolizer;
 import org.geotools.styling.PointSymbolizer;
 import org.geotools.styling.PolygonSymbolizer;
@@ -74,7 +76,9 @@
 import org.opengis.coverage.grid.GridCoverage;
 import org.opengis.filter.Filter;
 import org.opengis.filter.expression.Expression;
+import org.opengis.filter.expression.NilExpression;
 
+import com.sun.xml.bind.StringInputStream;
 import com.vividsolutions.jts.geom.Geometry;
 import com.vividsolutions.jts.geom.LineString;
 import com.vividsolutions.jts.geom.MultiLineString;
@@ -83,6 +87,7 @@
 import com.vividsolutions.jts.geom.Point;
 import com.vividsolutions.jts.geom.Polygon;
 
+import schmitzm.geotools.FilterUtil;
 import schmitzm.geotools.feature.FeatureUtil;
 import schmitzm.geotools.grid.GridUtil;
 
@@ -102,6 +107,11 @@
 
 	/** Standard-Instanz eines {@link StyleBuilder}. */
 	public static final StyleBuilder STYLE_BUILDER = new StyleBuilder();
+	
+
+	/** Standard-Instanz eines {@link SLDTransformer}. */
+	public final static SLDTransformer SLDTRANSFORMER = new SLDTransformer();
+
 	/**
 	 * Standard-Instanz eines {@link StyleBuilder}.
 	 * 
@@ -841,17 +851,16 @@
 	public static final void saveStyleToSLD(Style style, File exportFile,
 			Charset charset) throws TransformerException, IOException {
 		Writer w = null;
-		final SLDTransformer aTransformer = new SLDTransformer();
 		if (charset != null) {
-			aTransformer.setEncoding(charset);
+			SLDTRANSFORMER.setEncoding(charset);
 		}
-		aTransformer.setIndentation(2);
-		final String xml = aTransformer.transform(style);
+		SLDTRANSFORMER.setIndentation(2);
+		final String xml = SLDTRANSFORMER.transform(style);
 		w = new FileWriter(exportFile);
 		w.write(xml);
 		w.close();
 		LOGGER.info("Saved a Style to " + exportFile + " with charset "
-				+ aTransformer.getEncoding().name());
+				+ SLDTRANSFORMER.getEncoding().name());
 	}
 
 	/**
@@ -1156,4 +1165,113 @@
 	// }
 	// return null;
 	// }
+	
+
+
+	/**
+	 * Clones a Style by converting it to XML and reading it back in.  
+	 * @param style the {@link Style} to be copied.
+	 */
+	public static Style clone(Style style) {
+
+		String xml;
+		try {
+			xml = SLDTRANSFORMER.transform(style);
+
+			SLDParser stylereader;
+			stylereader = new SLDParser(StylingUtil.STYLE_FACTORY,
+					new StringInputStream(xml));
+			Style[] styles = stylereader.readXML();
+
+			return styles[0];
+		} catch (TransformerException e) {
+			ExceptionMonitor.show(null, e);
+			return null;
+		}
+	}
+
+
+	public static Symbolizer clone(Symbolizer sym) {
+		if (sym == null) {
+			throw new RuntimeException("cant clone null");
+		}
+		
+		/**
+		 * Doing some corrections. Otherwise the Symbolizer can not be converted to XML
+		 */
+		if (sym instanceof PointSymbolizer) {
+			PointSymbolizer ps = (PointSymbolizer)sym;
+			Graphic graphic = ps.getGraphic();
+			
+			if (graphic.getSize() instanceof NilExpression){
+				graphic.setSize(FilterUtil.FILTER_FAC.literal(10.));
+			}
+			try {
+				SLDTRANSFORMER.transform(graphic);
+			} catch (TransformerException e) {
+				LOGGER.error("gr",e);
+			}
+		}
+		
+		Style style = STYLE_BUILDER.createStyle(sym); // TODO Mag ich das?
+		
+
+		Filter allwaysTrueFilter = FilterUtil.FILTER_FAC2.equals(FilterUtil.FILTER_FAC2.literal("1"),
+				FilterUtil.FILTER_FAC2.literal("1"));
+
+		style.getFeatureTypeStyles()[0].getRules()[0].setFilter(allwaysTrueFilter);
+		
+
+		String xml;
+		try {
+			xml = SLDTRANSFORMER.transform(style);
+
+			SLDParser stylereader;
+			stylereader = new SLDParser(StylingUtil.STYLE_FACTORY,
+					new StringInputStream(xml));
+			Style[] styles = stylereader.readXML();
+
+			return (Symbolizer) styles[0].getFeatureTypeStyles()[0].getRules()[0]
+					.getSymbolizers()[0];
+		} catch (TransformerException e) {
+			ExceptionMonitor.show(null, e);
+			return STYLE_FACTORY.createPointSymbolizer(); // TODO Abhängig vom
+			// sym geometry type
+		}
+
+	}
+
+	/**
+	 * Clons a {@link Graphic} element
+	 * 
+	 * @param graphicFill
+	 * @return
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Graphic clone(Graphic graphic) {
+		try {
+			PointSymbolizer ps = STYLE_FACTORY.createPointSymbolizer(graphic, "ACHTUNG");
+
+			Style style = STYLE_BUILDER.createStyle(ps);
+
+			final String xml = SLDTRANSFORMER.transform(style);
+
+			SLDParser stylereader;
+			stylereader = new SLDParser(StylingUtil.STYLE_FACTORY,
+					new StringInputStream(xml));
+			Style[] styles = stylereader.readXML();
+
+			PointSymbolizer clonedPs = (PointSymbolizer) styles[0]
+					.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];
+
+			Graphic clonedG = clonedPs.getGraphic();
+
+			return clonedG;
+
+		} catch (TransformerException e) {
+			LOGGER.error(e);
+		}
+		return null;
+	}
 }

Modified: trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
===================================================================
--- trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java	2009-05-08 12:29:45 UTC (rev 100)
+++ trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java	2009-05-08 13:17:02 UTC (rev 101)
@@ -26,7 +26,13 @@
 
 import org.geotools.feature.Feature;
 import org.geotools.map.MapLayer;
+import org.geotools.styling.Rule;
+import org.geotools.styling.RuleImpl;
 import org.geotools.styling.Style;
+import org.geotools.styling.StyleImpl;
+import org.opengis.filter.Filter;
+import org.opengis.filter.FilterVisitor;
+import org.opengis.layer.StyleURL;
 
 import schmitzm.geotools.gui.FeatureCollectionTableModel;
 import schmitzm.geotools.gui.JMapPane;
@@ -135,11 +141,38 @@
 				
 			} else {
 				LOGGER.debug("SELECTION .. change style");
-				selectionMapStyle = StylingUtil
-						.createDefaultStyle(styledMapLayer.getGeoObject());
 				
-				selectionMapStyle = styledMapLayer.getStyle();
+				Style originalStyle = styledMapLayer.getStyle();
 				
+				// TODO This clone is a deep clone and it is slow.. 
+				selectionMapStyle =  StylingUtil.clone(originalStyle);
+				
+				Rule[] rules = selectionMapStyle.getFeatureTypeStyles()[0].getRules();
+				
+				Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();
+				selectedRule.setFilter(new Filter() {
+
+					@Override
+					public Object accept(FilterVisitor visitor, Object extraData) {
+						return null;
+					}
+
+					@Override
+					public boolean evaluate(Object obj) {
+						
+						return false;
+					}
+					
+				});
+				
+				
+//				selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)
+				
+				// Flat copy the style
+				
+				
+				
+				
 			}
 
 			mapLayer.setStyle(selectionMapStyle);



More information about the Schmitzm-commits mailing list