001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) Copyright IBM Corporation, 2005-2007. All rights reserved.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.data.db2;
018:
019: import com.vividsolutions.jts.geom.Envelope;
020: import org.geotools.data.DefaultQuery;
021: import org.geotools.data.FeatureSource;
022: import org.geotools.data.Query;
023: import org.geotools.factory.CommonFactoryFinder;
024: import org.geotools.feature.Feature;
025: import org.geotools.feature.FeatureCollection;
026: import org.geotools.feature.FeatureType;
027: import org.geotools.filter.IllegalFilterException;
028: import org.opengis.filter.FilterFactory2;
029: import org.opengis.filter.spatial.BBOX;
030: import org.opengis.referencing.crs.CoordinateReferenceSystem;
031: import java.io.IOException;
032: import java.util.Iterator;
033:
034: /**
035: * Exercise DB2FeatureSource
036: *
037: * @author David Adler - IBM Corporation
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/test/java/org/geotools/data/db2/DB2FeatureSourceTest.java $
039: */
040: public class DB2FeatureSourceTest extends DB2TestCase {
041: private DB2DataStore dataStore = null;
042: private Envelope placesEnv1 = new Envelope(-74.15, -74.1, 42.0,
043: 42.02);
044: private Envelope placesEnv2 = new Envelope(-74.15, -74.12, 42.0,
045: 42.01);
046: private Envelope roadsEnv1 = new Envelope(600000.0, 604000.0,
047: 1160000.0, 1162000.0);
048: FilterFactory2 ff = (FilterFactory2) CommonFactoryFinder
049: .getFilterFactory(null);
050:
051: /**
052: * Get a DB2DataStore that we will use for all the tests.
053: *
054: * @throws Exception
055: */
056: public void setUp() throws Exception {
057: super .setUp();
058: dataStore = getDataStore();
059: }
060:
061: public void testGetBounds() throws Exception {
062: FeatureSource featureSource;
063: Envelope env;
064: String coordString = null;
065: DefaultQuery query;
066:
067: //Test "Places" - all bounds
068: featureSource = new DB2FeatureSource(dataStore, dataStore
069: .getSchema("Places"));
070: env = featureSource.getBounds();
071: assertEquals(
072: "all places bounds",
073: "ReferencedEnvelope[-74.160507 : -74.067637, 41.993695 : 42.05990399999999]",
074: env.toString());
075:
076: env = featureSource.getBounds(Query.ALL);
077: coordString = env2CoordString(env);
078: assertEquals(
079: "all places bounds",
080: "Env[-74.160507 : -74.067637, 41.993695 : 42.05990399999999]",
081: coordString);
082:
083: //Test "Roads" - all bounds
084: featureSource = dataStore.getFeatureSource("Roads");
085: env = featureSource.getBounds();
086: coordString = env2CoordString(env);
087: assertEquals("all roads bounds",
088: "Env[598054.2 : 604430.47, 1158025.78 : 1165565.78]",
089: coordString);
090:
091: //Test "Roads" - roadsEnv1 bbox
092: query = getBBOXQuery(featureSource, roadsEnv1);
093: env = featureSource.getBounds(query);
094: coordString = env2CoordString(env);
095: assertEquals("all roads bounds",
096: "Env[599280.58 : 604430.47, 1159468.47 : 1162830.55]",
097: coordString);
098: }
099:
100: private void checkFidTable(String featureName, String testValue)
101: throws IOException {
102: FeatureSource featureSource;
103: FeatureCollection features;
104: Iterator it;
105:
106: featureSource = dataStore.getFeatureSource(featureName);
107: features = featureSource.getFeatures();
108: it = features.iterator();
109: while (it.hasNext()) {
110: Feature f = (Feature) it.next();
111: String s = f.toString();
112: int pos = testValue.indexOf(s);
113: System.out.println(pos + s);
114: assertTrue(featureName, (pos >= 0));
115: }
116:
117: }
118:
119: public void testFidTables() throws Exception {
120: checkFidTable(
121: "FIDMCOLPRIKEY",
122: "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) ]");
123: checkFidTable(
124: "FIDCHARPRIKEY",
125: "Feature[ id=FIDCHARPRIKEY.key1 , IDCOL=key1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDCHARPRIKEY.key2 , IDCOL=key2 , GEOM=POINT (-76.5 42) ]");
126: checkFidTable(
127: "FIDVCHARPRIKEY",
128: "Feature[ id=FIDVCHARPRIKEY.key1 , IDCOL=key1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDVCHARPRIKEY.key2 , IDCOL=key2 , GEOM=POINT (-76.5 42) ]");
129: checkFidTable(
130: "FIDNOPRIKEY",
131: "Feature[ id=FIDNOPRIKEY.2 , IDCOL=1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDNOPRIKEY.3 , IDCOL=2 , GEOM=POINT (-76.5 42) ]");
132: checkFidTable(
133: "FIDINTPRIKEY",
134: "Feature[ id=FIDINTPRIKEY.1 , IDCOL=1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDINTPRIKEY.2 , IDCOL=2 , GEOM=POINT (-76.5 42) ]");
135: checkFidTable(
136: "FIDAUTOINC",
137: "Feature[ id=FIDAUTOINC.1 , GEOM=POINT (-76 42.5) ];Feature[ id=FIDAUTOINC.2 , GEOM=POINT (-76.5 42) ]");
138: }
139:
140: public void testGetCount() throws Exception {
141: FeatureSource featureSource;
142: int count;
143:
144: // Check "Roads"
145: featureSource = dataStore.getFeatureSource("Roads");
146: count = featureSource.getCount(Query.ALL);
147: assertEquals("all roads count", 87, count);
148:
149: count = featureSource.getCount(getBBOXQuery(featureSource,
150: roadsEnv1));
151: assertEquals("all roads count", 28, count);
152:
153: // Check "Places"
154: featureSource = dataStore.getFeatureSource("Places");
155: count = featureSource.getCount(Query.ALL);
156: assertEquals("all places count", 3, count);
157:
158: count = featureSource.getCount(getBBOXQuery(featureSource,
159: placesEnv1));
160: assertEquals("bbox1 places count", 3, count);
161:
162: count = featureSource.getCount(getBBOXQuery(featureSource,
163: placesEnv2));
164: assertEquals("bbox2 places count", 1, count);
165: }
166:
167: public void testCRS() throws IOException {
168: FeatureSource featureSource;
169: CoordinateReferenceSystem crs;
170:
171: // Check "Roads"
172: featureSource = dataStore.getFeatureSource("Roads");
173: crs = featureSource.getSchema().getDefaultGeometry()
174: .getCoordinateSystem();
175: assertEquals("CRS mismatch",
176: "NAD_1983_StatePlane_New_York_East_FIPS_3101_Feet", crs
177: .getName().toString());
178:
179: // Check "Places"
180: featureSource = dataStore.getFeatureSource("Places");
181: crs = featureSource.getSchema().getDefaultGeometry()
182: .getCoordinateSystem();
183: assertEquals("CRS mismatch", "GCS_North_American_1983", crs
184: .getName().toString());
185: }
186:
187: public void testSchema() throws IOException {
188: FeatureSource featureSource;
189: CoordinateReferenceSystem crs;
190: featureSource = dataStore.getFeatureSource("Roads");
191:
192: String schemaFound = featureSource.getSchema().toString();
193: String schemaCompare = "DefaultFeatureType [name=Roads , namespace=Test , abstract=false , types=(DefaultAttributeType [name=ID , type=class java.lang.Integer , nillable=true, min=1, max=1],DefaultAttributeType [name=Name , type=class java.lang.String , nillable=true, min=0, max=0],DefaultAttributeType [name=Length , type=class java.lang.Double , nillable=true, min=0, max=0],DefaultAttributeType [name=Geom , type=class com.vividsolutions.jts.geom.LineString , nillable=true, min=0, max=0],)]";
194: System.out.println("schema: " + schemaFound);
195: assertEquals("schema mismatch", schemaCompare, schemaFound);
196: }
197:
198: private BBOX getBBOXFilter(FeatureSource featureSource, Envelope env)
199: throws IllegalFilterException {
200:
201: double xmin = env.getMinX();
202: double ymin = env.getMinY();
203: double xmax = env.getMaxX();
204: double ymax = env.getMaxY();
205:
206: BBOX bbox = ff.bbox("Geom", xmin, ymin, xmax, ymax, "");
207:
208: return bbox;
209: }
210:
211: private DefaultQuery getBBOXQuery(FeatureSource featureSource,
212: Envelope env) throws IllegalFilterException {
213: BBOX bbox = getBBOXFilter(featureSource, env);
214: FeatureType ft = featureSource.getSchema();
215:
216: return new DefaultQuery(ft.getTypeName(), bbox);
217: }
218:
219: public String env2CoordString(Envelope env) {
220: String result = null;
221: result = "Env[" + env.getMinX() + " : " + env.getMaxX() + ", "
222: + env.getMinY() + " : " + env.getMaxY() + "]";
223: return result;
224: }
225: }
|