GeoTools应用-Filter

org.geotools.filter

This is most often used when making a Query to retrieve specific Feature s from a DataStore

  1. package com.mapbar.geo.main; 
  2.  
  3. import java.io.File; 
  4. import java.io.IOException; 
  5. import java.io.InputStream; 
  6. import java.nio.charset.Charset; 
  7. import java.util.HashSet; 
  8. import java.util.Set; 
  9.  
  10. import javax.xml.parsers.ParserConfigurationException; 
  11.  
  12. import org.geotools.data.FeatureSource; 
  13. import org.geotools.data.shapefile.ShapefileDataStore; 
  14. import org.geotools.factory.CommonFactoryFinder; 
  15. import org.geotools.factory.GeoTools; 
  16. import org.geotools.feature.FeatureCollection; 
  17. import org.geotools.feature.FeatureIterator; 
  18. import org.geotools.filter.text.cql2.CQL; 
  19. import org.geotools.filter.text.cql2.CQLException; 
  20. import org.geotools.xml.Configuration; 
  21. import org.geotools.xml.Parser; 
  22. import org.opengis.feature.simple.SimpleFeature; 
  23. import org.opengis.feature.simple.SimpleFeatureType; 
  24. import org.opengis.filter.Filter; 
  25. import org.opengis.filter.FilterFactory2; 
  26. import org.opengis.filter.identity.FeatureId; 
  27. import org.xml.sax.SAXException; 
  28.  
  29. /**
  30. *
  31. * Class FilterExam.java
  32. *
  33. * Description GeoTools Filter 应用
  34. * GeoTools makes use of the GeoAPI project Filter interface in order to express constraints.
  35. * This is most often used when making a Query to retrieve specific Features from a DataStore
  36. * notice:GeoTools FilterFactory is deprecated
  37. *
  38. * Company mapbar
  39. *
  40. * author Chenll E-mail: Chenll@mapbar.com
  41. *
  42. * Version 1.0
  43. *
  44. * Date 2012-2-15 下午02:27:44
  45. */ 
  46. public class FilterExam { 
  47.      
  48.     //使用openGIS FilterFactory2 
  49.     private static FilterFactory2  ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints()); 
  50.     /**
  51.      * 获取数据
  52.      *
  53.      * @param shpPath
  54.      * @return
  55.      * @throws IOException
  56.      */ 
  57.     public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureS( 
  58.             String shpPath) throws IOException { 
  59.         ShapefileDataStore store = new ShapefileDataStore(new File(shpPath) 
  60.                 .toURI().toURL()); 
  61.         store.setStringCharset(Charset.forName(“GBK”)); 
  62.         FeatureSource<SimpleFeatureType, SimpleFeature> features = store 
  63.                 .getFeatureSource(store.getTypeNames()[0]); 
  64.         return features; 
  65.     } 
  66.  
  67.      
  68.     /**
  69.      * 获取feature id 集合
  70.      * @param fs
  71.      * @return
  72.      * @throws IOException
  73.      */ 
  74.     public Set<FeatureId> getFeatureId( 
  75.             FeatureSource<SimpleFeatureType, SimpleFeature> fs)
    throws IOException { 
  76.         FeatureIterator<SimpleFeature> itertor = fs.getFeatures().features(); 
  77.         Set<FeatureId> fids = new HashSet<FeatureId>(); 
  78.         while (itertor.hasNext()) { 
  79.             SimpleFeature feature = itertor.next(); 
  80.             fids.add(feature.getIdentifier()); 
  81.         } 
  82.         itertor.close(); 
  83.         return fids; 
  84.     } 
  85.      
  86.     /**
  87.      * 使用feature id 作为过滤条件
  88.      * @param fs
  89.      * @return
  90.      * @throws IOException
  91.      */ 
  92.     public FeatureCollection filterFidEx( 
  93.             FeatureSource<SimpleFeatureType, SimpleFeature> fs) 
  94.             throws IOException { 
  95.         Set<FeatureId> fids = getFeatureId(fs); 
  96.         FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints()); 
  97.         Filter filt = (Filter) ff.id(fids); 
  98.         FeatureCollection col = fs.getFeatures(filt); 
  99.         return col; 
  100.     } 
  101.      
  102.     /**
  103.      * 相等,不等,超过,不超过几种情况
  104.      *
  105.      * @param fs
  106.      * @return
  107.      * @throws IOException
  108.      */ 
  109.     public FeatureCollection compareFilterEx( 
  110.             FeatureSource<SimpleFeatureType, SimpleFeature> fs) 
  111.             throws IOException { 
  112.         Filter left = ff.equals(ff.property( “NAME” ), ff.literal(
    “4路(长兴-火车站)” )); 
  113.         FeatureCollection col = fs.getFeatures(left); 
  114.         return col; 
  115.     } 
  116.      
  117.      
  118.     /**
  119.      * 过滤器对xml的支持,即把条件写在xml文件里面,这样做到可以配置
  120.      * @param fs
  121.      * @return
  122.      * @throws IOException
  123.      * @throws SAXException
  124.      * @throws ParserConfigurationException
  125.      */ 
  126.     public FeatureCollection filterXML(FeatureSource<SimpleFeatureType, SimpleFeature> fs,String fileName)
    throws IOException, SAXException, ParserConfigurationException{ 
  127.         Configuration configuration =
    new
    org.geotools.filter.v1_0.OGCConfiguration(); 
  128.         Parser parser = new Parser( configuration ); 
  129.         InputStream xml = ClassLoader.getSystemResourceAsStream(fileName); 
  130.         //parse 
  131.         Filter filter = (Filter) parser.parse( xml ); 
  132.         FeatureCollection col = fs.getFeatures(filter); 
  133.         return col; 
  134.     } 
  135.      
  136.      
  137.      
  138.     /**
  139.      * CQL (common query language)
  140.      * @param fs
  141.      * @param name
  142.      * @return
  143.      * @throws CQLException
  144.      * @throws IOException
  145.      * Filter f = CQL.toFilter(“ATTR1 < 10 AND ATTR2 < 2 OR ATTR3 > 10”);
  146.      Filter f = CQL.toFilter(“NAME = ‘New York’ “);
  147.      Filter f = CQL.toFilter(“NAME LIKE ‘New%’ “);
  148.      Filter f = CQL.toFilter(“NAME IS NULL”);
  149.      Filter f = CQL.toFilter(“DATE BEFORE 2006-11-30T01:30:00Z”);
  150.      Filter f = CQL.toFilter(“NAME DOES-NOT-EXIST”);
  151.      Filter f = CQL.toFilter(“QUANTITY BETWEEN 10 AND 20”);
  152.      Filter f = CQL.toFilter(“CROSSES(SHAPE, LINESTRING(1 2, 10 15))”);
  153.      Filter f = CQL.toFilter(“BBOX(SHAPE, 10,20,30,40)”);
  154.      Expression e = CQL.toExpression(“NAME”);
  155.      Expression e = CQL.toExpression(“QUANTITY * 2”);
  156.      Expression e = CQL.toExpression(“strConcat(NAME, ‘suffix’)”);
  157.      List filters = CQL.toFilterList(“NAME IS NULL;BBOX(SHAPE, 10,20,30,40);INCLUDE”);
  158.      */ 
  159.     public FeatureCollection filterCQL(FeatureSource<SimpleFeatureType, SimpleFeature> fs,String name)
    throws CQLException, IOException{ 
  160.         return fs.getFeatures(CQL.toFilter(“NAME like ‘%” + name +
    “%'”)); 
  161.     } 
  162.      
  163.  
  164.     public static
    void main(String[] args) throws IOException, CQLException, SAXException, ParserConfigurationException { 
  165.         FilterExam fe = new FilterExam(); 
  166.         FeatureSource<SimpleFeatureType, SimpleFeature> fs = fe.getFeatureS(“D:\\安康公交线_polyline.shp”); 
  167.         FeatureCollection col1 = fe.compareFilterEx(fs); 
  168.         System.out.println(col1.size()); 
  169.         FeatureCollection col2 = fe.filterFidEx(fs); 
  170.         System.out.println(col2.size()); 
  171.         FeatureCollection col3 = fe.filterCQL(fs,
    “13路”
    ); 
  172.         System.out.println(col3.size()); 
  173.         FeatureCollection col4 = fe.filterXML(fs,
    “filter.xml”
    ); 
  174.         System.out.println(col4.size()); 
  175.     } 
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 文件格式定义如下:

  1. <Filter
    xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” 
  2.     xmlns:ogc=“http://www.opengis.net/ogc” 
  3.     xmlns=“http://www.opengis.net/ogc” 
  4.     xsi:schemaLocation=“http://www.opengis.net/ogc filter.xsd”> 
  5.   <PropertyIsEqualTo> 
  6.     <PropertyName>NAME</PropertyName> 
  7.     <Literal>13路上行(火车站-市工商局)</Literal> 
  8.   </PropertyIsEqualTo> 
  9. </Filter> 


转载自:https://blog.csdn.net/sxausgyy/article/details/8113111

You may also like...