POM.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>18-SNAPSHOT</geotools.version>
</properties>
<dependencies>
<!-- Provides map projections -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- Provides support for PostGIS. Note the different groupId -->
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- Provides support for shapefiles -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- Provides GUI components -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
package com.mapbar.geo.main;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.GeoTools;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.filter.text.cql2.CQL;
import org.geotools.filter.text.cql2.CQLException;
import org.geotools.xml.Configuration;
import org.geotools.xml.Parser;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.identity.FeatureId;
import org.xml.sax.SAXException;
/**
*
* Class FilterExam.java
*
* Description GeoTools Filter 应用
* GeoTools makes use of the GeoAPI project Filter interface in order to express constraints.
* This is most often used when making a Query to retrieve specific Features from a DataStore
* notice:GeoTools FilterFactory is deprecated
*
* Company mapbar
*
* author Chenll E-mail: Chenll@mapbar.com
*
* Version 1.0
*
* Date 2012-2-15 下午02:27:44
*/
public class FilterExam {
//使用openGIS FilterFactory2
private static FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());
/**
* 获取数据
*
* @param shpPath
* @return
* @throws IOException
*/
public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureS(
String shpPath) throws IOException {
ShapefileDataStore store = new ShapefileDataStore(new File(shpPath)
.toURI().toURL());
store.setStringCharset(Charset.forName("GBK"));
FeatureSource<SimpleFeatureType, SimpleFeature> features = store
.getFeatureSource(store.getTypeNames()[0]);
return features;
}
/**
* 获取feature id 集合
* @param fs
* @return
* @throws IOException
*/
public Set<FeatureId> getFeatureId(
FeatureSource<SimpleFeatureType, SimpleFeature> fs) throws IOException {
FeatureIterator<SimpleFeature> itertor = fs.getFeatures().features();
Set<FeatureId> fids = new HashSet<FeatureId>();
while (itertor.hasNext()) {
SimpleFeature feature = itertor.next();
fids.add(feature.getIdentifier());
}
itertor.close();
return fids;
}
/**
* 使用feature id 作为过滤条件
* @param fs
* @return
* @throws IOException
*/
public FeatureCollection filterFidEx(
FeatureSource<SimpleFeatureType, SimpleFeature> fs)
throws IOException {
Set<FeatureId> fids = getFeatureId(fs);
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());
Filter filt = (Filter) ff.id(fids);
FeatureCollection col = fs.getFeatures(filt);
return col;
}
/**
* 相等,不等,超过,不超过几种情况
*
* @param fs
* @return
* @throws IOException
*/
public FeatureCollection compareFilterEx(
FeatureSource<SimpleFeatureType, SimpleFeature> fs)
throws IOException {
Filter left = ff.equals(ff.property( "NAME" ), ff.literal( "4路(长兴-火车站)" ));
FeatureCollection col = fs.getFeatures(left);
return col;
}
/**
* 过滤器对xml的支持,即把条件写在xml文件里面,这样做到可以配置
* @param fs
* @return
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public FeatureCollection filterXML(FeatureSource<SimpleFeatureType, SimpleFeature> fs,String fileName) throws IOException, SAXException, ParserConfigurationException{
Configuration configuration = new org.geotools.filter.v1_0.OGCConfiguration();
Parser parser = new Parser( configuration );
InputStream xml = ClassLoader.getSystemResourceAsStream(fileName);
//parse
Filter filter = (Filter) parser.parse( xml );
FeatureCollection col = fs.getFeatures(filter);
return col;
}
/**
* CQL (common query language)
* @param fs
* @param name
* @return
* @throws CQLException
* @throws IOException
* Filter f = CQL.toFilter("ATTR1 < 10 AND ATTR2 < 2 OR ATTR3 > 10");
Filter f = CQL.toFilter("NAME = 'New York' ");
Filter f = CQL.toFilter("NAME LIKE 'New%' ");
Filter f = CQL.toFilter("NAME IS NULL");
Filter f = CQL.toFilter("DATE BEFORE 2006-11-30T01:30:00Z");
Filter f = CQL.toFilter("NAME DOES-NOT-EXIST");
Filter f = CQL.toFilter("QUANTITY BETWEEN 10 AND 20");
Filter f = CQL.toFilter("CROSSES(SHAPE, LINESTRING(1 2, 10 15))");
Filter f = CQL.toFilter("BBOX(SHAPE, 10,20,30,40)");
Expression e = CQL.toExpression("NAME");
Expression e = CQL.toExpression("QUANTITY * 2");
Expression e = CQL.toExpression("strConcat(NAME, 'suffix')");
List filters = CQL.toFilterList("NAME IS NULL;BBOX(SHAPE, 10,20,30,40);INCLUDE");
*/
public FeatureCollection filterCQL(FeatureSource<SimpleFeatureType, SimpleFeature> fs,String name) throws CQLException, IOException{
return fs.getFeatures(CQL.toFilter("NAME like '%" + name + "%'"));
}
public static void main(String[] args) throws IOException, CQLException, SAXException, ParserConfigurationException {
FilterExam fe = new FilterExam();
FeatureSource<SimpleFeatureType, SimpleFeature> fs = fe.getFeatureS("D:\\安康公交线_polyline.shp");
FeatureCollection col1 = fe.compareFilterEx(fs);
System.out.println(col1.size());
FeatureCollection col2 = fe.filterFidEx(fs);
System.out.println(col2.size());
FeatureCollection col3 = fe.filterCQL(fs, "13路");
System.out.println(col3.size());
FeatureCollection col4 = fe.filterXML(fs, "filter.xml");
System.out.println(col4.size());
}
}
filter.xml 文件格式定义如下:
<Filter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns="http://www.opengis.net/ogc"
xsi:schemaLocation="http://www.opengis.net/ogc filter.xsd">
<PropertyIsEqualTo>
<PropertyName>NAME</PropertyName>
<Literal>13路上行(火车站-市工商局)</Literal>
</PropertyIsEqualTo>
</Filter>