/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
* (C) Copyright IBM Corporation, 2005-2007. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
package org.geotools.data.db2;
import java.io.IOException;
import java.util.Iterator;
import org.geotools.data.DefaultQuery;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.filter.IllegalFilterException;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.spatial.BBOX;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import com.vividsolutions.jts.geom.Envelope;
/**
* Exercise DB2FeatureSource
*
* @author David Adler - IBM Corporation
* @source $URL$
*/
public class DB2FeatureSourceOnlineTest extends AbstractDB2OnlineTestCase {
private DB2DataStore dataStore = null;
private Envelope placesEnv1 = new Envelope(-74.15, -74.1, 42.0, 42.02);
private Envelope placesEnv2 = new Envelope(-74.15, -74.12, 42.0, 42.01);
private Envelope roadsEnv1 = new Envelope(600000.0, 604000.0, 1160000.0,
1162000.0);
FilterFactory2 ff = (FilterFactory2) CommonFactoryFinder.getFilterFactory(null);
/**
* Get a DB2DataStore that we will use for all the tests.
*
* @throws Exception
*/
public void setUp() throws Exception {
super.setUp();
dataStore = getDataStore();
}
public void testGetBounds() throws Exception {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
Envelope env;
String coordString = null;
DefaultQuery query;
//Test "Places" - all bounds
featureSource = new DB2FeatureSource(dataStore,dataStore.getSchema("Places"));
env = featureSource.getBounds();
assertEquals("all places bounds",
"ReferencedEnvelope[-74.160507 : -74.067637, 41.993695 : 42.05990399999999]",
env.toString());
env = featureSource.getBounds(Query.ALL);
coordString = env2CoordString(env);
assertEquals("all places bounds",
"Env[-74.160507 : -74.067637, 41.993695 : 42.05990399999999]", coordString);
//Test "Roads" - all bounds
featureSource = dataStore.getFeatureSource("Roads");
env = featureSource.getBounds();
coordString = env2CoordString(env);
assertEquals("all roads bounds",
"Env[598054.2 : 604430.47, 1158025.78 : 1165565.78]", coordString);
//Test "Roads" - roadsEnv1 bbox
query = getBBOXQuery(featureSource, roadsEnv1);
env = featureSource.getBounds(query);
coordString = env2CoordString(env);
assertEquals("all roads bounds",
"Env[599280.58 : 604430.47, 1159468.47 : 1162830.55]",
coordString);
}
private void checkFidTable(String featureName, String testValue) throws IOException {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
FeatureCollection<SimpleFeatureType, SimpleFeature> features;
Iterator it;
featureSource = dataStore.getFeatureSource(featureName);
features = featureSource.getFeatures();
it = features.iterator();
while (it.hasNext()) {
SimpleFeature f = (SimpleFeature) it.next();
String id = f.getID();
String s = f.toString();
System.out.println(featureName + ": id=" + id);
int pos = testValue.indexOf(s);
System.out.println(pos + s);
// assertTrue(featureName, (pos >= 0));
}
}
// DWA - 04/03/08 - No longer sure what this was supposed to be testing, but the results are very different with GT 2.5
public void xtestFidTables() throws Exception {
checkFidTable("FIDMCOLPRIKEY",
"Feature[ id=FIDMCOLPRIKEY.key1+++++++&1 , IDCOL1=key1 , IDCOL2=1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDMCOLPRIKEY.key2+++++++&2 , IDCOL1=key2 , IDCOL2=2 , GEOM=POINT (-76.5 42) ]");
checkFidTable("FIDCHARPRIKEY",
"Feature[ id=FIDCHARPRIKEY.key1 , IDCOL=key1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDCHARPRIKEY.key2 , IDCOL=key2 , GEOM=POINT (-76.5 42) ]");
checkFidTable("FIDVCHARPRIKEY",
"Feature[ id=FIDVCHARPRIKEY.key1 , IDCOL=key1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDVCHARPRIKEY.key2 , IDCOL=key2 , GEOM=POINT (-76.5 42) ]");
checkFidTable("FIDNOPRIKEY",
"Feature[ id=FIDNOPRIKEY.2 , IDCOL=1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDNOPRIKEY.3 , IDCOL=2 , GEOM=POINT (-76.5 42) ]");
checkFidTable("FIDINTPRIKEY",
"Feature[ id=FIDINTPRIKEY.1 , IDCOL=1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDINTPRIKEY.2 , IDCOL=2 , GEOM=POINT (-76.5 42) ]");
checkFidTable("FIDAUTOINC",
"Feature[ id=FIDAUTOINC.1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDAUTOINC.2 , GEOM=POINT (-76.5 42) ]");
}
public void testGetCount() throws Exception {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
int count;
// Check "Roads"
featureSource = dataStore.getFeatureSource("Roads");
count = featureSource.getCount(Query.ALL);
assertEquals("all roads count", 87, count);
count = featureSource.getCount(getBBOXQuery(featureSource, roadsEnv1));
assertEquals("all roads count", 28, count);
// Check "Places"
featureSource = dataStore.getFeatureSource("Places");
count = featureSource.getCount(Query.ALL);
assertEquals("all places count", 3, count);
count = featureSource.getCount(getBBOXQuery(featureSource, placesEnv1));
assertEquals("bbox1 places count", 3, count);
count = featureSource.getCount(getBBOXQuery(featureSource, placesEnv2));
assertEquals("bbox2 places count", 1, count);
}
public void testCRS() throws IOException {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
CoordinateReferenceSystem crs;
// Check "Roads"
featureSource = dataStore.getFeatureSource("Roads");
crs = featureSource.getSchema().getGeometryDescriptor()
.getCoordinateReferenceSystem();
assertEquals("CRS mismatch",
"NAD_1983_StatePlane_New_York_East_FIPS_3101_Feet",
crs.getName().toString());
// Check "Places"
featureSource = dataStore.getFeatureSource("Places");
crs = featureSource.getSchema().getGeometryDescriptor()
.getCoordinateReferenceSystem();
assertEquals("CRS mismatch", "GCS_North_American_1983",
crs.getName().toString());
}
public void testSchema() throws IOException {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
featureSource = dataStore.getFeatureSource("Roads");
String schemaFound = featureSource.getSchema().toString();
String schemaCompare = "SimpleFeatureTypeImpl[name=TestRoads, binding=interface java.util.Collection, abstrsct= false, identified=true, restrictions=[], superType=SimpleFeatureTypeImpl[name=Feature, binding=interface java.util.Collection, abstrsct= true, identified=true, restrictions=[], superType=null, schema=[]], schema=[AttributeDescriptorImpl:type=org.geotools.feature.type.AttributeTypeImpl:name=ID; binding=class java.lang.Integer; isAbstrsact=, false; restrictions=[]; description=null; super=[null]; isIdentified=false;name=ID;minOccurs=1;maxOccurs=1;isNillable=true;defaultValue=null, AttributeDescriptorImpl:type=org.geotools.feature.type.AttributeTypeImpl:name=Name; binding=class java.lang.String; isAbstrsact=, false; restrictions=[]; description=null; super=[null]; isIdentified=false;name=Name;minOccurs=0;maxOccurs=1;isNillable=true;defaultValue=null, AttributeDescriptorImpl:type=org.geotools.feature.type.AttributeTypeImpl:name=Length; binding=class java.lang.Double; isAbstrsact=, false; restrictions=[]; description=null; super=[null]; isIdentified=false;name=Length;minOccurs=0;maxOccurs=1;isNillable=true;defaultValue=null, GeometryDescriptorImpl:type=org.geotools.feature.type.GeometryTypeImpl:name=Geom; binding=class com.vividsolutions.jts.geom.LineString; isAbstrsact=, false; restrictions=[]; description=null; super=[null]; isIdentified=false;name=Geom;minOccurs=0;maxOccurs=1;isNillable=true;defaultValue=null]]";
System.out.println("schema: " + schemaFound);
assertEquals("schema mismatch", schemaCompare, schemaFound);
}
private BBOX getBBOXFilter(FeatureSource<SimpleFeatureType, SimpleFeature> featureSource,
Envelope env) throws IllegalFilterException {
double xmin = env.getMinX();
double ymin = env.getMinY();
double xmax = env.getMaxX();
double ymax = env.getMaxY();
BBOX bbox = ff.bbox("Geom",xmin,ymin,xmax,ymax,"");
return bbox;
}
private DefaultQuery getBBOXQuery(FeatureSource<SimpleFeatureType, SimpleFeature> featureSource, Envelope env)
throws IllegalFilterException {
BBOX bbox = getBBOXFilter(featureSource, env);
SimpleFeatureType ft = featureSource.getSchema();
return new DefaultQuery(ft.getTypeName(), bbox);
}
public String env2CoordString(Envelope env) {
String result = null;
result = "Env[" + env.getMinX() + " : " + env.getMaxX()
+ ", " + env.getMinY() + " : " + env.getMaxY() + "]";
return result;
}
}