GIS的学习(二十三)geoserver中CQL和ECQL的使用
以下引用自官方文档:
CQL and ECQL¶
CQL (Common Query Language) is a query language created by the OGC for the Catalogue Web Services specification. Unlike the XML-based Filter Encoding language, CQL is written using a familiar text-based syntax. It is thus more readable and better-suited for manual authoring.
However, CQL has some limitations. For example it cannot encode id filters, and it requires an attribute to be on the left side of any comparison operator. For this reason, GeoServer provides an extended version of CQL called ECQL. ECQL removes the limitations of CQL, providing a more flexible language with stronger similarities with SQL.
GeoServer supports the use of both CQL and ECQL in WMS and WFS requests, as well as in GeoServer’s SLD dynamic symbolizers. Whenever the documentation refers to CQL, ECQL syntax can be used as well (and if not, please report that as a bug!).
This tutorial introduces the CQL/ECQL language by example. For a full reference, refer to the ECQL Reference.
实例如下:
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/longgangbai/article/details/84287020