[Schmitzm-commits] r1248 - in trunk: src/schmitzm/geotools/grid src/schmitzm/geotools/styling src_junit/schmitzm/geotools src_junit/schmitzm/geotools/grid src_junit/schmitzm/geotools/styling src_junit/schmitzm/swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Nov 7 00:49:58 CET 2010


Author: alfonx
Date: 2010-11-07 00:49:57 +0100 (Sun, 07 Nov 2010)
New Revision: 1248

Added:
   trunk/src_junit/schmitzm/geotools/grid/
   trunk/src_junit/schmitzm/geotools/grid/GridUtilTest.java
Modified:
   trunk/src/schmitzm/geotools/grid/GridUtil.java
   trunk/src/schmitzm/geotools/styling/StylingUtil.java
   trunk/src_junit/schmitzm/geotools/styling/StylingUtilTest.java
   trunk/src_junit/schmitzm/swing/TestingUtil.java
Log:
* SLDs are now explicitly stored in UTF-8 encoding. Before it was the systems default encoding.
* Added tests to check GridUtil generated Styles for SLD XSD validity.

Modified: trunk/src/schmitzm/geotools/grid/GridUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/grid/GridUtil.java	2010-11-06 21:34:22 UTC (rev 1247)
+++ trunk/src/schmitzm/geotools/grid/GridUtil.java	2010-11-06 23:49:57 UTC (rev 1248)
@@ -57,6 +57,7 @@
 import org.geotools.styling.RasterSymbolizer;
 import org.geotools.styling.Style;
 import org.geotools.styling.StyleBuilder;
+import org.geotools.util.SimpleInternationalString;
 import org.opengis.coverage.PointOutsideCoverageException;
 import org.opengis.coverage.SampleDimension;
 import org.opengis.coverage.grid.GridEnvelope;
@@ -373,12 +374,14 @@
 	public static Style createDefaultStyle() {
 		Style defaultStyle = createStyle(null, 1.0);
 		defaultStyle.setName(DEFAULT_RASTER_STYLE_NAME);
-		defaultStyle.setTitle(DEFAULT_RASTER_STYLE_TITLE);
+		
+//		defaultStyle.setTitle(DEFAULT_RASTER_STYLE_TITLE);
+		defaultStyle.getDescription().setTitle( new SimpleInternationalString( DEFAULT_RASTER_STYLE_TITLE) );
 
 		defaultStyle.featureTypeStyles().get(0).semanticTypeIdentifiers()
 				.add(SemanticType.RASTER);
 
-		return (defaultStyle);
+		return defaultStyle;
 	}
 
 	/**

Modified: trunk/src/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/styling/StylingUtil.java	2010-11-06 21:34:22 UTC (rev 1247)
+++ trunk/src/schmitzm/geotools/styling/StylingUtil.java	2010-11-06 23:49:57 UTC (rev 1248)
@@ -169,7 +169,12 @@
 	public final static SLDTransformer SLDTRANSFORMER = new SLDTransformer();
 
 	static {
-		SLDTRANSFORMER.setEncoding(Charset.defaultCharset());
+		// @since 1.7: All SLD files are by default created with UTF-8, no
+		// matter what the systems default it.
+		SLDTRANSFORMER.setEncoding(Charset.forName("UTF-8"));
+		// SLDTRANSFORMER.setEncoding(Charset.defaultCharset());
+
+		SLDTRANSFORMER.setIndentation(2);
 	}
 
 	/** Standard-Instanz einer {@link StyleFactory} */
@@ -1681,10 +1686,8 @@
 	 *            Farbpaletten-Eintrag
 	 */
 	public static Color getColorFromColorMapEntry(ColorMapEntry entry) {
-		// if ( entry == null || entry.getColor() == null ||
-		// entry.getColor().getValue(null) == null ) // gt2-2.3.4
 		if (entry == null || entry.getColor() == null
-				|| entry.getColor().evaluate(null) == null) // gt2-2-4-2
+				|| entry.getColor().evaluate(null) == null)
 			return null;
 
 		// // Transparenz
@@ -2469,8 +2472,6 @@
 	 *         fomatting.
 	 */
 	public static boolean isStyleDifferent(Style style1, File style2file) {
-		SLDTRANSFORMER.setIndentation(2);
-
 		try {
 
 			if (!style2file.exists())
@@ -2499,7 +2500,6 @@
 	 */
 	public static boolean isStyleDifferent(Style style1, Style style2) {
 		try {
-			// SLDTRANSFORMER.setEncoding(Charset.forName("UTF-8"));
 			// Transforming style2 to an XML String
 			String style1string = SLDTRANSFORMER.transform(style1);
 			String style2string = SLDTRANSFORMER.transform(style2);
@@ -2916,7 +2916,9 @@
 	 * FeatureTypeStyle for selection is used, it is automatically removed. This
 	 * method also checks, whether the style is actually differing from any
 	 * existing style in the {@link File}. If there is no difference, the file
-	 * is not saved again (that is more svn friendly).
+	 * is not saved again (that is more svn friendly).<br/>
+	 * The charset is defined by the {@link #SLDTRANSFORMER} charset, which is
+	 * set it <code>UTF-8</code> by default.
 	 * 
 	 * @param origStyle
 	 *            {@link Style} to save. Any selectino related FeatureTypeStyle
@@ -2925,10 +2927,9 @@
 	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Tzeggai</a>
 	 * 
 	 * @return <code>true</code> if the file was really written.
-	 *         <code>false</code> means, that the existing file already was
-	 *         equals.
+	 *         <code>false</code> means, that the existing file already
+	 *         contained the same content and was not touched.
 	 * @throws IOException
-	 * @throws TransformerException
 	 */
 	public static final boolean saveStyleToSld(Style origStyle, File exportFile)
 			throws IOException {
@@ -2938,8 +2939,6 @@
 			// anhängen
 			exportFile = IOUtil.appendFileExt(exportFile, ".sld");
 
-			SLDTRANSFORMER.setIndentation(2);
-
 			Style exportStyle = removeSelectionFeatureTypeStyle(origStyle);
 
 			// Nur in Datei speichern, wenn
@@ -2952,7 +2951,7 @@
 			List<Exception> validateSld = validateSld(new FileInputStream(
 					exportFile));
 			if (validateSld.size() > 0) {
-				LOGGER.error(exportFile + " is not SLD valid: "
+				LOGGER.error(exportFile + " is not valid SLD: "
 						+ validateSld.size() + " exceptions");
 			}
 			for (Exception e : validateSld) {
@@ -3004,7 +3003,6 @@
 
 		style = removeSelectionFeatureTypeStyle(style);
 
-		SLDTRANSFORMER.setIndentation(2);
 		SLDTRANSFORMER.transform(style, exportStream);
 	}
 
@@ -3118,7 +3116,6 @@
 	 * @throws TransformerException
 	 */
 	public static String toXMLString(Object sld) throws TransformerException {
-		SLDTRANSFORMER.setIndentation(1);
 		return SLDTRANSFORMER.transform(sld);
 	}
 

Added: trunk/src_junit/schmitzm/geotools/grid/GridUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/geotools/grid/GridUtilTest.java	2010-11-06 21:34:22 UTC (rev 1247)
+++ trunk/src_junit/schmitzm/geotools/grid/GridUtilTest.java	2010-11-06 23:49:57 UTC (rev 1248)
@@ -0,0 +1,40 @@
+package schmitzm.geotools.grid;
+
+import static org.junit.Assert.assertTrue;
+
+import java.awt.Color;
+import java.io.IOException;
+
+import org.geotools.coverage.grid.GridCoverage2D;
+import org.geotools.styling.Style;
+import org.junit.Test;
+
+import schmitzm.geotools.styling.StylingUtil;
+import schmitzm.swing.TestingUtil;
+
+public class GridUtilTest {
+
+	@Test
+	public void testCreateDefaultStyleValidates() throws IOException {
+		Style style = GridUtil.createDefaultStyle();
+		assertTrue(StylingUtil.validates(style));
+	}
+
+	@Test
+	public void testCreateInterpolativeStyleValidates() throws Exception {
+		GridCoverage2D grid = (GridCoverage2D) TestingUtil.TestDatasetsRaster.geotiffWithSld.getCoverage();
+		Style style = GridUtil.createInterpolativeStyle(grid, 1., new Color[] {
+				Color.red, Color.green });
+		assertTrue(StylingUtil.validates(style));
+	}
+	
+	@Test
+	public void testCreateDiscreteStyleValidates() throws Exception {
+		GridCoverage2D grid = (GridCoverage2D) TestingUtil.TestDatasetsRaster.geotiffWithSld.getCoverage();
+		Style style = GridUtil.createDiscreteStyle(grid, 1., 2, new Color[] {
+				Color.red, Color.green });
+		assertTrue(StylingUtil.validates(style));
+	}
+
+
+}


Property changes on: trunk/src_junit/schmitzm/geotools/grid/GridUtilTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native

Modified: trunk/src_junit/schmitzm/geotools/styling/StylingUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/geotools/styling/StylingUtilTest.java	2010-11-06 21:34:22 UTC (rev 1247)
+++ trunk/src_junit/schmitzm/geotools/styling/StylingUtilTest.java	2010-11-06 23:49:57 UTC (rev 1248)
@@ -7,6 +7,8 @@
 import java.awt.Rectangle;
 import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
 
 import javax.swing.ImageIcon;
 import javax.swing.JLabel;
@@ -29,7 +31,9 @@
 import org.opengis.feature.simple.SimpleFeatureType;
 
 import schmitzm.geotools.JTSUtil;
+import schmitzm.geotools.feature.FeatureUtil.GeometryForm;
 import schmitzm.geotools.styling.StylingUtil.SelectionStylesTypes;
+import schmitzm.io.IOUtil;
 import schmitzm.swing.TestingUtil;
 
 public class StylingUtilTest extends TestCase {
@@ -103,6 +107,7 @@
 
 	}
 
+	@Test
 	public void testCorrectRasterStyle() {
 		ColorMap wrongOrderColormap = StylingUtil.STYLE_BUILDER.createColorMap(
 				new String[] { "3", "4", "1" }, new double[] { 3, 4, 1 },
@@ -122,7 +127,22 @@
 		assertEquals("3.0", colorMapEntries[1].getQuantity().toString());
 		assertEquals("4.0", colorMapEntries[2].getQuantity().toString());
 	}
+	
 
+
+	@Test
+	public void testSldEncoding() throws IOException {
+		
+		Style style = StylingUtil.createDefaultStyle(GeometryForm.POLYGON);
+		
+		File tf = File.createTempFile("junit", ".sld");
+		StylingUtil.saveStyleToSld(style, tf);
+		assertTrue(tf.exists());
+		
+		String ss = IOUtil.readFileAsString(tf);
+		assertTrue("SLD is not written in UTF-8 encoding", ss.contains("encoding=\"UTF-8"));
+	}
+
 //	@Test
 //	@Ignore
 //	public void testParseColormapToSld() throws IOException, TransformerException {

Modified: trunk/src_junit/schmitzm/swing/TestingUtil.java
===================================================================
--- trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-11-06 21:34:22 UTC (rev 1247)
+++ trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-11-06 23:49:57 UTC (rev 1248)
@@ -32,6 +32,7 @@
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PatternLayout;
+import org.geotools.coverage.grid.GridCoverage2D;
 import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
 import org.geotools.data.DataStore;
 import org.geotools.data.DataUtilities;
@@ -164,6 +165,16 @@
 				return new ArcGridReader(getUrl());
 			return null;
 		}
+		
+		public GridCoverage2D getCoverage() throws Exception {
+			if (resLoc.endsWith("tif"))
+				return GeoImportUtil.readGridFromGeoTiff(DataUtilities.urlToFile(getUrl()));
+			else if (resLoc.endsWith("asc"))
+				return GeoImportUtil.readGridFromArcInfoASCII(DataUtilities.urlToFile(getUrl()));
+			return null;
+			
+			
+		}
 
 		public StyledGridCoverageReader getStyled() throws IOException {
 			return new StyledGridCoverageReader(getReader());



More information about the Schmitzm-commits mailing list