GeoTools应用-Filter
2012-10-25 22:13
274 查看
org.geotools.filter
This is most often used when making a Query to retrieve specific Feature s from a DataStore
[java]
view plaincopyprint?
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 文件格式定义如下:
[html]
view plaincopyprint?
<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>
This is most often used when making a Query to retrieve specific Feature s from a DataStore
[java]
view plaincopyprint?
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());
}
}
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 文件格式定义如下:
[html]
view plaincopyprint?
<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>
相关文章推荐
- GeoTools应用-Filter
- GeoTools应用-(JTS Geometry Operations)(二)
- GeoTools应用-DATA
- GeoTools应用-JTS(Geometry之间的关系)
- GeoTools应用-DATA
- GeoTools应用-DATA
- Java Web+GeoTools工具+自定义几何对象构造器+简单应用
- eclipse平台下geotools应用开发环境(创建maven项目)
- GeoTools应用-(JTS Geometry Operations)
- GeoTools应用-(JTS Geometry Operations)(一)
- GeoTools应用-JTS(Geometry之间的关系)
- GeoTools应用-JTS(Geometry之间的关系)
- GeoTools应用-JTS(Geometry)
- Geohash的原理、算法和具体应用探究(优秀)
- Java Portlet Tools:将 Java Web 应用转换为适应性 Portlet
- java基础知识回顾之javaIO类--File类应用:过滤器接口FilenameFilter和FileFilter
- VS2005中SetUnhandledExceptionFilter函数应用
- GeoTools学习笔记--MapContext
- javaweb中Filter(过滤器)的常见应用
- DTM测试中的filter应用