使用GeoTools 调用WFS服务(Java)

让人想犯罪 __ 提交于 2020-07-27 08:23:43
  1. /**
    • Title: TestBufferAnalysis.java
    • Description:
    • Copyright: Copyright (c) 2020
  2. */
  3. package com.shuidao01.test.geotools;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import org.geotools.data.DataStore;
  8. import org.geotools.data.DataStoreFinder;
  9. import org.geotools.data.FeatureSource;
  10. import org.geotools.data.Query;
  11. import org.geotools.factory.CommonFactoryFinder;
  12. import org.geotools.feature.FeatureCollection;
  13. import org.geotools.feature.FeatureIterator;
  14. import org.geotools.util.factory.GeoTools;
  15. import org.locationtech.jts.geom.Coordinate;
  16. import org.locationtech.jts.geom.GeometryFactory;
  17. import org.locationtech.jts.geom.Point;
  18. import org.opengis.feature.simple.SimpleFeature;
  19. import org.opengis.feature.simple.SimpleFeatureType;
  20. import org.opengis.filter.FilterFactory2;
  21. import org.opengis.filter.spatial.DWithin;
  22. /**
    • 空间分析测试(JKD1.8;GeoServer 2.15.2;Mavan配置,见备注。)
    • @author shuidao01
    • @date 2020年5月21日下午6:40:50
    • @version 1.0
  23. */
  24. public class TestBufferAnalysis2 {
  25.  //WFS服务查询地址
    
  26.   private static final String GET_CAPABILITIES = "http://localhost:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetCapabilities";
    
  27.   /**
    
  28.    * 点周边查询
    
  29.    * @return
    
  30.    */
    
  31.   public static void getBufferGeo(){
    
  32. 	    Map<String , String> connectionParameters = new HashMap<String , String>();
    
  33. 	    connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", GET_CAPABILITIES);
    
  34. 	    FeatureCollection<SimpleFeatureType, SimpleFeature>  features = null;
    
  35. 		try {
    
  36. 	  		//创建连接
    
  37. 	  		DataStore data = DataStoreFinder.getDataStore( connectionParameters );
    
  38. 	  		//获取所有的图层
    
  39. 	  		String typeNames[] = data.getTypeNames();
    
  40. 	  		//指定要查询的图层(根据自己情况修改)
    
  41. 	  		String typeName = typeNames[12];
    
  42. 	  		SimpleFeatureType schema = data.getSchema( typeName );
    
  43. 			FeatureSource<SimpleFeatureType, SimpleFeature> source = data.getFeatureSource( typeName );
    
  44. 			//执行查询
    
  45. 			String geomName = schema.getGeometryDescriptor().getLocalName();
    
  46. 			FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2( GeoTools.getDefaultHints());
    
  47. 			GeometryFactory gf = new GeometryFactory();
    
  48. 		    //缓冲查询标记的点位
    
  49. 			Point ls = gf.createPoint(new Coordinate(119.9436 , 31.8056));
    
  50. 			//缓冲区查询( 1km 为1*0.01 )
    
  51. 			DWithin filter = filterFactory.dwithin(filterFactory.property( geomName ), filterFactory.literal(ls), 0.05, 	"km");
    
  52. 			//输出全部字段
    
  53. 			Query query = new Query(typeName, filter);
    
  54. 			features = source.getFeatures( query );
    
  55. 			//查询结果集
    
  56. 			FeatureIterator `<SimpleFeature>` featureIterator =  features.features();
    
  57. 			System.out.println("==========数据条数:"+features.size());
    
  58. 			while( featureIterator.hasNext() ){
    
  59. 		    	SimpleFeature  feature =  featureIterator.next();
    
  60.     	        System.out.println("==========输出结果:"+feature.getProperties());
    
  61. 		    }
    
  62. 		} catch (IOException e) {
    
  63. 			// TODO Auto-generated catch block
    
  64. 			e.printStackTrace();
    
  65. 		}finally {
    
  66. 			if(features!=null) {
    
  67. 				features.features().close();
    
  68. 			}
    
  69. 		}
    
  70.   }
    
  71.   public static void main(String[] args) throws Exception {
    
  72. 	 getBufferGeo();
    
  73. }
    
  74. }

备注: <geotools.version>23-SNAPSHOT</geotools.version>

<!--geotools使用 start-->

    <dependency>
	    <groupId>org.geotools.ogc</groupId>
	    <artifactId>net.opengis.wfs</artifactId>
	    <version>${geotools.version}</version>
	</dependency>
	<dependency>
	   <groupId>org.geotools</groupId>
	   <artifactId>gt-wfs-ng</artifactId>
	   <version>${geotools.version}</version>
	 </dependency>
    <dependency>
	   <groupId>org.geotools.xsd</groupId>
	   <artifactId>gt-xsd-wfs</artifactId>
	   <version>${geotools.version}</version>
	</dependency>
	<dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-epsg-hsql</artifactId>
        <version>${geotools.version}</version>             
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-referencing</artifactId>
        <version>${geotools.version}</version>
    </dependency>    
    <!--geotools使用  end-->
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!