使用GeoTools调用WFS服务(Java)

编程

/**

  • <p>Title: TestBufferAnalysis2.java</p>

  • <p>Description: </p>

  • <p>Copyright: Copyright (c) 2020</p>

*/

  • package com.shuidao01.test.geotools;

  • import java.io.IOException;

  • import java.util.HashMap;

  • import java.util.Map;

  • import org.geotools.data.DataStore;

  • import org.geotools.data.DataStoreFinder;

  • import org.geotools.data.FeatureSource;

  • import org.geotools.data.Query;

  • import org.geotools.factory.CommonFactoryFinder;

  • import org.geotools.feature.FeatureCollection;

  • import org.geotools.feature.FeatureIterator;

  • import org.geotools.util.factory.GeoTools;

  • import org.locationtech.jts.geom.Coordinate;

  • import org.locationtech.jts.geom.GeometryFactory;

  • import org.locationtech.jts.geom.Point;

  • import org.opengis.feature.simple.SimpleFeature;

  • import org.opengis.feature.simple.SimpleFeatureType;

  • import org.opengis.filter.FilterFactory2;

  • import org.opengis.filter.spatial.DWithin;

  • /**

    • 空间分析测试(JKD1.8;GeoServer 2.15.2;Mavan配置,见备注。)

    • @author shuidao01

    • @date 2020年5月21日下午4:30:50

    • @version 1.0

  • */

  • public class TestBufferAnalysis2 {

  • /**

  •  * WFS服务查询地址

  •  */

  • private static final String GET_CAPABILITIES = "http://localhost:8081/geoserver/wfs?service=WFS&version=1.0.0&request=GetCapabilities";

  • /**

  •  * 点周边查询

  •  * [@param](https://my.oschina.net/u/2303379) bufferVO

  •  * [@return](https://my.oschina.net/u/556800)

  •  */

  1.   public static void getBufferGeo(){

  2.  	    Map<String , String> connectionParameters = new HashMap<String , String>();

  3.  	    connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", GET_CAPABILITIES);

  4.  	    FeatureCollection<SimpleFeatureType, SimpleFeature>  features = null;

  5.  	try {

  6.    		//创建连接

  7.    		DataStore data = DataStoreFinder.getDataStore( connectionParameters );

  8.    		//获取所有的图层

  9.    		String typeNames[] = data.getTypeNames();

  10. 	  		//指定要查询的图层(根据自己情况修改)

  11. 	  		String typeName = typeNames[12];

  12. 	  		SimpleFeatureType schema = data.getSchema( typeName );

  13. 			FeatureSource<SimpleFeatureType, SimpleFeature> source = data.getFeatureSource( typeName );

  14. 			//执行查询

  15. 			String geomName = schema.getGeometryDescriptor().getLocalName();

  16. 			FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2( GeoTools.getDefaultHints());

  17. 			GeometryFactory gf = new GeometryFactory();

  18. 		    //缓冲查询标记的点位

  19. 			Point ls = gf.createPoint(new Coordinate(119.9436 , 31.8056));

  20. 			//缓冲区查询( 1km 为1*0.01 )

  21. 			DWithin filter = filterFactory.dwithin(filterFactory.property( geomName ), filterFactory.literal(ls), 0.05, 	"km");

  22. 			//输出全部字段

  23. 			Query query = new Query(typeName, filter);

  24. 			features = source.getFeatures( query );

  25. 			//查询结果集

  26. 			FeatureIterator <SimpleFeature> featureIterator =  features.features();

  27. 			System.out.println("==========数据条数:"+features.size());

  28. 			while( featureIterator.hasNext() ){

  29. 		    	SimpleFeature  feature =  featureIterator.next();

  30.     	        System.out.println("==========输出结果:"+feature.getProperties());

  31. 		    }

  32. 		} catch (IOException e) {

  33. 			// TODO Auto-generated catch block

  34. 			e.printStackTrace();

  35. 		}finally {

  36. 			if(features!=null) {

  37. 				features.features().close();

  38. 			}

  39. 		}

  40.   }

  41.   public static void main(String[] args) throws Exception {

  42. 	 getBufferGeo();

  43. }

  44. }

备注:

<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-->

以上是 使用GeoTools调用WFS服务(Java) 的全部内容, 来源链接: utcz.com/z/517136.html

回到顶部