【问题标题】:Find points within a circle using hibernate Spatial criteria - distanceWithin使用休眠空间标准查找圆内的点 - distanceWithin
【发布时间】:2018-01-19 14:33:07
【问题描述】:

我有一个域对象car。汽车属性之一是location 作为空间类型Point 存储在MySQL 中(columnDefinition 不能属于Geometry,会引发异常)。

@Type(type="org.hibernate.spatial.GeometryType")
@Column(name = "location", columnDefinition="Point")
private Point location;

使用休眠空间标准,我想获得一定半径内的空间标准。在本机 sql 中,我可以使用 ST_Distance_Sphere,但我想改用标准。问题是,这给了我一个错误org.hibernate.HibernateException: dwithin function not supported by this dialect:

final Point circleCenterPoint = 
new GeometryFactory().createPoint(new Coordinate(latitude,longitude));

(...).add(SpatialRestrictions.distanceWithin(Car.LOCATION, circleCenterPoint, radiusInKm * 0.009));

我正在使用:jts-1.13hibernate-core-4.3.4hibernate-spatial-4.3(根据that,它们应该匹配)以及org.hibernate.spatial.dialect.mysql.MySQLSpatial56Dialect

【问题讨论】:

    标签: mysql hibernate spatial spatial-query hibernate-spatial


    【解决方案1】:

    问题只是在 Hibernate Spatial 4.x 版(或 5.x 版)中不支持 ST_Distance_Sphere 函数。

    我创建了一张票 (https://hibernate.atlassian.net/browse/HHH-12229) 来更新 MySQL Spatial 方言并添加对这些新功能的支持。但是,这些更改不会向后移植到 4.x 版。

    【讨论】:

    • 对,但实际上我可以使用不同的方法。使用within 函数并作为多边形传递一个圆(线性环)。使用 Ian Turton 先生的方法 stackoverflow.com/questions/44249945/… 但实际上他的 bufferPoint 方法给了我一个奇怪的圆圈(留下评论),你认为有没有更简单的方法来生成一个环(多边形)以便使用 within功能 ?我可以使用GeometricShapeFactory,但不确定setSize()(半径)单位(以及在计算过程中是否将地球视为球体)
    【解决方案2】:

    作为“此方言不支持的 dwithin 函数”的解决方法,我添加了 dwithin 函数,如下所示。

    CREATE FUNCTION `dwithin`(`pt1` POINT, `pt2` POINT, `radius` FLOAT) RETURNS tinyint(1)
     DETERMINISTIC
    BEGIN
     DECLARE dist FLOAT;
     SELECT ST_Distance_Sphere(pt1, pt2) INTO dist;
     RETURN dist<radius;
    END
    

    【讨论】:

      猜你喜欢
      • 2013-04-26
      • 2021-10-19
      • 2014-06-21
      • 2014-04-05
      • 2011-10-09
      • 2012-12-24
      • 1970-01-01
      相关资源
      最近更新 更多