使用Python隐藏40万纬度经度到邮政编码

发布于 2025-01-10 08:50:38 字数 742 浏览 1 评论 0原文

我有 400,000 个带有纬度和经度的案例。我想将这些转换为邮政编码。下面的代码有效...

import geopy

from geopy.geocoders import Nominatim

geolocator = geopy.Nominatim(user_agent='my-application')

def get_zipcode(df, geolocator, lat_field, lon_field):
    location = geolocator.reverse((df[lat_field], df[lon_field]))
    if 'address' in location.raw.keys():
      if 'postcode' in location.raw['address'].keys():
        return location.raw['address']['postcode']
    else:
      None

但仅适用于较小的批次,而且需要一段时间,例如 2,000 个案例需要 15 分钟。

dfbatch1['pickup_zip'] = dfbatch1.apply(get_zipcode, axis=1, geolocator=geolocator, lat_field='pickup_latitude', lon_field='pickup_longitude')

转换我所有纬度和纬度的最佳方法是什么?经度到邮政编码?

谢谢!

I have 400,000 cases with latitudes and longitudes. I want to convert these to zip codes. The code below works...

import geopy

from geopy.geocoders import Nominatim

geolocator = geopy.Nominatim(user_agent='my-application')

def get_zipcode(df, geolocator, lat_field, lon_field):
    location = geolocator.reverse((df[lat_field], df[lon_field]))
    if 'address' in location.raw.keys():
      if 'postcode' in location.raw['address'].keys():
        return location.raw['address']['postcode']
    else:
      None

But only on smaller batches, but it takes a while, like 15 minutes for 2,000 cases.

dfbatch1['pickup_zip'] = dfbatch1.apply(get_zipcode, axis=1, geolocator=geolocator, lat_field='pickup_latitude', lon_field='pickup_longitude')

What would be the best way to convert all of my latitudes & longitudes to zip codes?

Thanks!

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

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

发布评论

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

评论(1

生生漫 2025-01-17 08:50:39

警告:这里不是 GIS 专家!

使用 geopandas 和来源似乎很容易邮政编码多边形(当然,请注意,邮政编码是 实际上不是多边形):

例如,如果我有一个点数据源,文件中包含 (lat, lon)points.geojson,我可以这样做:

import geopandas

points = geopandas.read_file('points.geojson')
zipcodes = geopandas.read_file("zip_poly.gdb")
zip_points = points.sjoin(zipcodes, how='left', )

sjoin 的默认行为是执行 intersects 查询,这正是我们想要的。

这给了我一个地理数据帧,它将坐标(在 .geometry 属性中)映射到邮政编码(在 .ZIP_CODE 属性中)。我使用此来源来获取邮政编码数据。

例如,给定一个点:

>>> points.query('NAME == "Boston"').geometry
1436    POINT (-71.05671 42.35959)
Name: geometry, dtype: geometry

我现在知道它的邮政编码:

>>> zip_points.query('NAME=="Boston"').ZIP_CODE
1436    02109
Name: ZIP_CODE, dtype: object

我使用大约 4000 个点的数据源对此进行了测试;我没有方便的任何接近你的 400000 点数据源的东西。

Warning: not a GIS expert here!

It seems like this would be pretty easy using geopandas and a source of zip code polygons (noting, of course, that zip codes are not, in fact, polygons):

For example, if I have a point data source with (lat, lon) pairs in a file points.geojson, I could do something like this:

import geopandas

points = geopandas.read_file('points.geojson')
zipcodes = geopandas.read_file("zip_poly.gdb")
zip_points = points.sjoin(zipcodes, how='left', )

The default behavior of sjoin is to perform an intersects query, which is what we want.

That gives me a geodataframe that maps coordinates (in the .geometry attribute) to zip codes (in the .ZIP_CODE attribute). I used this source for zip code data.

For example, given a point:

>>> points.query('NAME == "Boston"').geometry
1436    POINT (-71.05671 42.35959)
Name: geometry, dtype: geometry

I now know its zip code:

>>> zip_points.query('NAME=="Boston"').ZIP_CODE
1436    02109
Name: ZIP_CODE, dtype: object

I tested this using a data source with about 4000 points; I don't have handy anything approaching your 400000 point data source.

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