存储 3 个最近的坐标

发布于 2024-11-03 11:43:04 字数 402 浏览 0 评论 0原文

我有一个 XML 文件,其中包含许多点及其经度和纬度。

目前,我的 python 代码只需循环遍历 XML 文件,找到最近的点(以英里为单位),然后将其与之前的最近点进行比较,即可获得最近的点。如果它更接近,那么我将这个新点的值分配给变量。所以这方面一切都在进行。

现在,我想做的实际上是存储最近的 2 或 3 个点。 我该怎么做呢? XML 文件不是按最接近的顺序排序的,此外,每次发出请求时,用户位置都会发生变化。我可以使用 XML 文件来执行此操作吗?还是我可能需要考虑将数据存储在 SQL Server 或 MySQL 中?

感谢您的帮助。 PS,如果有人感兴趣,可以在此处获取示例代码。这是大学项目的一部分。

I have an XML file that contains a number of points with their longitude and latitude.

My python code at the moment gets the nearest point by simply looping through the XML file, finding the nearest, in miles or whatever, then comparing it with the previous closest point. If its nearer then I assign the variable the value of this new point. So everything is working in that regard.

Now, what I want to do is actually store the closest 2 or 3 points.
How do I go about doing this? The XML file isn't ordered by closest, and besides, the users location will change each time a request is made. Can I do this with an XML file or will I perhaps have to look into storing the data is SQL Server or MySQL?

Thanks for the help.
PS, the sample code is available here if anyone is interested. This is part of a college project.

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

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

发布评论

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

评论(2

你另情深 2024-11-10 11:43:04

在解析 xml 文件时,您应该将所有点对及其距离存储在元组列表中(例如)。

mypoints = [(distance12, x1, x2),...,(distancenm, xn, xm)]
mypoints.sort()
three_closer = mypoints[:3]

将此调整为您的代码:

..............
mypoints = []
for row in rows:
     # Get coords for current record
     curr_coords = row.getAttribute("lat") + ',' + row.getAttribute("lng")
     # Get distance
     tempDistance = distance.distance(user_coords, curr_coords).miles
     mypoints.append((tempDistance, row))

mypoints.sort()
#the three closest points:
mythree_shorter = mypoints[0:3]
for distance, row in mythree_shorter:
    shortestStation = json.dumps(
                            {'number': row.getAttribute("number"),
                             'address': row.getAttribute("address"),
                             'lat': row.getAttribute("lat"),
                             'lng': row.getAttribute("lng"),
                             'open': row.getAttribute("open")},
                             sort_keys=True,
                             indent=4)
    save_in_some_way(shortestStation)   #maybe writing to a file?
..................

You should store in a list of tuples (for example) all the point pairs and their distances as you parse de xml file.

mypoints = [(distance12, x1, x2),...,(distancenm, xn, xm)]
mypoints.sort()
three_closer = mypoints[:3]

Adapting this to your code:

..............
mypoints = []
for row in rows:
     # Get coords for current record
     curr_coords = row.getAttribute("lat") + ',' + row.getAttribute("lng")
     # Get distance
     tempDistance = distance.distance(user_coords, curr_coords).miles
     mypoints.append((tempDistance, row))

mypoints.sort()
#the three closest points:
mythree_shorter = mypoints[0:3]
for distance, row in mythree_shorter:
    shortestStation = json.dumps(
                            {'number': row.getAttribute("number"),
                             'address': row.getAttribute("address"),
                             'lat': row.getAttribute("lat"),
                             'lng': row.getAttribute("lng"),
                             'open': row.getAttribute("open")},
                             sort_keys=True,
                             indent=4)
    save_in_some_way(shortestStation)   #maybe writing to a file?
..................
二货你真萌 2024-11-10 11:43:04

这是一个适用于任意数量点的解决方案:

closest = points[:NUM_CLOSEST]
closest.sort()
for point in points[NUM_CLOSEST:]:
    if point.distance < closest[-1].distance:
        closest[-1] = point
        closest.sort()

显然,有点伪代码。 sort() 调用可能需要一个参数,以便以有用的方式对它们进行排序,并且您可能需要一个函数来计算距离来替换 distance 成员。

Here's a solution that will work for any number of points:

closest = points[:NUM_CLOSEST]
closest.sort()
for point in points[NUM_CLOSEST:]:
    if point.distance < closest[-1].distance:
        closest[-1] = point
        closest.sort()

Obviously, a bit pseudo-cody. The sort() calls will probably need an argument so they are sorted in a useful way, and you'll probably want a function to calculate the distance to replace the distance member.

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