房屋之间的距离,Google Directions API 查询限制太低,需要更好的算法

发布于 2024-10-31 13:22:41 字数 485 浏览 6 评论 0原文

我需要租两套房子。我希望他们尽可能接近。约有300间房屋可供出租。我希望使用 Google 地图方向 API 来计算任意两座可用房屋之间的步行距离,这样我就可以对列表进行排序并选择两座距离较近的房屋。

一切都运行良好,只是 Google 设置了每天 2,500 次查询的理论限制(而实际上该限制要低得多,每天仅为 250 次)。我有 3002/2 - 300 = 44,700 个查询要做,显然这个限制对我来说是不够的。

这将是一次性的事情,关于如何使用 Google Maps API 完成我所需要的任何提示?我可以以某种方式运行分布式程序,这样限制只会影响一个实例吗? Google App Engine 有帮助吗?

我也欢迎提出改进算法的建议。如果两栋房子相距较远,而另一栋房子靠近其中一栋,则意味着不必将第三栋房子与剩余的房子进行检查,因为它们可能相距很远。我也更关心算法的定性本质,而不是确切的距离,所以也许我可以做一个简单的近似,这将导致更少的查询。

谢谢,

I need to rent two houses. I want them to be as close as possible. There are about 300 houses available for rent. I wish to use the Google Maps Directions API to calculate the walking distance between any two available houses so then I can sort the list and choose two that are close.

Everything works great, except that Google sets a theoretical limit of 2,500 queries per day (and in practice the limit is much lower, at only 250 per day). I have 3002/2 - 300 = 44,700 queries to make so obviously this limit is not enough for me.

This would be a one time thing, any hints on how can I accomplish what I need with the Google Maps API? Can I somehow run the program distributed so the limit will affect only one instance? Would Google App Engine help?

I also welcome advice for improving the algorithm. If two houses are far apart, and another house is close to one of them it means it would not have to check the third house with the remaining house as they are probably far away. I also care more about the qualitative nature of the algorithm, not the exact distances, so maybe there is a simple approximation I can make that will result in fewer queries.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隐诗 2024-11-07 13:22:41

任何两栋房子之间的地理距离(直线距离)将成为步行距离的严格下限。因此,我将从 300 个查询开始,为了获取每个房屋的经度/纬度,将它们代入半正弦公式(例如)以获取 45,000 个无序对之间的距离,并对它们进行排序以按地理距离获取最近的对。然后与
手头有一些可能的候选人,您可以开始通过另一组对 Google API 的调用来检查步行距离。

The geographic distance between any two houses, as the crow flies, will be a strict lower bound on the walking distance. So I'd start with 300 queries, to get the long/lat for each house, plug them into the Haversine formula (for example) to get the distances between the 45,000 unordered pairs, and sort them to get the closest pairs by geographic distance. Then with
some likely candidates in hand, you can start checking the walking distances with another set of calls to the Google API.

空袭的梦i 2024-11-07 13:22:41

假设您是披萨送货员,并且想要计算您的有效范围(您可以在 30 分钟内到达的范围)。您想要制作该时间数据的 N 到 E 部分的彩色条形 3D 图表,例如(带有虚假数据):

在此处输入图像描述

并且您想要包括大约 100k 栋房屋...至少我听说,像这样的程序是在 Google 地图引入限制之前制定的。在这种情况下,限制就很严重。

如果您有所有房屋的地理位置,那么当您像鸟一样飞行时,您可以根据地球上的点有多远来预测。在此基础上对它们进行排序,并找到最佳预测的结果。

编辑:添加了 Java 代码示例,这在创建预测时可能很有用:

/**
 * Thaddeus Vincenty's inverse method formulae implementation for
 * geographical distance between two given points on earth.
 * @param L1
 *        geographical latitude of standpoint in decimal degrees
 * @param G1
 *        geographical longitude of standpoint in decimal degrees
 * @param L2
 *        geographical latitude of destination in decimal degrees
 * @param G2
 *        geographical longitude of destination in decimal degrees
 * @return Geographical distance in kilometeres
 */
public static double getDistance(final double L1, final double G1,
        final double L2, final double G2) {
    double delta, p0, p1, p2, p3;
    // The average radius for a spherical approximation of Earth
    double rEarth = 6371.01d;

    delta = G1 - G2;
    p0 = Math.cos(L2) * Math.cos(delta);
    p1 = Math.cos(L2) * Math.sin(delta);
    p2 = Math.cos(L1) * Math.sin(L2) - Math.sin(L1) * p0;
    p3 = Math.sin(L1) * Math.sin(L2) + Math.cos(L1) * p0;

    return rEarth * Math.atan2(Math.sqrt(p1 * p1 + p2 * p2), p3);
}

/**
 * Rounds double to nr number of decimal places
 * @param d
 *        floating-point number
 * @param nr
 *        decimal places to keep
 * @return rounded number with nr decimal places
 */
public static double round(double d, int nr) {
    return new java.math.BigDecimal(Double.toString(d)).setScale(nr,
        java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
}

public static void main(String[] args) {
    double L1 = Math.toRadians(Double.parseDouble(args[0]));
    double G1 = Math.toRadians(Double.parseDouble(args[1]));
    double L2 = Math.toRadians(Double.parseDouble(args[2]));
    double G2 = Math.toRadians(Double.parseDouble(args[3]));

    System.out.println(round(getDistance(L1, G1, L2, G2), 2));
}

Consider, that you are pizza deliverer and you want to calculate your effective range (where you can go within 30 minutes). And you want to make a colored bar 3d graph of N to E section of that time data, something like (with bogus data):

enter image description here

And you want to include like 100k of houses ... Well at least I heard, that program like this, was made before limits where introduced in Google map. Limits just bite hard in this case.

If you have geo location from all the houses, then you can find a prediction from how far are the points on earth, when you fly like a bird. Sort them based on that, and find results for best predictions.

Edit: Added Java code example, that could be useful when creating predictions:

/**
 * Thaddeus Vincenty's inverse method formulae implementation for
 * geographical distance between two given points on earth.
 * @param L1
 *        geographical latitude of standpoint in decimal degrees
 * @param G1
 *        geographical longitude of standpoint in decimal degrees
 * @param L2
 *        geographical latitude of destination in decimal degrees
 * @param G2
 *        geographical longitude of destination in decimal degrees
 * @return Geographical distance in kilometeres
 */
public static double getDistance(final double L1, final double G1,
        final double L2, final double G2) {
    double delta, p0, p1, p2, p3;
    // The average radius for a spherical approximation of Earth
    double rEarth = 6371.01d;

    delta = G1 - G2;
    p0 = Math.cos(L2) * Math.cos(delta);
    p1 = Math.cos(L2) * Math.sin(delta);
    p2 = Math.cos(L1) * Math.sin(L2) - Math.sin(L1) * p0;
    p3 = Math.sin(L1) * Math.sin(L2) + Math.cos(L1) * p0;

    return rEarth * Math.atan2(Math.sqrt(p1 * p1 + p2 * p2), p3);
}

/**
 * Rounds double to nr number of decimal places
 * @param d
 *        floating-point number
 * @param nr
 *        decimal places to keep
 * @return rounded number with nr decimal places
 */
public static double round(double d, int nr) {
    return new java.math.BigDecimal(Double.toString(d)).setScale(nr,
        java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
}

public static void main(String[] args) {
    double L1 = Math.toRadians(Double.parseDouble(args[0]));
    double G1 = Math.toRadians(Double.parseDouble(args[1]));
    double L2 = Math.toRadians(Double.parseDouble(args[2]));
    double G2 = Math.toRadians(Double.parseDouble(args[3]));

    System.out.println(round(getDistance(L1, G1, L2, G2), 2));
}
极度宠爱 2024-11-07 13:22:41

我会使用这个公式:

distance = Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
           Math.cos(lat1)*Math.cos(lat2) *
           Math.cos(lon2-lon1)) * 6371;

按照直线距离对所有 45,000 栋房屋进行排名。
然后,我将按照最短距离排名前 250 个结果,并通过 google 运行它们,以获得准确的步行距离并重新排名。

i would use this formula :

distance = Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
           Math.cos(lat1)*Math.cos(lat2) *
           Math.cos(lon2-lon1)) * 6371;

to rank all 45,000 houses by distance as the crow flies.
I would then take the top 250 results ranked by shortest distance and run them through google to get the accurate walking distance and re-rank.

踏月而来 2024-11-07 13:22:41

我不知道这是否适用于方向方法,但您希望将方向查询交错(嵌套)到单个查询中。一个 google 块最多可以容纳 24 个方向。因此,您可以将查询增加到每天最多 250 * 24(6000 个方向)。也许您想在 6000 次查询后更改您的 IP 地址?也许 Google 让您每天查询超过 6000 个路线?我从 geweb tsp 求解器中得到了交错的想法,他将查询矩阵中的 24 个城市交错成 1 个块,最多可节省 22 个单个查询,从而减少带宽和 Google 的 API 限制。

I don't know if this is working with the directions method but you want to interleave (nesting) the directions query into a single query. A google chunk can hold up to 24 directions per chunk. Thus you can increase your query to a maximum of 250 * 24 (6000 directions) per day. Maybe you want to change your IP adress after 6000 queries? Maybe Google let you then query more then 6000 directions per day? I got the interleaving idea from geweb tsp solver where he interleaves 24 cities from a query matrix into 1 chunk saving up to 22 single queries and thus reducing bandwidth and Google's API limitation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文