GeoDjango距离搜索

发布于 2024-08-15 17:09:41 字数 859 浏览 7 评论 0原文

我想使用 GeoDjango 进行基本的位置搜索。具体来说,我想给搜索功能一个邮政编码/城市/县,并查找 5 英里、10 英里、20 英里等范围内的所有邮政编码/城市/县。我在文档中找到了以下段落:

使用地理坐标系可能会给开发人员带来以后的复杂性。例如,PostGIS 不具备使用地理坐标系执行非点几何形状之间的距离计算的能力,例如构建查询来查找存储为 WGS84 的县边界 5 英里内的所有点。 [6]

如果我想使用 PostGIS 并能够在美国各地进行上述搜索,这到底意味着什么?文档建议使用投影坐标系仅覆盖特定区域。我需要覆盖整个国家,所以我认为这不是一个选择。

基本上最后我希望能够在给定起始位置和距离的情况下找到邻近的邮政编码/城市/县。我真的不关心这在技术层面上是如何完成的。

另外,我在哪里可以找到包含美国邮政编码/城市/县的地理边界的数据库,我可以将其导入到 GeoDjango 模型中?

更新

我找到了一个数据库,其中包含美国所有邮政编码的纬度和经度坐标在这里。我的计划是将这些点导入到 GeoDjango 模型中,并使用 PostGis 构建查询,以找到距给定点 x 英里范围内的其他点。这解决了文档中提出的问题,因为所有邮政编码都被视为点而不是多边形。这对于我的用例来说很好,因为我不关心完美的准确性。

好处:数据文件是免费的

坏处:这个数据来自 2000 年的人口普查,所以它相当过时 有点

希望:美国人口普查局每 10 年进行一次人口普查,而且已经快到 2010 年了

结论:它足够好我

I want to use GeoDjango to do basic location searches. Specifically I want to give the search function a ZIP code/city/county and find all the ZIP codes/cities/counties within 5mi, 10mi, 20mi, etc. I found the following paragraph in the documentation:

Using a geographic coordinate system may introduce complications for the developer later on. For example, PostGIS does not have the capability to perform distance calculations between non-point geometries using geographic coordinate systems, e.g., constructing a query to find all points within 5 miles of a county boundary stored as WGS84. [6]

What does this exactly mean if I want to use PostGIS and to be able to do the searches described above across the USA? The docs suggest using a projected coordinate system to cover only a specific region. I need to cover the whole country so this I suppose is not an option.

Basically in the end I want to be able to find neighbouring ZIP codes/cities/counties given a starting location and distance. I don't really care how this is done on a technical level.

Also where would I find a database that contains the geographic boundaries of ZIP codes/cities/counties in the USA that I can import into a GeoDjango model?

UPDATE

I found a database of that contains the latitude and longitude coordinates of all ZIP codes in the USA here. My plan is to import these points into a GeoDjango model and use PostGis to construct queries that can find other points within x miles from a given point. This gets around the issue raised in the documentation because all the ZIP codes are treated as points instead of as polygons. This is fine for my use case because perfect accuracy is not something I care about.

The good: the data file is free

The bad: this data is from the 2000 census so it is quite dated

The somewhat hopeful: the United States Census Bureau conducts a census every 10 years and it is almost 2010

The conclusion: it's good enough for me

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

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

发布评论

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

评论(2

歌入人心 2024-08-22 17:09:41

为了解决报价中的限制,您只需取用户提供的邮政编码区域的质心,然后从该点查找与从该点发出的 5、10 或任何英里圆圈相交的所有邮政编码区域。我不确定在 geodjango 中如何实现这一点,但使用 postgis 绝对是可能的。

您引用的限制基本上是说您无法编写一个查询“给我俄亥俄州边界内部 5 英里范围内的所有点”。

To get around the limitation in the quote, you can just take the centroid of the zipcode region provided by the user, and then from that point find all zipcode regions that intersect a 5, 10 or whatever mile circle emanating from that point. I'm not sure how that would be achieved in geodjango, but with postgis it's definitely possible.

The limitation you quoted basically says you can't write a query that says "give me all points that are within 5 miles on the inside of the border of Ohio."

给不了的爱 2024-08-22 17:09:41
In [1]: o = Place.objects.get(pk=2463583)  # Oakland, CA

In [2]: sf = Place.objects.get(pk=2487956)  # San Francisco, CA

In [3]: o.coords.transform(3410)  # use the NSIDC EASE-Grid Global projection

In [4]: sf.coords.transform(3410)  # use the NSIDC EASE-Grid Global projection

In [5]: o.coords.distance(sf.coords)  # find the distance between Oakland and San Francisco (in meters)
Out[5]: 14401.942808571299
In [1]: o = Place.objects.get(pk=2463583)  # Oakland, CA

In [2]: sf = Place.objects.get(pk=2487956)  # San Francisco, CA

In [3]: o.coords.transform(3410)  # use the NSIDC EASE-Grid Global projection

In [4]: sf.coords.transform(3410)  # use the NSIDC EASE-Grid Global projection

In [5]: o.coords.distance(sf.coords)  # find the distance between Oakland and San Francisco (in meters)
Out[5]: 14401.942808571299
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文