【问题标题】:How to find the closest point in the DB using objectify-appengine如何使用 objectify-appengine 在数据库中找到最近的点
【发布时间】:2012-12-02 17:19:44
【问题描述】:

我在我的应用中使用 objectify-appengine。在数据库中,我存储地方的纬度和经度。 在某些时候,我想找到离特定点最近的地方(从数据库)。
据我了解,我无法执行常规的类似 SQL 的查询。 所以我的问题是如何以最好的方式完成?

【问题讨论】:

    标签: google-app-engine persistence objectify


    【解决方案1】:

    您应该查看GeoModel,它可以使用 Google App Engine 进行地理空间查询。

    更新:

    假设您在 Objectify 带注释的模型类中有一个名为 coordinates 的 GeoPt 属性。

    您的项目中需要有两个库:

    1. GeoLocation.java
    2. Java GeoModel

    在您要执行地理查询的代码中,您有以下内容:

    import com.beoui.geocell.GeocellManager;
    import com.beoui.geocell.model.BoundingBox;
    import you.package.path.GeoLocation;
    // other imports
    
    // in your method
    GeoLocation specificPointLocation = GeoLocation.fromDegrees(specificPoint.latitude, specificPoint.longitude);
    GeoLocation[] bc = specificPointLocation.boundingCoordinates(radius);
    
    // Transform this to a bounding box
    BoundingBox bb = new BoundingBox((float) bc[0].getLatitudeInDegrees(),
                     (float) bc[1].getLongitudeInDegrees(),
                     (float) bc[1].getLatitudeInDegrees(),
                     (float) bc[0].getLongitudeInDegrees());
    
    // Calculate the geocells list to be used in the queries (optimize
    // list of cells that complete the given bounding box)
    List<String> cells = GeocellManager.bestBboxSearchCells(bb, null);
    
    // calculate geocells of your model class instance
    List <String> modelCells = GeocellManager.generateGeoCell(myInstance.getCoordinate);
    
    // matching
    for (String c : cells) {
        if (modelCells.contains(c)) {
            // success, do sth with it
            break;
        }
    }
    

    【讨论】:

    • 我的问题是如何使用 objectify 来实现这一点,在 GeoModel 中,他们使用某种其他持久性代理,使他们能够执行 GQL 查询,这是我做不到的。
    • 请查看我的更新答案。它提供了有关使用哪些库的指南以及可用作在 Objectify 注释模型中进行边界框查询的基础的代码 sn-p。
    • @RCB:您应该使用本文中的概念并将它们应用到您的项目中。还有这个项目的 java 版本,并且有一个解决方法可以使它与 objectify 一起工作:brunofuster.wordpress.com/category/geomodel
    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 2013-01-05
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多