- /**
-
- Title: TestBufferAnalysis.java
-
- Description:
-
- Copyright: Copyright (c) 2020
- */
- 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日下午6:40:50
-
- @version 1.0
- */
- public class TestBufferAnalysis2 {
-
//WFS服务查询地址
-
private static final String GET_CAPABILITIES = "http://localhost:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetCapabilities";
-
/**
-
* 点周边查询
-
* @return
-
*/
-
public static void getBufferGeo(){
-
Map<String , String> connectionParameters = new HashMap<String , String>();
-
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", GET_CAPABILITIES);
-
FeatureCollection<SimpleFeatureType, SimpleFeature> features = null;
-
try {
-
//创建连接
-
DataStore data = DataStoreFinder.getDataStore( connectionParameters );
-
//获取所有的图层
-
String typeNames[] = data.getTypeNames();
-
//指定要查询的图层(根据自己情况修改)
-
String typeName = typeNames[12];
-
SimpleFeatureType schema = data.getSchema( typeName );
-
FeatureSource<SimpleFeatureType, SimpleFeature> source = data.getFeatureSource( typeName );
-
//执行查询
-
String geomName = schema.getGeometryDescriptor().getLocalName();
-
FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2( GeoTools.getDefaultHints());
-
GeometryFactory gf = new GeometryFactory();
-
//缓冲查询标记的点位
-
Point ls = gf.createPoint(new Coordinate(119.9436 , 31.8056));
-
//缓冲区查询( 1km 为1*0.01 )
-
DWithin filter = filterFactory.dwithin(filterFactory.property( geomName ), filterFactory.literal(ls), 0.05, "km");
-
//输出全部字段
-
Query query = new Query(typeName, filter);
-
features = source.getFeatures( query );
-
//查询结果集
-
FeatureIterator `<SimpleFeature>` featureIterator = features.features();
-
System.out.println("==========数据条数:"+features.size());
-
while( featureIterator.hasNext() ){
-
SimpleFeature feature = featureIterator.next();
-
System.out.println("==========输出结果:"+feature.getProperties());
-
}
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}finally {
-
if(features!=null) {
-
features.features().close();
-
}
-
}
-
}
-
public static void main(String[] args) throws Exception {
-
getBufferGeo();
-
}
- }
备注:
<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-->
来源:oschina
链接:https://my.oschina.net/zhxwang/blog/4301267