【问题标题】:Get distance and Domain attribute in same query with Hibernate and PostGIS使用 Hibernate 和 PostGIS 在同一查询中获取距离和域属性
【发布时间】:2014-03-01 00:35:19
【问题描述】:

我正在使用 Hibernate、Postgre、PostGIS 和 Grails。

要搜索给定半径内的所有用户,我正在使用 HibernnateSpetial 并且一切正常。

def criteria = sessionFactory.currentSession.createCriteria(User)
criteria.createAlias('location', 'location')
criteria.add(SpatialRestrictions.within("location.point", boundingPolygon))
criteria.list()

现在,我还想要在同一条件执行中所有用户与给定点的距离。

def criteria = sessionFactory.currentSession.createCriteria(User)
criteria.createAlias('location', 'location')
criteria.add(SpatialRestrictions.within("location.point", boundingPolygon))
String[] columnAlias = new String[1]
columnAlias[0] = "distance"
org.hibernate.type.Type[] types = new StandardBasicTypes[1]
types[0] = StandardBasicTypes.DOUBLE
criteria.setProjection(Projections.sqlProjection("ST_Distance(point, ST_GeomFromText('POINT(-84.3346473 33.9462125)', 4326)) as distance", columnAlias, types))
criteria.list()

它返回存储在数据库中的几何(点)与 SQL 中提供的点之间的距离,但忽略用户域的属性。我得到了这样的东西 - [0.0, 0.20430178027717, 0.0] 在标准执行后。

我想知道是否可以从给定的 lat/lng 中获取所有用户 + 每个用户的距离。我可以使用 Groovy/Java 代码来计算结果的距离,但我需要标准执行的排序结果,以便我也可以应用分页。

【问题讨论】:

  • 嗨 manish,我无法回答您的问题,但我正在尝试将 postGIS 与 grails 一起使用,但没有成功。您能否指出显示如何做到这一点的最新资源?

标签: hibernate grails postgis hibernate-spatial


【解决方案1】:

如果您希望返回距离,直接在您的 groovy 代码中计算似乎更容易,例如:

giveMeAllHitsFromGIS().each{
  it.distance = it.point1.distanceTo( it.point2 )
}

你的代码已经被低级休眠/空间的东西有点过载了,如果你增加空间查询的复杂性,它的可读性就会降低

【讨论】:

  • 实际上我需要给定半径内的域(用户)及其与给定中心(点)的距离。我可以使用 Groovy/Java 方法来计算结果的距离,但我需要排序的结果,以便我也可以应用分页。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 2019-04-09
  • 1970-01-01
  • 2022-01-22
  • 2015-12-29
  • 1970-01-01
相关资源
最近更新 更多