001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
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: package org.geotools.demo.data;
017:
018: import java.io.IOException;
019: import java.net.URL;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.geotools.data.DataStore;
025: import org.geotools.data.DataStoreFinder;
026: import org.geotools.data.FeatureSource;
027: import org.geotools.factory.CommonFactoryFinder;
028: import org.geotools.feature.Feature;
029: import org.geotools.feature.FeatureCollection;
030: import org.opengis.filter.FilterFactory2;
031: import org.opengis.filter.expression.Literal;
032: import org.opengis.filter.expression.PropertyName;
033: import org.opengis.filter.spatial.Intersects;
034:
035: import com.vividsolutions.jts.geom.Coordinate;
036: import com.vividsolutions.jts.geom.GeometryFactory;
037: import com.vividsolutions.jts.geom.Point;
038:
039: /**
040: * Examples of using filter with a provided file.
041: * <p>
042: * We use the bc_2m_lakes.shp included as test data, and will try out a range of
043: * geospatial filters for your amusement.
044: * </p>
045: *
046: * @author Gearhard
047: * @source $URL:
048: * http://svn.geotools.org/geotools/trunk/gt/demo/data/src/org/geotools/demo/data/ShapeReader.java $
049: */
050: public class FilterExample {
051:
052: private static URL getResource(String path) {
053: return FilterExample.class.getResource(path);
054: }
055:
056: /** Just run it */
057: public static void main(String[] args) {
058: try {
059: polygonIntersectsPoint();
060: } catch (Exception e) {
061: System.out.println("Ops! Something went wrong :-(");
062: e.printStackTrace();
063: }
064: System.exit(0);
065: }
066:
067: private static void polygonIntersectsPoint() throws IOException {
068: // locate the data
069: URL url = getResource("test-data/bc_2m_lakes.shp");
070:
071: // set up connection parameters
072: Map params = new HashMap();
073: params.put("url", url);
074:
075: // connect to the data
076: DataStore dataStore = DataStoreFinder.getDataStore(params);
077:
078: String typeName = dataStore.getTypeNames()[0];
079: FeatureSource featureSource = dataStore
080: .getFeatureSource(typeName);
081:
082: // create a filter
083: FilterFactory2 filterFactory = (FilterFactory2) CommonFactoryFinder
084: .getFilterFactory(null);
085:
086: // look up geometry attribute name used for testing
087: //
088: String geometryName = featureSource.getSchema()
089: .getDefaultGeometry().getName();
090:
091: // Create the attribute expression
092: PropertyName geometryExpression = filterFactory
093: .property(geometryName);
094:
095: // Create the point used for testing
096: //
097: Coordinate pointCoordinate = new Coordinate(-116.883011765104,
098: 50.0001019506261);
099:
100: GeometryFactory geometryFactory = new GeometryFactory();
101: Point point = geometryFactory.createPoint(pointCoordinate);
102:
103: // Create the literal expression
104: Literal pointExpression = filterFactory.literal(point);
105:
106: // create the actual test
107: //
108: Intersects withinFilter = filterFactory.intersects(
109: geometryExpression, pointExpression);
110:
111: // grab features matching the filter
112: FeatureCollection featureCollection = featureSource
113: .getFeatures(withinFilter);
114:
115: // now print out the first 10 features
116: //
117: Iterator iterator = featureCollection.iterator();
118: try {
119: int count = 0;
120: if (iterator.hasNext()) {
121: while (iterator.hasNext()) {
122: Feature feature = (Feature) iterator.next();
123: System.out.println(feature);
124: if ((++count) == 10)
125: break; // only 10 please
126: }
127: } else {
128: System.out.println("Point not found in any Polygons.");
129: }
130: } finally {
131: featureCollection.close(iterator);
132: }
133: }
134: }
|