Project

General

Profile

« Previous | Next » 

Revision 3048

Added by perry about 18 years ago

Make spatial query more accurate by actually intersecting geometries in addition to a simple bbox filter. Important for testing intersection of irregular shaped objects or multi-geometries

View differences:

src/edu/ucsb/nceas/metacat/spatial/SpatialQuery.java
42 42
import org.geotools.feature.FeatureCollection;
43 43
import org.geotools.feature.FeatureCollections;
44 44
import com.vividsolutions.jts.geom.Envelope;
45
import com.vividsolutions.jts.geom.Geometry;
46
import com.vividsolutions.jts.geom.GeometryFactory;
47
import com.vividsolutions.jts.geom.Polygon;
48
import com.vividsolutions.jts.geom.Coordinate;
45 49

  
46 50
import java.io.File;
47 51
import java.net.URI;
......
99 103
          Envelope envelope = new Envelope( w, e, s, n ); 
100 104
          Expression bbox = filterFactory.createBBoxExpression( envelope );
101 105

  
106
          // Construct the bbox as an actual geometry
107
          Coordinate[] linestringCoordinates = new Coordinate[5];
108
          linestringCoordinates[0] = new Coordinate( w, s );
109
          linestringCoordinates[1] = new Coordinate( w, n );
110
          linestringCoordinates[2] = new Coordinate( e, n );
111
          linestringCoordinates[3] = new Coordinate( e, s );
112
          linestringCoordinates[4] = new Coordinate( w, s );
113

  
114
          GeometryFactory geomFac = new GeometryFactory();
115
          Polygon bboxGeom = geomFac.createPolygon( geomFac.createLinearRing(linestringCoordinates), null);
116

  
102 117
          // Set up geometry filter based on bbox
103 118
          FeatureType featureType = store.getSchema( store.getTypeNames()[0] );
104 119
          Expression geometry = filterFactory.createAttributeExpression( featureType.getDefaultGeometry().getName()  );
......
113 128
          try {
114 129
              for( Iterator i=collection.iterator(); i.hasNext(); ) {
115 130
                  Feature feature = (Feature) i.next();
116
                  // assumes docid is attribute number 1 
117
                  // in a zero-based index of dbf columns
118
                  docids.add( (String) feature.getAttribute(1) );
131

  
132
                  Geometry geom = (Geometry)feature.getAttribute(0);
133
                  if ( geom.intersects( bboxGeom ) ) { 
134
                      // assumes docid is attribute number 1 
135
                      // in a zero-based index of dbf columns
136
                      docids.add( (String) feature.getAttribute(1) );
137
                  }
119 138
              }
120 139
          } finally {
121 140
              collection.close( iterator );
......
146 165
          try {
147 166
              for( Iterator i=collection.iterator(); i.hasNext(); ) {
148 167
                  Feature feature = (Feature) i.next();
149
                  // assumes docid is attribute number 1 
150
                  // in a zero-based index of dbf columns
151
                  docid = (String) feature.getAttribute(1);
152
                  if( !docids.contains( docid ) ) {
153
                      docids.add( docid );
168
                  Geometry geom = (Geometry)feature.getAttribute(0);
169
                  if ( geom.intersects( bboxGeom ) ) { 
170
                      // assumes docid is attribute number 1 
171
                      // in a zero-based index of dbf columns
172
                      docid = (String) feature.getAttribute(1);
173
                      if( !docids.contains( docid ) ) {
174
                          docids.add( docid );
175
                      }
154 176
                  }
155 177
              }
156 178
          } finally {

Also available in: Unified diff