[Schmitzm-commits] r1092 - in branches/2.2.x: src/schmitzm/geotools/feature src_junit/schmitzm/geotools/feature

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Oct 10 14:56:25 CEST 2010


Author: alfonx
Date: 2010-10-10 14:56:24 +0200 (Sun, 10 Oct 2010)
New Revision: 1092

Modified:
   branches/2.2.x/src/schmitzm/geotools/feature/FeatureUtil.java
   branches/2.2.x/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java
Log:
Improved the matching of attribute names, even if the attribute name contains illegal characters.

Modified: branches/2.2.x/src/schmitzm/geotools/feature/FeatureUtil.java
===================================================================
--- branches/2.2.x/src/schmitzm/geotools/feature/FeatureUtil.java	2010-10-09 23:47:43 UTC (rev 1091)
+++ branches/2.2.x/src/schmitzm/geotools/feature/FeatureUtil.java	2010-10-10 12:56:24 UTC (rev 1092)
@@ -128,8 +128,8 @@
 	 * sind in properties-Dateien unter {@code
 	 * schmitzm.geotools.feature.resource.locales} hinterlegt.
 	 */
-	public static ResourceProvider RESOURCE = ResourceProvider.newInstance(LangUtil
-			.extendPackagePath(FeatureUtil.class,
+	public static ResourceProvider RESOURCE = ResourceProvider.newInstance(
+			LangUtil.extendPackagePath(FeatureUtil.class,
 					"resource.locales.FeatureResourceBundle"), Locale.ENGLISH);
 
 	private static final NameImpl GC_NAME = new NameImpl(
@@ -210,10 +210,10 @@
 	 * Prueft, ob ein Attribut ein Geometrie-Attribut ist.
 	 */
 	public static boolean isGeometryAttribute(Object attrType) {
-	  return attrType instanceof GeometryAttributeType ||
-	         attrType instanceof GeometryDescriptor;
+		return attrType instanceof GeometryAttributeType
+				|| attrType instanceof GeometryDescriptor;
 	}
-	
+
 	/**
 	 * Determines the kind of geometry of a {@link SimpleFeatureType}.
 	 * 
@@ -311,7 +311,7 @@
 
 		for (int j = 0; j < f.getFeatureType().getAttributeCount(); j++)
 			// if ( f.getFeatureType().getAttributeType(j).isGeometry() )
-			if ( isGeometryAttribute(f.getFeatureType()) )
+			if (isGeometryAttribute(f.getFeatureType()))
 				geomVec.add(f.getAttribute(j));
 
 		Geometry[] geomArr = new Geometry[geomVec.size()];
@@ -515,7 +515,7 @@
 	public static SimpleFeature[] featuresToArray(
 			FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
 			boolean includeNullFeatures) {
-		if (fc == null || fc.size() == 0 /*fc.isEmpty()*/ )
+		if (fc == null || fc.size() == 0 /* fc.isEmpty() */)
 			return new SimpleFeature[0];
 
 		// long startTime = System.currentTimeMillis();
@@ -757,8 +757,8 @@
 
 			try {
 				builder.add(aType);
-				if ( isGeometryAttribute(aType) &&
-					 builder.getDefaultGeometry() == null)
+				if (isGeometryAttribute(aType)
+						&& builder.getDefaultGeometry() == null)
 					builder.setDefaultGeometry(aType.getLocalName());
 			} catch (IllegalArgumentException err) {
 				builder.add(AttributeTypeFactory.newAttributeType(aType
@@ -1681,8 +1681,7 @@
 	 * 
 	 * @param geomType
 	 *            LineString.class, Point.class or Polygon.class from com.jts...
-	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
-	 *         Tzeggai</a>
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Tzeggai</a>
 	 */
 
 	public static SimpleFeatureType createFeatureType(final Class<?> geomType) {
@@ -1696,8 +1695,7 @@
 	 * @param geomType
 	 *            LineString.class, Point.class or Polygon.class from com.jts...
 	 * 
-	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons
-	 *         Tzeggai</a>
+	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Tzeggai</a>
 	 */
 	public static SimpleFeatureType createFeatureType(final Class<?> geomType,
 			CoordinateReferenceSystem crs) {
@@ -2037,93 +2035,93 @@
 	 * This method determines the average distance to next centroid.
 	 * 
 	 * @param styledFeatures
-	 * @throws IOException if features can't be accessed
+	 * @throws IOException
+	 *             if features can't be accessed
 	 */
-	public static Double calcAvgNN(StyledFeaturesInterface<?> styledFeatures) throws IOException {
-			final Integer SAMPLESIZE = 60;
+	public static Double calcAvgNN(StyledFeaturesInterface<?> styledFeatures)
+			throws IOException {
+		final Integer SAMPLESIZE = 60;
 
-			final DynamicBin1D dists = new DynamicBin1D();
+		final DynamicBin1D dists = new DynamicBin1D();
 
-			final Integer countAll = styledFeatures
-					.getFeatureCollectionFiltered().size();
-			if (countAll <= 1)
-				return Double.MAX_VALUE;
+		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);
+		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) {
+		Filter filter = FeatureUtil.FILTER_FACTORY2.and(styledFeatures
+				.getFilter(), new FilterAbstract(FeatureUtil.FILTER_FACTORY2) {
 
-				@Override
-				public boolean evaluate(Object object) {
-					return rand.nextDouble() <= ratio;
-				}
+			@Override
+			public boolean evaluate(Object object) {
+				return rand.nextDouble() <= ratio;
+			}
 
-			});
-			final DefaultQuery random100query = new DefaultQuery(styledFeatures
-					.getSchema().getTypeName(), filter, SAMPLESIZE, null,
-					"random query for " + ratio);
-			
-			CoordinateReferenceSystem crs = styledFeatures.getCrs();
-			
-			Unit<Length> unit = (Unit<Length>) CRSUtilities.getUnit(crs
-					.getCoordinateSystem());
-			
-			// Restrict the query to only the geometry column 
-			random100query.setPropertyNames(new String[] {styledFeatures
-					.getSchema().getGeometryDescriptor().getLocalName()});
+		});
+		final DefaultQuery random100query = new DefaultQuery(styledFeatures
+				.getSchema().getTypeName(), filter, SAMPLESIZE, null,
+				"random query for " + ratio);
 
-			// See if the unit is convertable to meters. If not, request the features in a world wgs meters CRS.
-			try {
-				unit.getConverterTo(SI.METER);
-			} catch (ConversionException ce) {
-				random100query.setCoordinateSystemReproject(GTUtil.WORLD_METER);
-			}
-			
+		CoordinateReferenceSystem crs = styledFeatures.getCrs();
 
-			final GeometryForm geometryForm = FeatureUtil
-					.getGeometryForm(styledFeatures.getSchema());
+		Unit<Length> unit = (Unit<Length>) CRSUtilities.getUnit(crs
+				.getCoordinateSystem());
 
-			Iterator<SimpleFeature> it;
-			it = styledFeatures.getFeatureSource().getFeatures(random100query)
-					.iterator();
+		// Restrict the query to only the geometry column
+		random100query.setPropertyNames(new String[] { styledFeatures
+				.getSchema().getGeometryDescriptor().getLocalName() });
 
-			while (it.hasNext()) {
+		// See if the unit is convertable to meters. If not, request the
+		// features in a world wgs meters CRS.
+		try {
+			unit.getConverterTo(SI.METER);
+		} catch (ConversionException ce) {
+			random100query.setCoordinateSystemReproject(GTUtil.WORLD_METER);
+		}
 
-				SimpleFeature f1 = it.next();
+		final GeometryForm geometryForm = FeatureUtil
+				.getGeometryForm(styledFeatures.getSchema());
 
-				switch (geometryForm) {
-				case POINT:
-					final FeatureCollection<SimpleFeatureType, SimpleFeature> allFeatures = styledFeatures
-							.getFeatureSource().getFeatures(random100query);
+		Iterator<SimpleFeature> it;
+		it = styledFeatures.getFeatureSource().getFeatures(random100query)
+				.iterator();
 
-					dists.add(extractedPoint(allFeatures, f1));
-					break;
-				case LINE:
-					dists.add(((Geometry) f1.getDefaultGeometry()).getLength());
-					break;
-				case POLYGON:
-					dists.add(((Geometry) f1.getDefaultGeometry()).getLength());
-					break;
-				}
+		while (it.hasNext()) {
+
+			SimpleFeature f1 = it.next();
+
+			switch (geometryForm) {
+			case POINT:
+				final FeatureCollection<SimpleFeatureType, SimpleFeature> allFeatures = styledFeatures
+						.getFeatureSource().getFeatures(random100query);
+
+				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();
+		final Double avgNN = dists.mean();
 
-			LOGGER.info("avgNN for " + styledFeatures.getTitle() + " is "
-					+ avgNN + " in " + unit);
+		LOGGER.info("avgNN for " + styledFeatures.getTitle() + " is " + avgNN
+				+ " in " + unit);
 
-			double inMeter = unit.getConverterTo(SI.METER).convert(avgNN);
+		double inMeter = unit.getConverterTo(SI.METER).convert(avgNN);
 
-			double inPixels = inMeter * 100 * 10 / 0.28;
+		double inPixels = inMeter * 100 * 10 / 0.28;
 
-			return inPixels;
+		return inPixels;
 
 	}
 
@@ -2295,6 +2293,7 @@
 				return new NameImpl(d.getName().getNamespaceURI(), d.getName()
 						.getLocalPart());
 		}
+
 		// Checking for ignoreCase match
 		for (AttributeDescriptor d : attributeDescriptors) {
 			if (d.getName().getLocalPart().equalsIgnoreCase(localName)) {
@@ -2328,15 +2327,15 @@
 						.getLocalPart());
 			}
 		}
-		
-		// Checking for excact match with a _ infront of the seached name 
+
+		// Checking for excact match with a _ infront of the seached name
 		for (AttributeDescriptor d : attributeDescriptors) {
 			if ((d.getName().getLocalPart()).equals("_" + localName)) {
 				return new NameImpl(d.getName().getNamespaceURI(), d.getName()
 						.getLocalPart());
 			}
 		}
-		
+
 		// Checking for excact match with a _ infront of the schema name
 		for (AttributeDescriptor d : attributeDescriptors) {
 			if (("_" + d.getName().getLocalPart()).equalsIgnoreCase(localName)) {
@@ -2344,8 +2343,8 @@
 						.getLocalPart());
 			}
 		}
-		
-		// Checking for excact match with a _ infront of the seached name 
+
+		// Checking for excact match with a _ infront of the seached name
 		for (AttributeDescriptor d : attributeDescriptors) {
 			if ((d.getName().getLocalPart()).equalsIgnoreCase("_" + localName)) {
 				return new NameImpl(d.getName().getNamespaceURI(), d.getName()
@@ -2353,6 +2352,34 @@
 			}
 		}
 
+		// Checking for exact match with a _ infront of the seached name
+		for (AttributeDescriptor d : attributeDescriptors) {
+			if ((d.getName().getLocalPart()).equalsIgnoreCase("_" + localName)) {
+				return new NameImpl(d.getName().getNamespaceURI(), d.getName()
+						.getLocalPart());
+			}
+		}
+
+		// Checking for exact match, after all non ASCII characters have been
+		// transformed to � characters in both strings.
+		for (AttributeDescriptor d : attributeDescriptors) {
+			if (d.getName().getLocalPart().replaceAll("[^\\p{ASCII}]", "�")
+					.equals(localName.replaceAll("[^\\p{ASCII}]", "�"))) {
+				return new NameImpl(d.getName().getNamespaceURI(), d.getName()
+						.getLocalPart());
+			}
+		}
+		
+		// Checking for IGNORECASE match, after all non ASCII characters have been
+		// transformed to � characters in both strings.
+		for (AttributeDescriptor d : attributeDescriptors) {
+			if (d.getName().getLocalPart().replaceAll("[^\\p{ASCII}]", "�")
+					.equalsIgnoreCase(localName.replaceAll("[^\\p{ASCII}]", "�"))) {
+				return new NameImpl(d.getName().getNamespaceURI(), d.getName()
+						.getLocalPart());
+			}
+		}
+
 		return null;
 	}
 

Modified: branches/2.2.x/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java
===================================================================
--- branches/2.2.x/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java	2010-10-09 23:47:43 UTC (rev 1091)
+++ branches/2.2.x/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java	2010-10-10 12:56:24 UTC (rev 1092)
@@ -1,9 +1,12 @@
 package schmitzm.geotools.feature;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
 import org.junit.Test;
+import org.opengis.feature.simple.SimpleFeatureType;
 import org.opengis.feature.type.GeometryDescriptor;
 
 import schmitzm.geotools.feature.FeatureUtil.GeometryForm;
@@ -11,7 +14,6 @@
 import com.vividsolutions.jts.geom.Point;
 import com.vividsolutions.jts.geom.Polygon;
 
-
 public class FeatureUtilTest {
 
 	@Test
@@ -29,4 +31,19 @@
 		assertFalse(FeatureUtil.getGeometryForm(defaultGeometry) == GeometryForm.POLYGON);
 		assertFalse(FeatureUtil.getGeometryForm(defaultGeometry) == GeometryForm.LINE);
 	}
+
+	@Test
+	public void testFindBestMatchingAttribute() {
+		SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
+		builder.setName("testSchema");
+		builder.add("DENSIT�", String.class);
+		builder.add("densit�", String.class);
+		SimpleFeatureType schema = builder.buildFeatureType();
+
+		assertEquals("densit�", FeatureUtil.findBestMatchingAttribute(schema,
+				"densitä").getLocalPart());
+		
+		assertEquals("DENSIT�", FeatureUtil.findBestMatchingAttribute(schema,
+		"DENSITä").getLocalPart());
+	}
 }



More information about the Schmitzm-commits mailing list