Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 * Author: Matthew Perry 
7
 * '$Date: 2006-09-13 16:10:55 -0700 (Wed, 13 Sep 2006) $'
8
 * '$Revision: 3044 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat.spatial;
26

    
27
import org.geotools.feature.FeatureType;
28
import org.geotools.data.shapefile.ShapefileDataStore;
29
import org.geotools.data.FeatureStore;
30
import org.geotools.data.FeatureSource;
31
import org.geotools.data.DefaultTransaction;
32
import org.geotools.data.Transaction;
33
import org.geotools.feature.Feature;
34
import org.geotools.filter.Filter;
35
import org.geotools.filter.AbstractFilter;
36
import org.geotools.filter.Expression;
37
import org.geotools.filter.CompareFilter;
38
import org.geotools.filter.GeometryFilter;
39
import org.geotools.filter.FilterFactory;
40
import org.geotools.filter.FilterFactoryFinder;
41
import org.geotools.filter.IllegalFilterException;
42
import org.geotools.feature.FeatureCollection;
43
import org.geotools.feature.FeatureCollections;
44
import com.vividsolutions.jts.geom.Envelope;
45

    
46
import java.io.File;
47
import java.net.URI;
48
import java.net.URL;
49
import java.net.MalformedURLException;
50
import java.sql.ResultSet;
51
import java.sql.PreparedStatement;
52
import java.util.Vector;
53
import java.util.Iterator; 
54
import java.io.IOException;
55

    
56
import org.apache.log4j.Logger;
57

    
58
import edu.ucsb.nceas.metacat.MetaCatUtil;
59

    
60
/** 
61
 * Class to query the persistent spatial cache
62
 * and returns docids matching spatial constraints
63
 */
64
public class SpatialQuery {
65
 
66
  private static Logger log = Logger.getLogger(SpatialQuery.class.getName());
67
 
68
  /** 
69
   * empty constructor to initialize spatial query
70
   */
71
  public SpatialQuery() { }
72
 
73
  /**
74
   * Querys all features in the spatial cache 
75
   * and filters based on bouding coordinates.
76
   * Returns Vector of docids.
77
   *
78
   * @param w West bounding coordinate 
79
   * @param s South bounding coordinate 
80
   * @param e East bounding coordinate 
81
   * @param n North bounding coordinate 
82
   *
83
   */
84
  public Vector filterByBbox( Float w, Float s, Float e, Float n ) {
85
      Vector docids = new Vector();
86
      SpatialFeatureSchema featureSchema = new SpatialFeatureSchema();
87
      
88
      // MPTDO, perform geotools spatial query
89
      // test
90
      docids.add("test1.1");
91
      docids.add("test2.1");
92

    
93
      ShapefileDataStore store = null;
94
      FeatureSource features = null;
95
      FeatureCollection collection = null;
96
      FilterFactory filterFactory = FilterFactoryFinder.createFilterFactory();
97

    
98
      try {
99
          store = new ShapefileDataStore( (new File( featureSchema.polygonShpUri )).toURL() );
100
          features = store.getFeatureSource(store.getTypeNames()[0]);
101
          //count features
102
          //col = features.getFeatures();
103
          //System.out.println("# cities (no filter)= "+col.size());
104

    
105
          Envelope envelope = new Envelope( w, e, s, n ); 
106
          Expression bbox = filterFactory.createBBoxExpression( envelope );
107

    
108
          FeatureType featureType = store.getSchema( store.getTypeNames()[0] );
109
          Expression geometry = filterFactory.createAttributeExpression( featureType.getDefaultGeometry().getName()  );
110
          GeometryFilter bboxFilter = filterFactory.createGeometryFilter(AbstractFilter.GEOMETRY_BBOX);
111
          bboxFilter.addLeftGeometry( geometry );
112
          bboxFilter.addRightGeometry( bbox );
113

    
114
          //count filtered features
115
          collection = features.getFeatures(bboxFilter);
116
          //System.out.println("# cities (after filter)= "+col.size());
117

    
118
          Iterator iterator = collection.iterator();
119
          try {
120
              for( Iterator i=collection.iterator(); i.hasNext(); ) {
121
                  Feature feature = (Feature) i.next();
122
                  docids.add( (String) feature.getAttribute(1) );
123
              }
124
          } finally {
125
              collection.close( iterator );
126
          }
127
         
128
        } catch (MalformedURLException ex) {
129
            ex.printStackTrace();
130
        } catch (IOException ex) {
131
            ex.printStackTrace();
132
        } catch (IllegalFilterException ex) {
133
            ex.printStackTrace();
134
        }
135

    
136

    
137
      return docids;
138

    
139
  }
140

    
141
}
(6-6/8)