[Schmitzm-commits] r171 - trunk/src/schmitzm/geotools/styling

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Jun 30 22:38:52 CEST 2009


Author: alfonx
Date: 2009-06-30 22:38:52 +0200 (Tue, 30 Jun 2009)
New Revision: 171

Modified:
   trunk/src/schmitzm/geotools/styling/StylingUtil.java
Log:
* Moved many many methods from Atlas-Framework ASUtil to Schmitzm StylingUtil. These methods deal with determining and changing the "main" color color of an SLD fragment.

Modified: trunk/src/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/styling/StylingUtil.java	2009-06-30 19:41:53 UTC (rev 170)
+++ trunk/src/schmitzm/geotools/styling/StylingUtil.java	2009-06-30 20:38:52 UTC (rev 171)
@@ -1517,121 +1517,10 @@
 			polS0.setFill(SELECTION_FILL0);
 			symbolizers = LangUtil.extendArray(symbolizers, polS0);
 
-			//			
-			// PolygonSymbolizer polS = STYLE_FACTORY.createPolygonSymbolizer();
-			//			
-			// Fill SELECTION_FILL1 = STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.RED), null,
-			// FeatureUtil.FILTER_FACTORY2.literal(1),
-			// STYLE_FACTORY.createGraphic(new ExternalGraphic[0],
-			// new Mark[] { STYLE_FACTORY.createMark(
-			// FeatureUtil.FILTER_FACTORY2
-			// .literal("hatch"), null,
-			// STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.YELLOW
-			// .brighter())), size1,
-			// zeroLit) },
-			// new org.geotools.styling.Symbol[0],
-			// FeatureUtil.FILTER_FACTORY2.literal(1), size1,
-			// zeroLit));
-			//			
-			//			
-			// polS.setFill(SELECTION_FILL1);
-			// symbolizers = LangUtil.extendArray(symbolizers, polS);
-
 			PolygonSymbolizer polS = STYLE_FACTORY.createPolygonSymbolizer();
 			polS.setFill(SELECTION_FILL1);
 			symbolizers = LangUtil.extendArray(symbolizers, polS);
 
-			//			
-			//			
-			// PolygonSymbolizer polS2 =
-			// STYLE_FACTORY.createPolygonSymbolizer();
-			// Mark hatchMark1 = STYLE_FACTORY.createMark(
-			// FeatureUtil.FILTER_FACTORY2.literal("hatch"), null,
-			// STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.RED)), size1,
-			// zeroLit);
-			//		
-			// Fill SELECTION_FILL2 = STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.GREEN.brighter()), null,
-			// FeatureUtil.FILTER_FACTORY2.literal(1), STYLE_FACTORY
-			// .createGraphic(new ExternalGraphic[0],
-			// new Mark[] { hatchMark1 },
-			// new org.geotools.styling.Symbol[0],
-			// FeatureUtil.FILTER_FACTORY2.literal(1),
-			// size1, FeatureUtil.FILTER_FACTORY2
-			// .literal(90)));
-			// polS2.setFill(SELECTION_FILL2);
-			// symbolizers = LangUtil.extendArray(symbolizers, polS2);
-
-			//			
-			// PolygonSymbolizer polS3 =
-			// STYLE_FACTORY.createPolygonSymbolizer();
-			// Mark hatchMark2 = STYLE_FACTORY.createMark(
-			// FeatureUtil.FILTER_FACTORY2.literal("x"), null,
-			// STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.WHITE)), size0,
-			// zeroLit);
-			//		
-			// Fill SELECTION_FILL3 = STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.GREEN.brighter()), null,
-			// FeatureUtil.FILTER_FACTORY2.literal(1), STYLE_FACTORY
-			// .createGraphic(new ExternalGraphic[0],
-			// new Mark[] { hatchMark2 },
-			// new org.geotools.styling.Symbol[0],
-			// FeatureUtil.FILTER_FACTORY2.literal(1),
-			// size0, FeatureUtil.FILTER_FACTORY2
-			// .literal(15)));
-			// polS3.setFill(SELECTION_FILL3);
-			// symbolizers = LangUtil.extendArray(symbolizers, polS3);
-			//
-			//
-			//			
-			// PolygonSymbolizer polS4 =
-			// STYLE_FACTORY.createPolygonSymbolizer();
-			// Mark hatchMark3 = STYLE_FACTORY.createMark(
-			// FeatureUtil.FILTER_FACTORY2.literal("x"), null,
-			// STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.BLACK)), size1,
-			// zeroLit);
-			//		
-			// Fill SELECTION_FILL4 = STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.GREEN.brighter()), null,
-			// FeatureUtil.FILTER_FACTORY2.literal(1), STYLE_FACTORY
-			// .createGraphic(new ExternalGraphic[0],
-			// new Mark[] { hatchMark3 },
-			// new org.geotools.styling.Symbol[0],
-			// FeatureUtil.FILTER_FACTORY2.literal(1),
-			// size1, FeatureUtil.FILTER_FACTORY2
-			// .literal(60)));
-			// polS4.setFill(SELECTION_FILL4);
-			// symbolizers = LangUtil.extendArray(symbolizers, polS4);
-			//			
-			//
-			//			
-			// PolygonSymbolizer polS5 =
-			// STYLE_FACTORY.createPolygonSymbolizer();
-			// Mark hatchMark4 = STYLE_FACTORY.createMark(
-			// FeatureUtil.FILTER_FACTORY2.literal("x"), null,
-			// STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.YELLOW.brighter())), size2,
-			// zeroLit);
-			//		
-			// Fill SELECTION_FILL5 = STYLE_FACTORY.createFill(STYLE_BUILDER
-			// .colorExpression(Color.GREEN.brighter()), null,
-			// FeatureUtil.FILTER_FACTORY2.literal(1), STYLE_FACTORY
-			// .createGraphic(new ExternalGraphic[0],
-			// new Mark[] { hatchMark4 },
-			// new org.geotools.styling.Symbol[0],
-			// FeatureUtil.FILTER_FACTORY2.literal(1),
-			// size2, FeatureUtil.FILTER_FACTORY2
-			// .literal(60)));
-			// polS5.setFill(SELECTION_FILL5);
-			// symbolizers = LangUtil.extendArray(symbolizers, polS5);
-			//			
-
-			// break;
 		case LINE:
 			LineSymbolizer ls = STYLE_FACTORY.createLineSymbolizer();
 			ls.setStroke(SELECTION_STROKE1);
@@ -1661,7 +1550,73 @@
 
 	}
 	
+	public static Style createStyleSimple(Object geoObject, Color color, Color color2) {
+		if (geoObject instanceof GridCoverage2D
+				|| geoObject instanceof org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) {
+			// Wenn irgendwann mal selection für raster möglich ist, dann hier
+			// einen schöneren style erstellen.
+			return GridUtil.createDefaultStyle();
+		}
 
+		// We have vector-data. Now let's determine the type...
+		GeometryForm geometryForm = null;
+
+		if (geoObject instanceof FeatureCollection) {
+			geometryForm = FeatureUtil
+					.getGeometryForm((FeatureCollection) geoObject);
+		} else if (geoObject instanceof GeometryAttributeType) {
+			geometryForm = FeatureUtil
+					.getGeometryForm((GeometryAttributeType) geoObject);
+		} else if (geoObject instanceof FeatureSource) {
+			geometryForm = FeatureUtil
+					.getGeometryForm((FeatureSource) geoObject);
+		}
+
+		// /**
+		// * Let's see if we have a cached version of the requested style.
+		// */
+		// if (selectionStyles.containsKey(geometryForm))
+		// return selectionStyles.get(geometryForm);
+
+		Style style = STYLE_FACTORY.createStyle();
+		style.addFeatureTypeStyle(STYLE_FACTORY.createFeatureTypeStyle());
+		Symbolizer[] symbolizers = new Symbolizer[0];
+
+		final Expression redExpression = STYLE_BUILDER.colorExpression(color);
+		switch (geometryForm) {
+
+		case POINT:
+			PointSymbolizer ps = STYLE_BUILDER.createPointSymbolizer();
+			ps.getGraphic().getMarks()[0].getFill().setColor( redExpression);
+			ps.getGraphic().getMarks()[0].setStroke(STYLE_BUILDER.createStroke(color2));
+			symbolizers = LangUtil.extendArray(symbolizers, ps);
+
+			break;
+		case POLYGON:
+			PolygonSymbolizer polS = STYLE_BUILDER.createPolygonSymbolizer();
+			polS.getFill().setColor( redExpression);
+			polS.setStroke(STYLE_BUILDER.createStroke(color2));
+			symbolizers = LangUtil.extendArray(symbolizers, polS);
+
+		case LINE:
+			LineSymbolizer ls = STYLE_BUILDER.createLineSymbolizer();
+			ls.getStroke().setColor(redExpression);
+			symbolizers = LangUtil.extendArray(symbolizers, ls);
+			break;
+
+		default:
+			throw new IllegalArgumentException("Provide a suitable object.");
+		}
+
+		style.getFeatureTypeStyles()[0].setRules(new Rule[] { STYLE_FACTORY
+				.createRule() });
+		style.getFeatureTypeStyles()[0].getRules()[0]
+				.setSymbolizers(symbolizers);
+
+		return style;
+	}
+	
+
 	/**
 	 * Creates a {@link Box} that shows a legend for a list of
 	 * {@link FeatureTypeStyle}s and a targeted featureType
@@ -1782,5 +1737,312 @@
 	//
 	// return newPalettes;
 	// }
+	
+	
 
+	/**
+	 * Replaces the "main" color in a given {@link Symbolizer} element
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static void replaceSymbolizerColor(Symbolizer symb, Color oldColor,
+			Color newColor) {
+		if (symb == null)
+			return;
+
+		if (symb instanceof PointSymbolizer) {
+			PointSymbolizer ps = (PointSymbolizer) symb;
+			replacePointSymbolizerColor(ps, oldColor, newColor);
+		}
+
+		if (symb instanceof PolygonSymbolizer) {
+			PolygonSymbolizer ps = (PolygonSymbolizer) symb;
+			replacePolygonSymbolizerColor(ps, oldColor, newColor);
+		}
+
+		if (symb instanceof LineSymbolizer) {
+			LineSymbolizer ps = (LineSymbolizer) symb;
+			replaceLineSymbolizerColor(ps, oldColor, newColor);
+		}
+
+	}
+
+	/**
+	 * Replaces the "main" color in a given {@link LineSymbolizer} element
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static void replaceLineSymbolizerColor(LineSymbolizer ps,
+			Color oldColor, Color newColor) {
+		if (ps == null)
+			return;
+
+		replaceStrokeColor(ps.getStroke(), oldColor, newColor);
+	}
+
+	
+	/**
+	 * Replaces the "main" color in a given {@link PointSymbolizer} element
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static void replacePointSymbolizerColor(PointSymbolizer ps,
+			Color oldColor, Color newColor) {
+		if (ps == null)
+			return;
+
+		replaceGraphicColor(ps.getGraphic(), oldColor, newColor);
+	}
+
+
+	/**
+	 * Replaces the "main" color in a given {@link PolygonSymbolizer} element
+	 * 
+	 * @param oldColor
+	 * @param newColor
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 * @param ps
+	 */
+	public static void replacePolygonSymbolizerColor(PolygonSymbolizer ps,
+			Color oldColor, Color newColor) {
+		replaceFillColor(ps.getFill(), oldColor, newColor);
+		replaceStrokeColor(ps.getStroke(), oldColor, newColor);
+	}
+
+	/**
+	 * Replaces the "main" color in a given {@link Stroke} element
+	 * @param stroke 
+	 * @param oldColor
+	 * @param newColor
+	 */
+	public static void replaceStrokeColor(Stroke stroke, Color oldColor,
+			Color newColor) {
+		if (stroke == null)
+			return;
+
+		if ((stroke.getColor() != null)
+				&& (StylingUtil.getColorFromExpression(stroke.getColor()).equals(oldColor)))
+			stroke.setColor(StylingUtil.STYLE_BUILDER.colorExpression(newColor));
+
+		replaceGraphicColor(stroke.getGraphicFill(), oldColor, newColor);
+
+		replaceGraphicColor(stroke.getGraphicStroke(), oldColor, newColor);
+
+	}
+
+	/**
+	 * Replaces the "main" color in a given {@link Fill} element
+	 * @param fill
+	 * @param oldColor
+	 * @param newColor
+	 */
+	public static void replaceFillColor(Fill fill, Color oldColor,
+			Color newColor) {
+		if (fill == null)
+			return;
+
+		if ((fill.getColor() != null)
+				&& (getColorFromExpression(fill.getColor()).equals(oldColor)))
+			fill.setColor(STYLE_BUILDER.colorExpression(newColor));
+
+		replaceGraphicColor(fill.getGraphicFill(), oldColor, newColor);
+
+		if ((fill.getBackgroundColor() != null)
+				&& (getColorFromExpression(fill.getBackgroundColor())
+						.equals(oldColor)))
+			fill.setBackgroundColor(STYLE_BUILDER.colorExpression(newColor));
+	}
+
+	/**
+	 * Replaces the "main" color in a given {@link Graphic} element
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static void replaceGraphicColor(Graphic graphic, Color oldColor,
+			Color newColor) {
+		if (graphic == null)
+			return;
+
+		if ((graphic.getMarks() != null) && (graphic.getMarks().length > 0)) {
+
+			// Checking for Colors in Marks...
+			for (Mark m : graphic.getMarks()) {
+
+				replaceFillColor(m.getFill(), oldColor, newColor);
+
+				replaceStrokeColor(m.getStroke(), oldColor, newColor);
+
+			}
+		}
+	}
+
+
+	/**
+	 * @return the {@link Color} used in the {@link Graphic} or null, if an
+	 *         {@link ExternalGraphic} is used.
+	 * 
+	 * @param graphic
+	 *            If <code>null</code> returns <code>null</code>
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Color getGraphicColor(Graphic graphic) {
+		if (graphic == null)
+			return null;
+
+		Color foundColor = null;
+
+		if ((graphic.getMarks() != null) && (graphic.getMarks().length > 0)) {
+
+			// Checking for Colors in Marks...
+			for (Mark m : graphic.getMarks()) {
+
+				if (foundColor == null)
+					foundColor = getFillColor(m.getFill());
+
+				if (foundColor == null)
+					foundColor = getStrokeColor(m.getStroke());
+			}
+
+		}
+
+		return foundColor;
+	}
+
+	/**
+	 * @return the first {@link Color} used in a {@link Stroke}.
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Color getStrokeColor(Stroke stroke) {
+		if (stroke == null)
+			return null;
+
+		if (stroke.getColor() != null)
+			return StylingUtil.getColorFromExpression(stroke.getColor());
+
+		if (getGraphicColor(stroke.getGraphicStroke()) != null)
+			return getGraphicColor(stroke.getGraphicStroke());
+
+		if (getGraphicColor(stroke.getGraphicFill()) != null)
+			return getGraphicColor(stroke.getGraphicFill());
+
+		return null;
+	}
+
+	/**
+	 * @return the first {@link Color} used in a {@link Fill}.
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Color getFillColor(Fill fill) {
+		if (fill == null)
+			return null;
+
+		Color foundColor = null;
+
+		if (foundColor == null)
+			foundColor = getColorFromExpression(fill.getColor());
+
+		if (foundColor == null)
+			foundColor = getGraphicColor(fill.getGraphicFill());
+
+		if (foundColor == null)
+			foundColor = getColorFromExpression(fill.getBackgroundColor());
+
+		return foundColor;
+	}
+	
+	/**
+	 * @return the first {@link Color} used in a {@link Symbolizer}.
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Color getSymbolizerColor(Symbolizer symb) {
+		if (symb == null)
+			return null;
+
+		if (symb instanceof PointSymbolizer) {
+			PointSymbolizer ps = (PointSymbolizer) symb;
+			return getPointSymbolizerColor(ps);
+		}
+
+		if (symb instanceof PolygonSymbolizer) {
+			PolygonSymbolizer ps = (PolygonSymbolizer) symb;
+			return getPolygonSymbolizerColor(ps);
+		}
+
+		if (symb instanceof LineSymbolizer) {
+			LineSymbolizer ps = (LineSymbolizer) symb;
+			return getLineSymbolizerColor(ps);
+		}
+
+		return null;
+	}
+
+	
+	/**
+	 * @return the first {@link Color} used in a {@link LineSymbolizer}.
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Color getLineSymbolizerColor(LineSymbolizer ps) {
+		if (ps == null)
+			return null;
+
+		Color foundColor = null;
+
+		if (foundColor == null)
+			foundColor = getStrokeColor(ps.getStroke());
+
+		return foundColor;
+	}
+
+
+	/**
+	 * @return the first {@link Color} used in a {@link PolygonSymbolizer}.
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Color getPolygonSymbolizerColor(PolygonSymbolizer ps) {
+		if (ps == null)
+			return null;
+
+		Color foundColor = null;
+
+		if (foundColor == null)
+			foundColor = getFillColor(ps.getFill());
+
+		if (foundColor == null)
+			foundColor = getStrokeColor(ps.getStroke());
+
+		return foundColor;
+	}
+
+
+	/**
+	 * @return the first {@link Color} used in a {@link PointSymbolizer}.
+	 * 
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
+	 *         Kr&uuml;ger</a>
+	 */
+	public static Color getPointSymbolizerColor(PointSymbolizer ps) {
+		if (ps == null)
+			return null;
+
+		return getGraphicColor(ps.getGraphic());
+	}
+
+
 }



More information about the Schmitzm-commits mailing list