jie_er

geoserver中除了使用kml来查询数据以外,还可以使用csql或ecsql

 

  1. package com.geoserver;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import org.geotools.data.DataStore;  
  9. import org.geotools.data.DataStoreFinder;  
  10. import org.geotools.data.simple.SimpleFeatureCollection;  
  11. import org.geotools.data.simple.SimpleFeatureSource;  
  12. import org.geotools.feature.FeatureIterator;  
  13. import org.geotools.filter.text.cql2.CQL;  
  14. import org.geotools.filter.text.cql2.CQLException;  
  15. import org.geotools.geometry.jts.ReferencedEnvelope;  
  16. import org.opengis.feature.simple.SimpleFeature;  
  17. import org.opengis.filter.Filter;  
  18. /** 
  19.  * 采用geotools中公共查询语言 
  20.  * 过滤条件如下 
  21.  *  
  22.  * 例如: 
  23.  *    PERSONS > 15000000 
  24.  *    PERSONS BETWEEN 1000000 AND 3000000 
  25.  *    STATE_NAME LIKE 'N%' 
  26.  *    STATE_NAME = 'California' 
  27.  *    MALE > FEMALE 
  28.  *    UNEMPLOY / (EMPLOYED + UNEMPLOY) > 0.07 
  29.  *     IN ('states.1', 'states.12'): 
  30.  *   STATE_NAME IN ('New York', 'California', 'Montana', 'Texas'): 
  31.  *  带函数的使用: 
  32.  *     strToLowerCase(STATE_NAME) like ‘%m%’ 
  33.  *      
  34.  *      
  35.  *      
  36.  * @Title:  
  37.  * @Description: 实现TODO 
  38.  * @Copyright:Copyright (c) 2011 
  39.  * @Company: 
  40.  * @Date:2012-9-10 
  41.  * @author  longgangbai 
  42.  * @version 1.0 
  43.  */  
  44. public class GeoServerCQLECQL {  
  45.     /** 
  46.      *  
  47.      * @param filterStr 
  48.      * @param layerName 
  49.      * @return 
  50.      * @throws IOException 
  51.      */  
  52.     public static ArrayList<SimpleFeature> queryMethod(String filterStr,String layerName) throws IOException {  
  53.         String getCapabilities = "http://localhost:8080/geoserver/wfs?REQUEST=GetCapabilities";  
  54.         Map<String,String> connectionParameters = new HashMap<String,String>();  
  55.         connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities );  
  56.         // Step 2 - connection  
  57.         DataStore data = DataStoreFinder.getDataStore( connectionParameters );  
  58.         SimpleFeatureSource featureSource =data.getFeatureSource(layerName);   
  59.         ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();  
  60.         if(featureSource==null)  
  61.             return featureList;  
  62.         try {  
  63.             Filter  filter = CQL.toFilter(filterStr); // filterStr形式 如  name='武汉大学' or code like 'tt123%'  
  64.             SimpleFeatureCollection result = featureSource.getFeatures(filter);  
  65.   
  66.             ReferencedEnvelope bounds = new ReferencedEnvelope();  
  67.             FeatureIterator<SimpleFeature> itertor = result.features();  
  68.             while (itertor.hasNext()) {  
  69.                 SimpleFeature feature = itertor.next();  
  70.                 bounds.include( feature.getBounds() );  
  71.                 featureList.add(feature);  
  72.             }  
  73.              System.out.println( "Calculated Bounds:"+ bounds );  
  74.             itertor.close();  
  75.             result.close( itertor );  
  76.             return featureList;  
  77.         } catch (CQLException e) {  
  78.             // TODO Auto-generated catch block  
  79.             e.printStackTrace();  
  80.         } catch (IOException e) {  
  81.             // TODO Auto-generated catch block  
  82.             e.printStackTrace();  
  83.         }finally {  
  84.               
  85.         }  
  86.             return null;  
  87.     }  
  88.     public static void main(String[] args) throws IOException {  
  89.          ArrayList<SimpleFeature> list=queryMethod("STATE_NAME='Arizona'","topp:states");  
  90.          System.out.println("list="+list.toString());  
  91.     }  

posted on 2016-01-22 13:39  jie_er的种码园子  阅读(379)  评论(0编辑  收藏  举报

导航