[Schmitzm-commits] r1124 - in trunk: src/schmitzm/geotools/feature src/schmitzm/geotools/io src/schmitzm/jfree src/schmitzm/jfree/chart/style src/skrueger/i8n src_junit/schmitzm/geotools/feature
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Oct 14 22:58:07 CEST 2010
Author: alfonx
Date: 2010-10-14 22:58:06 +0200 (Thu, 14 Oct 2010)
New Revision: 1124
Modified:
trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java
trunk/src/schmitzm/geotools/feature/FeatureUtil.java
trunk/src/schmitzm/geotools/io/GeoImportUtil.java
trunk/src/schmitzm/jfree/JFreeChartUtil.java
trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
trunk/src/skrueger/i8n/I8NUtil.java
trunk/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java
Log:
Modified: trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java
===================================================================
--- trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java 2010-10-14 20:39:04 UTC (rev 1123)
+++ trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java 2010-10-14 20:58:06 UTC (rev 1124)
@@ -4,11 +4,8 @@
import org.geotools.data.FeatureSource;
import org.geotools.feature.AttributeTypeFactory;
-import org.geotools.feature.GeometryAttributeType;
-import org.geotools.feature.NameImpl;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryDescriptor;
-import org.opengis.feature.type.Name;
import org.opengis.filter.Filter;
import schmitzm.geotools.FilterUtil;
@@ -166,4 +163,19 @@
newMetaData
);
}
+
+ /**
+ * Testet ob ein Array mit {@link AttributeModificationRule}s bereits einen
+ * Eintrag für einen zu prüfenden Namen enthaelt.
+ */
+ public static boolean containsNewName(AttributeModificationRule[] amrs,
+ String cleanAttname) {
+
+ for (AttributeModificationRule amr : amrs) {
+ if (cleanAttname.equals(amr.getNewAttrName()))
+ return true;
+ }
+
+ return false;
+ }
}
Modified: trunk/src/schmitzm/geotools/feature/FeatureUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/feature/FeatureUtil.java 2010-10-14 20:39:04 UTC (rev 1123)
+++ trunk/src/schmitzm/geotools/feature/FeatureUtil.java 2010-10-14 20:58:06 UTC (rev 1124)
@@ -51,6 +51,7 @@
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Vector;
+import java.util.regex.Pattern;
import javax.measure.converter.ConversionException;
import javax.measure.quantity.Length;
@@ -104,6 +105,7 @@
import schmitzm.geotools.FilterUtil;
import schmitzm.geotools.GTUtil;
+import schmitzm.io.IOUtil;
import schmitzm.lang.LangUtil;
import schmitzm.lang.ResourceProvider;
import schmitzm.temp.BaseTypeUtil;
@@ -140,13 +142,13 @@
private static final NameImpl GC_NAME = new NameImpl(
"http://www.opengis.net/gml", "GridCoverage");
- /**
- * Prueft, ob ein Attribut ein Geometrie-Attribut ist.
- */
- public static boolean isGeometryAttribute(Object attrType) {
- return attrType instanceof GeometryAttributeType ||
- attrType instanceof GeometryDescriptor;
- }
+ /**
+ * Prueft, ob ein Attribut ein Geometrie-Attribut ist.
+ */
+ public static boolean isGeometryAttribute(Object attrType) {
+ return attrType instanceof GeometryAttributeType
+ || attrType instanceof GeometryDescriptor;
+ }
/**
* This is a cheap and not good hack to extract the names of attributes used
@@ -311,7 +313,7 @@
else if (Polygon.class.isAssignableFrom(geometryType)
|| MultiPolygon.class.isAssignableFrom(geometryType))
return GeometryForm.POLYGON;
-
+
// Aus anderem, doppelem alten code
// if (geometryAttrib != null
// && (com.vividsolutions.jts.geom.Polygon.class
@@ -377,7 +379,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()];
@@ -410,7 +412,7 @@
}
return result;
}
-
+
/**
* Erzeugt einen Standard-Style fuer eine {@link FeatureCollection} Und
* setzt eine default namen
@@ -439,8 +441,8 @@
}
/**
- * Erzeugt einen Standard-Style fuer einen {@link GeometryForm} Und
- * setzt eine default Namen.
+ * Erzeugt einen Standard-Style fuer einen {@link GeometryForm} Und setzt
+ * eine default Namen.
*
* @param geometryAttrib
* GeometryAttributeType
@@ -700,7 +702,8 @@
// if it is a GeometryAttributeType, the CRS must be stored
// in the meta data
if (aType instanceof GeometryDescriptor)
- metaData = ((GeometryDescriptor)aType).getCoordinateReferenceSystem();
+ metaData = ((GeometryDescriptor) aType)
+ .getCoordinateReferenceSystem();
// combine the restrictions of the attribute type for the
// AttributeTypeFactory
@@ -846,8 +849,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(
@@ -2450,106 +2453,160 @@
return null;
}
-
/**
* "Modifies" a {@link FeatureSource} according to the given
- * {@link AttributeModificationRule AttributeModificationDefinitions}
- * and stores the resulting {@link FeatureSource} in a data store.<br>
- * <b>Note</b>:
- * Only the attibutes for which a {@link AttributeModificationRule} is
- * specified, are copied to the destination {@link FeatureSource}. So even
- * if an attribute should be transfered unchanged, there must be an
- * {@link AttributeModificationRule} for it!!
- * This <b>also</b> applies to the geometry attribute!!
- * @param sourceFs source {@link FeatureSource}
- * @param destDataStore destination for the new {@link FeatureSource}
- * @param destAttrRule defines which attributes of the source {@link FeatureSource}
- * are copied in the destination, and (optionally) how the
- * attributes are modified during this procedure
+ * {@link AttributeModificationRule AttributeModificationDefinitions} and
+ * stores the resulting {@link FeatureSource} in a data store.<br>
+ * <b>Note</b>: Only the attibutes for which a
+ * {@link AttributeModificationRule} is specified, are copied to the
+ * destination {@link FeatureSource}. So even if an attribute should be
+ * transfered unchanged, there must be an {@link AttributeModificationRule}
+ * for it!! This <b>also</b> applies to the geometry attribute!!
+ *
+ * @param sourceFs
+ * source {@link FeatureSource}
+ * @param destDataStore
+ * destination for the new {@link FeatureSource}
+ * @param destAttrRule
+ * defines which attributes of the source {@link FeatureSource}
+ * are copied in the destination, and (optionally) how the
+ * attributes are modified during this procedure
*/
- public static void modifyFeatureSource(FeatureSource<SimpleFeatureType,SimpleFeature> sourceFs, AbstractDataStore destDataStore, AttributeModificationRule... destAttrRule) throws IOException {
- SimpleFeatureType sType = sourceFs.getSchema();
- SimpleFeatureTypeBuilder typebuilder = new SimpleFeatureTypeBuilder();
- typebuilder.setName(sType.getTypeName());
-
- //***** Create the new attribute type for the destination FeatureSource *****
- // Loop the attributes, which should be transfered to the
- // destination FeatureSource
- for (AttributeModificationRule rule : destAttrRule) {
- AttributeDescriptor sDescr = sType.getDescriptor(rule.getAttrIdx());
- AttributeDescriptor dDescr = rule.applyToAttributeType(sDescr);
- typebuilder.add(dDescr);
- }
- // create the FeatureType for the destination FeatureSource
- SimpleFeatureType dType = typebuilder.buildFeatureType();
+ public static void modifyFeatureSource(
+ FeatureSource<SimpleFeatureType, SimpleFeature> sourceFs,
+ AbstractDataStore destDataStore,
+ AttributeModificationRule... destAttrRule) throws IOException {
+ SimpleFeatureType sType = sourceFs.getSchema();
+ SimpleFeatureTypeBuilder typebuilder = new SimpleFeatureTypeBuilder();
+ typebuilder.setName(sType.getTypeName());
- //***** Create the new FeatureSource *****
- destDataStore.createSchema(dType);
- FeatureWriter<SimpleFeatureType, SimpleFeature> fWriter = destDataStore.getFeatureWriter(dType.getTypeName(), Transaction.AUTO_COMMIT);
- FeatureCollection<SimpleFeatureType, SimpleFeature> sourceFc = sourceFs.getFeatures();
- FeatureIterator<SimpleFeature> fIter = sourceFc.features();
- try {
- for (SimpleFeature sFeature = null; fIter.hasNext(); ) {
- // Determine source feature
- sFeature = fIter.next();
- // Create destination feature
- SimpleFeature dFeature = fWriter.next();
- // Set the values in the destination feature
- for (int destAttrIdx=0; destAttrIdx < destAttrRule.length; destAttrIdx++) {
- AttributeModificationRule rule = destAttrRule[destAttrIdx];
- Object sValue = sFeature.getAttribute(rule.getAttrIdx());
- Object dValue = null;
- if ( !rule.isNullValue(sValue))
- dValue = transformAttributeValue(sValue, rule.getNewAttrClass());
- dFeature.setAttribute(destAttrIdx, dValue);
- }
- // write the destination feature
- fWriter.write();
- }
- } finally {
- sourceFc.close(fIter);
- fWriter.close();
- }
-
- LOGGER.error("FeatureUtil.modifyFeatureSource(..) not yet implemented!");
-
+ // ***** Create the new attribute type for the destination FeatureSource
+ // *****
+ // Loop the attributes, which should be transfered to the
+ // destination FeatureSource
+ for (AttributeModificationRule rule : destAttrRule) {
+ AttributeDescriptor sDescr = sType.getDescriptor(rule.getAttrIdx());
+ AttributeDescriptor dDescr = rule.applyToAttributeType(sDescr);
+ typebuilder.add(dDescr);
+ }
+ // create the FeatureType for the destination FeatureSource
+ SimpleFeatureType dType = typebuilder.buildFeatureType();
+
+ // ***** Create the new FeatureSource *****
+ destDataStore.createSchema(dType);
+ FeatureWriter<SimpleFeatureType, SimpleFeature> fWriter = destDataStore
+ .getFeatureWriter(dType.getTypeName(), Transaction.AUTO_COMMIT);
+ FeatureCollection<SimpleFeatureType, SimpleFeature> sourceFc = sourceFs
+ .getFeatures();
+ FeatureIterator<SimpleFeature> fIter = sourceFc.features();
+ try {
+ for (SimpleFeature sFeature = null; fIter.hasNext();) {
+ // Determine source feature
+ sFeature = fIter.next();
+ // Create destination feature
+ SimpleFeature dFeature = fWriter.next();
+ // Set the values in the destination feature
+ for (int destAttrIdx = 0; destAttrIdx < destAttrRule.length; destAttrIdx++) {
+ AttributeModificationRule rule = destAttrRule[destAttrIdx];
+ Object sValue = sFeature.getAttribute(rule.getAttrIdx());
+ Object dValue = null;
+ if (!rule.isNullValue(sValue))
+ dValue = transformAttributeValue(sValue,
+ rule.getNewAttrClass());
+ dFeature.setAttribute(destAttrIdx, dValue);
+ }
+ // write the destination feature
+ fWriter.write();
+ }
+ } finally {
+ sourceFc.close(fIter);
+ fWriter.close();
+ }
+
+ LOGGER.error("FeatureUtil.modifyFeatureSource(..) not yet implemented!");
+
}
-
+
/**
* Transforms a value to another type.
- * @param value a value
- * @param destType destination type
- * @return {@code null} if {@code value} is {@code null} or if the transformation can
- * not be applied; the {@code value} itself if no destination type is given or
- * {@code value} is already in instance of the destination type
+ *
+ * @param value
+ * a value
+ * @param destType
+ * destination type
+ * @return {@code null} if {@code value} is {@code null} or if the
+ * transformation can not be applied; the {@code value} itself if no
+ * destination type is given or {@code value} is already in instance
+ * of the destination type
*/
public static Object transformAttributeValue(Object value, Class<?> destType) {
- if ( value == null )
- return null;
- if ( destType == null )
- return value;
- if ( destType.isInstance(value) )
- return value;
-
- // convert to string --> easy!
- if ( String.class.equals(destType) )
- return value.toString();
- // convert to number
- if ( Number.class.isAssignableFrom(destType) ) {
- if ( value instanceof Number )
- return BaseTypeUtil.convertNumber((Number)value,(Class<Number>)destType);
- if ( value instanceof String )
- return BaseTypeUtil.convertFromString((String)value,(Class<Number>)destType);
- }
-
- // TODO: implement more transformation rules
-
- LOGGER.warn("Can not apply attribute value transformation: "+LangUtil.getSimpleClassName(value)+" --> "+LangUtil.getSimpleClassName(destType)+" (for '"+value+"')");
-
- return null;
+ if (value == null)
+ return null;
+ if (destType == null)
+ return value;
+ if (destType.isInstance(value))
+ return value;
+
+ // convert to string --> easy!
+ if (String.class.equals(destType))
+ return value.toString();
+ // convert to number
+ if (Number.class.isAssignableFrom(destType)) {
+ if (value instanceof Number)
+ return BaseTypeUtil.convertNumber((Number) value,
+ (Class<Number>) destType);
+ if (value instanceof String)
+ return BaseTypeUtil.convertFromString((String) value,
+ (Class<Number>) destType);
+ }
+
+ // TODO: implement more transformation rules
+
+ LOGGER.warn("Can not apply attribute value transformation: "
+ + LangUtil.getSimpleClassName(value) + " --> "
+ + LangUtil.getSimpleClassName(destType) + " (for '" + value
+ + "')");
+
+ return null;
}
+ /**
+ * @returns a "cleaned" (DBF compatible) proposal for a attribute name based
+ * on the existing name.
+ */
+ public static String cleanAttname(String name) {
+ return cleanAttname(name, null);
+ }
+
+ static public final Pattern StringStartsWithNumber = Pattern
+ .compile("^\\d.*$");
+
+ /**
+ * @returns a "cleaned" (DBF compatible) proposal for a attribute name based
+ * on the existing name.
+ */
+ public static String cleanAttname(String name, Integer counter) {
+
+ String clean = IOUtil.cleanFilename(name);
+ clean = clean.toUpperCase();
+
+ if (StringStartsWithNumber.matcher(clean).find()) {
+ clean = "_" + clean;
+ }
+
+ clean = clean.substring(0, Math.min(11, clean.length() - 1));
+
+ if (!FeatureUtil.checkAttributeNameRestrictions(clean)) {
+ if (counter != null)
+ return "COLUMN" + counter;
+ else
+ return "COLUMN" + new Random().nextInt(9999);
+ }
+
+ return clean;
+ }
+
// /**
// * Extrahiert alle Geometrien aus einer FeatureCollection. Fuer jedes
// * Geometry-Attribut der FeatureCollection wird eine GeometrieCollection
Modified: trunk/src/schmitzm/geotools/io/GeoImportUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/io/GeoImportUtil.java 2010-10-14 20:39:04 UTC (rev 1123)
+++ trunk/src/schmitzm/geotools/io/GeoImportUtil.java 2010-10-14 20:58:06 UTC (rev 1124)
@@ -1091,7 +1091,7 @@
/**
* Liest das CRS aus einer URL. Wenn keine CRS gelesen werden kann, dann
- * wird die Endung der URL gegen .prj gewechselt und nochmal versucht bevor
+ * wird die Endung der URL gegen .prj und .PRJ gewechselt und nochmal versucht bevor
* das {@link #DEFAULT_CRS} benutzt wird.
*
* @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Tzeggai</a>
@@ -1099,8 +1099,8 @@
public static CoordinateReferenceSystem determineProjection(URL prjUrl) {
CoordinateReferenceSystem crs = null;
// Versuchen CRS aus angegebener Datei zu lesen
- LOGGER.debug("determineProjection: Try to read a projection from URL "
- + prjUrl);
+// LOGGER.debug("determineProjection: Try to read a projection from URL "
+// + prjUrl);
try {
crs = GeoImportUtil.readProjectionFile(prjUrl);
} catch (IOException e) {
@@ -1115,6 +1115,15 @@
}
}
+ if (crs == null) {
+ try {
+ crs = GeoImportUtil.readProjectionFile(IOUtil.changeUrlExt(
+ prjUrl, "PRJ"));
+ } catch (IllegalArgumentException e) {
+ } catch (IOException e) {
+ }
+ }
+
// Wenn nicht erfolgreich, Default verwenden
if (crs == null) {
LOGGER.warn("No projection found in URL. Default CRS used.");
Modified: trunk/src/schmitzm/jfree/JFreeChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/JFreeChartUtil.java 2010-10-14 20:39:04 UTC (rev 1123)
+++ trunk/src/schmitzm/jfree/JFreeChartUtil.java 2010-10-14 20:58:06 UTC (rev 1124)
@@ -33,9 +33,6 @@
import java.awt.Color;
import java.awt.Stroke;
import java.text.DecimalFormat;
-import java.text.FieldPosition;
-import java.text.NumberFormat;
-import java.text.ParsePosition;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Modified: trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java 2010-10-14 20:39:04 UTC (rev 1123)
+++ trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java 2010-10-14 20:58:06 UTC (rev 1124)
@@ -70,7 +70,6 @@
import org.jfree.chart.urls.StandardXYURLGenerator;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.data.Range;
-import org.jfree.data.general.Dataset;
import schmitzm.jfree.JFreeChartUtil;
import schmitzm.jfree.chart.StyledToolTipGenerator;
Modified: trunk/src/skrueger/i8n/I8NUtil.java
===================================================================
--- trunk/src/skrueger/i8n/I8NUtil.java 2010-10-14 20:39:04 UTC (rev 1123)
+++ trunk/src/skrueger/i8n/I8NUtil.java 2010-10-14 20:58:06 UTC (rev 1124)
@@ -36,7 +36,6 @@
import java.util.TreeSet;
import org.apache.log4j.Logger;
-import org.geotools.resources.i18n.Locales;
public class I8NUtil {
static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
Modified: trunk/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java 2010-10-14 20:39:04 UTC (rev 1123)
+++ trunk/src_junit/schmitzm/geotools/feature/FeatureUtilTest.java 2010-10-14 20:58:06 UTC (rev 1124)
@@ -1,24 +1,27 @@
package schmitzm.geotools.feature;
+import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
-import java.io.IOException;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.junit.Test;
+import org.junit.internal.runners.statements.Fail;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.GeometryDescriptor;
import schmitzm.geotools.feature.FeatureUtil.GeometryForm;
-import schmitzm.geotools.io.GeoImportUtil;
import schmitzm.swing.TestingUtil;
import schmitzm.swing.TestingUtil.TestDatasets;
import skrueger.geotools.io.GeoImportUtilURL;
@@ -107,6 +110,9 @@
destAttrs[0] = new AttributeModificationRule(0);
destAttrs[1] = new AttributeModificationRule(3, "arab", null, null);
destAttrs[2] = new AttributeModificationRule(2);
+ Set<Object> nullValues = new HashSet<Object>();
+ nullValues.add(39207);
+ destAttrs[2].setNullValues(nullValues);
// // Umwandlung starten
FeatureUtil.modifyFeatureSource(sourceFs, outputFs, destAttrs);
@@ -118,14 +124,55 @@
// Schema testen
assertEquals(3, correctedFC.getSchema().getAttributeCount());
- assertEquals("the_geom", sourceFs.getSchema().getDescriptor(0)
+ assertEquals("the_geom", correctedFC.getSchema().getDescriptor(0)
.getLocalName());
- assertEquals("arab", sourceFs.getSchema().getDescriptor(1)
+
+ assertEquals("arab", correctedFC.getSchema().getDescriptor(1)
.getLocalName());
- assertEquals("SURFACE", sourceFs.getSchema().getDescriptor(2)
+
+ assertEquals("SURFACE", correctedFC.getSchema().getDescriptor(2)
.getLocalName());
- assertEquals(Integer.class, sourceFs.getSchema().getDescriptor(2)
+ assertEquals(Integer.class, correctedFC.getSchema().getDescriptor(2)
.getType().getBinding());
+
+
}
+ @Test
+ public void testTransformAttributeValue()
+ {
+ assertNull( FeatureUtil.transformAttributeValue(null, Integer.class) );
+
+ assertEquals("3", FeatureUtil.transformAttributeValue(3, String.class) );
+ assertEquals("3", FeatureUtil.transformAttributeValue("3", String.class) );
+ assertEquals("3.0", FeatureUtil.transformAttributeValue(3., String.class) );
+
+ assertEquals(3., FeatureUtil.transformAttributeValue(3, Double.class) );
+ assertEquals(3., FeatureUtil.transformAttributeValue("3", Double.class) );
+ assertEquals(3., FeatureUtil.transformAttributeValue(3l, Double.class) );
+
+ assertEquals(3l, FeatureUtil.transformAttributeValue(3, Long.class) );
+ assertEquals(3l, FeatureUtil.transformAttributeValue("3", Long.class) );
+ assertEquals(3l, FeatureUtil.transformAttributeValue(3l, Long.class) );
+
+ assertEquals(3l, FeatureUtil.transformAttributeValue(3, Long.class) );
+ assertEquals(3l, FeatureUtil.transformAttributeValue("3", Long.class) );
+ assertEquals(3l, FeatureUtil.transformAttributeValue(3l, Long.class) );
+
+ try {
+ FeatureUtil.transformAttributeValue("A", Long.class);
+ fail();
+ } catch (Exception e) {
+
+ }
+ }
+
+ @Test
+ public void testCleanAttname()
+ {
+ assertEquals("AEOEUSH", FeatureUtil.cleanAttname("äöush"));
+ assertEquals("_12312312", FeatureUtil.cleanAttname("12312312"));
+ assertEquals("_123123123", FeatureUtil.cleanAttname("123123123123"));
+ }
+
}
More information about the Schmitzm-commits
mailing list