在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询
?
我定义了两个方法,一起来看一下:
/**
* geodistance filter
* 一个过滤器来过滤基于一个特定的距离从一个特定的地理位置/点。
* @author chenjie
* @param x
* @param y
* @param distance
* @return
*/
protected static FilterBuilder geoDistanceFilter(Double x,Double y,Double distance) {
return FilterBuilders.geoDistanceFilter("the_geom")
.point(x, y)
.distance(distance, DistanceUnit.METERS)
.optimizeBbox("memory") // Can be also "indexed" or "none"
.geoDistance(GeoDistance.ARC); // Or GeoDistance.PLANE
}
/**
* geo bounding box filter
* 定义一个过滤器来过滤基于边界框左上角和右下角的位置/分
* @author chenjie
* @return FilterBuilder
*/
protected static FilterBuilder geoBoundingBoxFilter(Double topleft_x,Double topleft_y,Double bottomRight_x,Double bottomRight_y) {
return FilterBuilders.geoBoundingBoxFilter("the_geom")
.topLeft(topleft_x, topleft_y)
.bottomRight(bottomRight_x, bottomRight_y);
}
?
我们可以看的到这两个方法分是做距离查询(可以用于周边查询,注意方法中DistanceUnit.METERS指的是计算单位)和距形查询
?
我们再看看elasticsearch是如何查询数据的,我封装了一个方法
? ?
public List
?
各位一定要注意,所查询字段必须是geo_point类型,否则是无法进行空间查询的,本人开始也犯了这个错误。
?
如图:我查询的是the_geom 字段,我们可以看到是geo_point 。elasticsearch对GIS数据的支持还是蛮强大的。