GIS的学习(二十三)geoserver中CQL和ECQL的使用
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;
/**
* 采用geotools中公共查询语言
* 过滤条件如下
*
* 例如:
* PERSONS > 15000000
* PERSONS BETWEEN 1000000 AND 3000000
* STATE_NAME LIKE ‘N%’
* STATE_NAME = ‘California’
* MALE > FEMALE
* UNEMPLOY / (EMPLOYED + UNEMPLOY) > 0.07
* IN (‘states.1’, ‘states.12’):
* STATE_NAME IN (‘New York’, ‘California’, ‘Montana’, ‘Texas’):
* 带函数的使用:
* strToLowerCase(STATE_NAME) like ‘%m%’
*
*
*
* @Title:
* @Description: 实现TODO
* @Copyright:Copyright (c) 2011
* @Company:
* @Date:2012-9-10
* @author longgangbai
* @version 1.0
*/
public class GeoServerCQLECQL {
/**
*
* @param filterStr
* @param layerName
* @return
* @throws IOException
*/
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 );
// Step 2 – connection
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); // filterStr形式 如 name=’武汉大学’ or code like ‘tt123%’
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
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());
}
}
转载自:https://blog.csdn.net/sxausgyy/article/details/8150515