- package com.geoserver;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
-
- import org.geotools.data.DataStore;
- import org.geotools.data.DataStoreFinder;
- import org.geotools.data.simple.SimpleFeatureCollection;
- import org.geotools.data.simple.SimpleFeatureSource;
- import org.geotools.feature.FeatureIterator;
- import org.geotools.filter.text.cql2.CQL;
- import org.geotools.filter.text.cql2.CQLException;
- import org.geotools.geometry.jts.ReferencedEnvelope;
- import org.opengis.feature.simple.SimpleFeature;
- import org.opengis.filter.Filter;
- public class GeoServerCQLECQL {
-
- public static ArrayList<SimpleFeature> queryMethod(String filterStr,String layerName) throws IOException {
- String getCapabilities = "http://localhost:8080/geoserver/wfs?REQUEST=GetCapabilities";
- Map<String,String> connectionParameters = new HashMap<String,String>();
- connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities );
-
- DataStore data = DataStoreFinder.getDataStore( connectionParameters );
- SimpleFeatureSource featureSource =data.getFeatureSource(layerName);
- ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
- if(featureSource==null)
- return featureList;
- try {
- Filter filter = CQL.toFilter(filterStr);
- SimpleFeatureCollection result = featureSource.getFeatures(filter);
-
- ReferencedEnvelope bounds = new ReferencedEnvelope();
- FeatureIterator<SimpleFeature> itertor = result.features();
- while (itertor.hasNext()) {
- SimpleFeature feature = itertor.next();
- bounds.include( feature.getBounds() );
- featureList.add(feature);
- }
- System.out.println( "Calculated Bounds:"+ bounds );
- itertor.close();
- result.close( itertor );
- return featureList;
- } catch (CQLException e) {
-
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally {
-
- }
- return null;
- }
- public static void main(String[] args) throws IOException {
- ArrayList<SimpleFeature> list=queryMethod("STATE_NAME='Arizona'","topp:states");
- System.out.println("list="+list.toString());
- }
- }
相关文章:
-
2021-11-11
-
2021-09-03
-
2021-07-31
-
2021-07-08
-
2021-06-05
猜你喜欢
-
2022-12-23
-
2022-12-23
-
2021-12-05
-
2022-12-23
-
2021-12-06
-
2021-04-09
相关资源
-
下载
2023-04-04
-
下载
2023-01-22
-
下载
2021-06-24