[Schmitzm-commits] r454 - in branches/1.0-gt2-2.6: src/schmitzm/geotools/feature src/schmitzm/geotools/io src/schmitzm/geotools/styling src/schmitzm/jfree/chart/style src/schmitzm/jfree/feature/style src/skrueger/geotools src_junit src_junit/schmitzm src_junit/schmitzm/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Oct 11 18:12:06 CEST 2009


Author: alfonx
Date: 2009-10-11 18:12:05 +0200 (Sun, 11 Oct 2009)
New Revision: 454

Added:
   branches/1.0-gt2-2.6/src_junit/schmitzm/
   branches/1.0-gt2-2.6/src_junit/schmitzm/geotools/
   branches/1.0-gt2-2.6/src_junit/schmitzm/geotools/feature/
Modified:
   branches/1.0-gt2-2.6/src/schmitzm/geotools/feature/FeatureUtil.java
   branches/1.0-gt2-2.6/src/schmitzm/geotools/io/GeoImportUtil.java
   branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java
   branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
   branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartPlotStyle.java
   branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartStyle.java
   branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java
   branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFS.java
Log:
* Some frickeling in AtlasStyler...

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/feature/FeatureUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/feature/FeatureUtil.java	2009-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/feature/FeatureUtil.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -29,6 +29,8 @@
  ******************************************************************************/
 package schmitzm.geotools.feature;
 
+import hep.aida.bin.DynamicBin1D;
+
 import java.awt.Color;
 import java.io.IOException;
 import java.math.BigDecimal;
@@ -39,6 +41,7 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -48,11 +51,17 @@
 import java.util.TreeMap;
 import java.util.Vector;
 
+import javax.measure.quantity.Length;
+import javax.measure.unit.SI;
+import javax.measure.unit.Unit;
+
 import org.apache.log4j.Logger;
 import org.geotools.coverage.grid.GridCoverage2D;
 import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
 import org.geotools.data.DataUtilities;
+import org.geotools.data.DefaultQuery;
 import org.geotools.data.FeatureSource;
+import org.geotools.data.Query;
 import org.geotools.data.memory.MemoryDataStore;
 import org.geotools.factory.CommonFactoryFinder;
 import org.geotools.factory.GeoTools;
@@ -66,7 +75,9 @@
 import org.geotools.feature.SchemaException;
 import org.geotools.feature.simple.SimpleFeatureBuilder;
 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
+import org.geotools.filter.FilterAbstract;
 import org.geotools.map.MapLayer;
+import org.geotools.resources.CRSUtilities;
 import org.geotools.resources.coverage.FeatureUtilities;
 import org.geotools.styling.Graphic;
 import org.geotools.styling.Mark;
@@ -89,6 +100,7 @@
 import schmitzm.geotools.FilterUtil;
 import schmitzm.lang.LangUtil;
 import schmitzm.lang.ResourceProvider;
+import skrueger.geotools.StyledFeaturesInterface;
 
 import com.vividsolutions.jts.geom.Geometry;
 import com.vividsolutions.jts.geom.GeometryFactory;
@@ -1521,8 +1533,8 @@
 		featureTypeFac.setName(featureTypeName);
 		for (String attrName : attrValues.keySet()) {
 			Object attrValue = attrValues.get(attrName);
-			Class<?> attrClass = attrValue == null ? defaultAttrClass : attrValue
-					.getClass();
+			Class<?> attrClass = attrValue == null ? defaultAttrClass
+					: attrValue.getClass();
 			featureTypeFac.add(AttributeTypeFactory.newAttributeType(attrName,
 					attrClass));
 		}
@@ -1734,10 +1746,11 @@
 	 * @author SK
 	 * @throws org.opengis.feature.IllegalAttributeException
 	 */
-	public static SimpleFeature createSampleFeature(SimpleFeatureType schema, Object... values) {
+	public static SimpleFeature createSampleFeature(SimpleFeatureType schema,
+			Object... values) {
 		SimpleFeature sampleFeature;
 
-//		Object[] attributes = getDefaultAttributeValues(schema);
+		// Object[] attributes = getDefaultAttributeValues(schema);
 
 		try {
 			SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema);
@@ -1864,6 +1877,128 @@
 		}
 	}
 
+	/**
+	 * This method determines the average distance to next centroid.
+	 * 
+	 * @param styledFeatures
+	 */
+	public static Double calcAvgNN(StyledFeaturesInterface<?> styledFeatures) {
+		try {
+			final Integer SAMPLESIZE = 60;
+
+			final DynamicBin1D dists = new DynamicBin1D();
+
+			final Integer countAll = styledFeatures
+					.getFeatureCollectionFiltered().size();
+			if (countAll <= 1)
+				return Double.MAX_VALUE;
+
+			final Random rand = new Random();
+			final double ratio = SAMPLESIZE.doubleValue()
+					/ countAll.doubleValue();
+			// final double ratio = countAll > SAMPLESIZE ?
+			// countAll.doubleValue() / SAMPLESIZE.doubleValue() : 1.;
+			LOGGER.debug("ratio " + ratio);
+
+			Filter filter = FeatureUtil.FILTER_FACTORY2.and(styledFeatures
+					.getFilter(), new FilterAbstract(
+					FeatureUtil.FILTER_FACTORY2) {
+
+				@Override
+				public boolean evaluate(Object object) {
+					return rand.nextDouble() <= ratio;
+				}
+
+			});
+			final Query random100query = new DefaultQuery(styledFeatures
+					.getSchema().getTypeName(), filter, SAMPLESIZE, null,
+					"random query for " + ratio);
+
+			final GeometryForm geometryForm = FeatureUtil.getGeometryForm(styledFeatures.getSchema());
+
+			Iterator<SimpleFeature> it;
+			it = styledFeatures.getFeatureSource().getFeatures(random100query)
+					.iterator();
+
+			while (it.hasNext()) {
+
+				SimpleFeature f1 = it.next();
+
+				switch (geometryForm) {
+				case POINT:
+					final FeatureCollection<SimpleFeatureType, SimpleFeature> allFeatures = styledFeatures
+							.getFeatureCollectionFiltered();
+
+					dists.add(extractedPoint(allFeatures, f1));
+					break;
+				case LINE:
+					dists.add(((Geometry) f1.getDefaultGeometry()).getLength());
+					break;
+				case POLYGON:
+					dists.add(((Geometry) f1.getDefaultGeometry()).getLength());
+					break;
+				}
+			}
+
+			final Double avgNN = dists.mean();
+			CoordinateReferenceSystem crs = styledFeatures.getCrs();
+			Unit<Length> unit = (Unit<Length>) CRSUtilities.getUnit(crs
+					.getCoordinateSystem());
+			LOGGER.info("avgNN for " + styledFeatures.getTitle() + " is "
+					+ avgNN + " in " + unit);
+
+			// Measure<Double,Length> lengthNativeUnit = Measure.valueOf(avgNN,
+			// unit);
+
+			double inMeter = unit.getConverterTo(SI.METER).convert(avgNN);
+
+			double inPixels = inMeter * 100 * 10 / 0.28;
+
+			return inPixels;
+
+		} catch (IOException e) {
+			throw new RuntimeException("Can calculate AvgNN!", e);
+		}
+	}
+
+	private static double extractedPoint(
+			final FeatureCollection<SimpleFeatureType, SimpleFeature> allFeatures,
+			SimpleFeature f1) {
+
+		Double nearestDist = Double.MAX_VALUE;
+
+		Point centroid1 = getCentroid(f1.getDefaultGeometry());
+
+		// iterate over all Features again and determine the
+		// distance:
+		Iterator<SimpleFeature> it2 = allFeatures.iterator();
+		// Iterator<SimpleFeature> it2 =
+		// styledFeatures.getFeatureSource()
+		// .getFeatures(random100query).iterator();
+		while (it2.hasNext()) {
+			SimpleFeature f2 = it2.next();
+
+			if (f2.getID().equals(f1.getID()))
+				continue;
+
+			Point centroid2 = getCentroid(f2.getDefaultGeometry());
+
+			final double distance = centroid1.distance(centroid2);
+
+			nearestDist = Math.min(nearestDist, distance);
+		}
+
+		return nearestDist;
+	}
+
+	/**
+	 * Returns the centroid for any JTS {@link Geometry}
+	 */
+	private static Point getCentroid(Object someGeometry) {
+		Geometry g = (Geometry) someGeometry;
+		return g.getCentroid();
+	}
+
 	// /**
 	// * Extrahiert alle Geometrien aus einer FeatureCollection. Fuer jedes
 	// * Geometry-Attribut der FeatureCollection wird eine GeometrieCollection

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/io/GeoImportUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/io/GeoImportUtil.java	2009-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/io/GeoImportUtil.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -56,6 +56,7 @@
 import org.geotools.coverage.grid.GridCoverageFactory;
 import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
 import org.geotools.data.DataStore;
+import org.geotools.data.DataUtilities;
 import org.geotools.data.shapefile.ShapefileDataStore;
 import org.geotools.data.shapefile.ShpFiles;
 import org.geotools.data.shapefile.indexed.IndexType;
@@ -271,7 +272,7 @@
 	 *         (SK)
 	 */
 	public static FeatureCollection<SimpleFeatureType, SimpleFeature> readFeaturesFromShapeFile(File file) throws Exception {
-		ShapefileDataStore store = new ShapefileDataStore(file.toURI().toURL());
+		ShapefileDataStore store = new ShapefileDataStore(DataUtilities.fileToURL(file));
 		File prjFile = IOUtil.changeFileExt(file, "prj");
 		boolean delPrjFile = false;
 		try {
@@ -952,7 +953,7 @@
 	 * @return {@code null}, wenn kein CRS gelesen werden konnte
 	 */
 	public static CoordinateReferenceSystem determineProjection(File file) throws IOException {
-	  return determineProjection(file.toURI().toURL());
+	  return determineProjection(DataUtilities.fileToURL(file));
 	}
 
 	/**

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-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -178,33 +178,34 @@
 	static final Literal size2 = FeatureUtil.FILTER_FACTORY2.literal(7);
 	static final Literal size3 = FeatureUtil.FILTER_FACTORY2.literal(3);
 	static final Literal zeroLit = FeatureUtil.FILTER_FACTORY2.literal(0);
-//
-//	// static final Stroke SELECTION_STROKE1 =
-//	// STYLE_FACTORY.createStroke(STYLE_BUILDER.colorExpression(Color.YELLOW),
-//	// size2);
-//
-//	static final Stroke SELECTION_STROKE1 = STYLE_FACTORY.createStroke(
-//			STYLE_BUILDER.colorExpression(Color.YELLOW.brighter()),
-//			FeatureUtil.FILTER_FACTORY2.literal(4), FeatureUtil.FILTER_FACTORY2
-//					.literal(1.), FILTER_FACTORY.literal("round"),
-//			FILTER_FACTORY.literal("round"), new float[0],
-//			FeatureUtil.FILTER_FACTORY2.literal(0), null, null);
-//
-//	static final Stroke SELECTION_STROKE2 = STYLE_FACTORY.createStroke(
-//			STYLE_BUILDER.colorExpression(Color.BLACK),
-//			FeatureUtil.FILTER_FACTORY2.literal(1.5),
-//			FeatureUtil.FILTER_FACTORY2.literal(1.), FILTER_FACTORY
-//					.literal("round"), FILTER_FACTORY.literal("round"),
-//			new float[] { 3f, 3f }, FeatureUtil.FILTER_FACTORY2.literal(0),
-//			null, null);
-//
-//	static final Stroke SELECTION_STROKE3 = STYLE_FACTORY.createStroke(
-//			STYLE_BUILDER.colorExpression(Color.WHITE),
-//			FeatureUtil.FILTER_FACTORY2.literal(1.5),
-//			FeatureUtil.FILTER_FACTORY2.literal(1.), FILTER_FACTORY
-//					.literal("round"), FILTER_FACTORY.literal("round"),
-//			new float[] { 3f, 3f }, FeatureUtil.FILTER_FACTORY2.literal(3),
-//			null, null);
+	//
+	// // static final Stroke SELECTION_STROKE1 =
+	// //
+	// STYLE_FACTORY.createStroke(STYLE_BUILDER.colorExpression(Color.YELLOW),
+	// // size2);
+	//
+	// static final Stroke SELECTION_STROKE1 = STYLE_FACTORY.createStroke(
+	// STYLE_BUILDER.colorExpression(Color.YELLOW.brighter()),
+	// FeatureUtil.FILTER_FACTORY2.literal(4), FeatureUtil.FILTER_FACTORY2
+	// .literal(1.), FILTER_FACTORY.literal("round"),
+	// FILTER_FACTORY.literal("round"), new float[0],
+	// FeatureUtil.FILTER_FACTORY2.literal(0), null, null);
+	//
+	// static final Stroke SELECTION_STROKE2 = STYLE_FACTORY.createStroke(
+	// STYLE_BUILDER.colorExpression(Color.BLACK),
+	// FeatureUtil.FILTER_FACTORY2.literal(1.5),
+	// FeatureUtil.FILTER_FACTORY2.literal(1.), FILTER_FACTORY
+	// .literal("round"), FILTER_FACTORY.literal("round"),
+	// new float[] { 3f, 3f }, FeatureUtil.FILTER_FACTORY2.literal(0),
+	// null, null);
+	//
+	// static final Stroke SELECTION_STROKE3 = STYLE_FACTORY.createStroke(
+	// STYLE_BUILDER.colorExpression(Color.WHITE),
+	// FeatureUtil.FILTER_FACTORY2.literal(1.5),
+	// FeatureUtil.FILTER_FACTORY2.literal(1.), FILTER_FACTORY
+	// .literal("round"), FILTER_FACTORY.literal("round"),
+	// new float[] { 3f, 3f }, FeatureUtil.FILTER_FACTORY2.literal(3),
+	// null, null);
 
 	static final Graphic SELECTION_GRAPHIC1 = STYLE_FACTORY.createGraphic(
 			new ExternalGraphic[0], new Mark[] { STYLE_FACTORY.createMark(
@@ -230,7 +231,6 @@
 					zeroLit) }, new org.geotools.styling.Symbol[0],
 			FeatureUtil.FILTER_FACTORY2.literal(1), size3, zeroLit);
 
-
 	/**
 	 * Erstellt einen Default-Style fuer die Klassen/Interfaces:
 	 * StyledFeaturesInterface, GridCoverage2D, FeatureCollection, FeatureSource
@@ -2200,4 +2200,61 @@
 		return rsList;
 	}
 
+	/**
+	 * Super smart metod ;-) Needs to be extended to look at the graphic width
+	 * and Geometry type.
+	 * 
+	 * @param geometryDescriptor
+	 * @param avgNn
+	 */
+	public static double getMaxScaleDenominator(Double avgNn) {
+
+		return getMaxScaleDenominator(avgNn, null);
+	}
+
+	/**
+	 * Super smart metod ;-) Needs to be extended to look at the graphic width
+	 * and Geometry type.
+	 * 
+	 * @param geometryDescriptor
+	 * @param avgNn
+	 */
+	public static double getMaxScaleDenominator(Double avgNn, GeometryForm form) {
+
+		double FAKTOR = .2;
+		// if (geometry.isAssignableFrom(GeometryCollection.class)) {
+		// FAKTOR = 20;
+		// }
+		if (form == GeometryForm.LINE) {
+			FAKTOR = 1;
+		} else if (form == GeometryForm.POLYGON) {
+			FAKTOR = .01;
+		}
+
+		if (Double.isNaN(avgNn) || Double.isInfinite(avgNn))
+			return Double.MAX_VALUE;
+
+		double maxScale = avgNn * FAKTOR;
+
+		LOGGER.debug("maxScale for " + avgNn + " is " + maxScale);
+
+		maxScale = Math.round(maxScale / 1000) * 1000.;
+
+		LOGGER.debug("maxScale for " + avgNn + " is gerundet " + maxScale);
+		//
+		// switch (FeatureUtil.getGeometryForm(geometryDescriptor)) {
+		// case POINT:
+		// maxScale = avgNn * 10000.;
+		// break;
+		// case LINE:
+		// maxScale = avgNn * 10000.;
+		// break;
+		// case POLYGON:
+		// maxScale = avgNn * 10000.;
+		// break;
+		// }
+
+		return maxScale;
+	}
+
 }

Modified: branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartAxisStyle.java	2009-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartAxisStyle.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -41,7 +41,6 @@
 import org.jfree.chart.axis.NumberAxis;
 
 import schmitzm.lang.LangUtil;
-import skrueger.geotools.Copyable;
 import skrueger.i8n.Translation;
 
 /**

Modified: branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartPlotStyle.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartPlotStyle.java	2009-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/schmitzm/jfree/chart/style/ChartPlotStyle.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -30,15 +30,11 @@
 package schmitzm.jfree.chart.style;
 
 import java.awt.Color;
-import java.awt.Insets;
-import java.awt.Stroke;
-import java.awt.geom.Point2D;
 
 import org.apache.log4j.Category;
 import org.jfree.chart.plot.CategoryPlot;
 import org.jfree.chart.plot.Plot;
 import org.jfree.chart.plot.XYPlot;
-import org.jfree.data.Range;
 import org.jfree.ui.RectangleInsets;
 
 import schmitzm.jfree.JFreeChartUtil;

Modified: branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartStyle.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartStyle.java	2009-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartStyle.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -41,10 +41,8 @@
 import org.opengis.feature.simple.SimpleFeatureType;
 
 import schmitzm.jfree.chart.style.AbstractChartStyle;
-import schmitzm.jfree.chart.style.ChartLabelStyle;
 import schmitzm.jfree.chart.style.ChartStyle;
 import schmitzm.lang.LangUtil;
-import skrueger.geotools.Copyable;
 
 /**
  * This interface extends the chart style with several functionalities

Modified: branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java	2009-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -35,7 +35,6 @@
 import org.jfree.data.general.Dataset;
 import org.jfree.data.xy.XYDataset;
 
-import schmitzm.jfree.chart.style.BasicChartStyle;
 import schmitzm.jfree.chart.style.ScatterChartStyle;
 
 /**

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFS.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFS.java	2009-10-09 21:50:48 UTC (rev 453)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFS.java	2009-10-11 16:12:05 UTC (rev 454)
@@ -50,6 +50,7 @@
 import org.opengis.filter.Filter;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
+import schmitzm.geotools.io.GeoImportUtil;
 import schmitzm.geotools.styling.StylingUtil;
 import skrueger.AttributeMetaData;
 import skrueger.i8n.Translation;
@@ -147,7 +148,12 @@
 	}
 
 	public CoordinateReferenceSystem getCrs() {
-		return fs.getSchema().getCoordinateReferenceSystem();
+		CoordinateReferenceSystem crs = fs.getSchema().getCoordinateReferenceSystem();
+		if (fs.getSchema().getCoordinateReferenceSystem() == null) {
+			LOGGER.warn("Could not determine the CRS of "+getTitle()+". Using default "+GeoImportUtil.getDefaultCRS());
+			crs = GeoImportUtil.getDefaultCRS();
+		}
+		return crs;
 	}
 
 	public Translation getDesc() {



More information about the Schmitzm-commits mailing list