使用Python隐藏40万纬度经度到邮政编码
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
警告:这里不是 GIS 专家!
使用
geopandas
和来源似乎很容易邮政编码多边形(当然,请注意,邮政编码是 实际上不是多边形):例如,如果我有一个点数据源,文件中包含
(lat, lon)
对points.geojson,我可以这样做:
sjoin
的默认行为是执行intersects
查询,这正是我们想要的。这给了我一个地理数据帧,它将坐标(在
.geometry
属性中)映射到邮政编码(在.ZIP_CODE
属性中)。我使用此来源来获取邮政编码数据。例如,给定一个点:
我现在知道它的邮政编码:
我使用大约 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 filepoints.geojson
, I could do something like this:The default behavior of
sjoin
is to perform anintersects
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:
I now know its zip code:
I tested this using a data source with about 4000 points; I don't have handy anything approaching your 400000 point data source.