How to use geopy to obtain the zip code from coordinates?

我的梦境 提交于 2021-02-08 09:47:31

问题


So below is the code I have been using. I'm a bit of a newb. I've been testing with just the head of the data because of the quota for using the API. Below is a snapshot of the dataframe:

        latitude    longitude
0   -73.99107       40.730054
1   -74.000193      40.718803
2   -73.983849      40.761728
3   -73.97499915    40.68086214
4   -73.89488591    40.66471445

This is where I am getting tripped up.

train['latlng'] = train.apply(lambda row: '{},{}'.format(row['latitude'], 
row['longitude']), axis=1)
train['geocode_data'] = train['latlng'].map(reverse_geocode)
train['Zip'] =train['latlng'].apply(geolocator.reverse)
train['Zip'].apply(lambda x: pd.Series(x.split(',')))
foo = lambda x: pd.Series([i for i in reversed(x.split(','))])
train['Zip']=train['Zip'].apply(lambda x: str(x))
train['Zip']=train['Zip'].apply(foo)[1]

train

At the moment, I'm getting an error that:

AttributeError: 'Location' object has no attribute 'split'

How would I go about splitting up location so that I can just pick up the zip code?


回答1:


If all you want to get is the zip code, you can directly access the postcode attribute within the Location object.

`location.raw['address']['postcode`]



回答2:


In the geopy version 1.21.0 and using the import

from geopy.geocoders import Nominatim

I couldn't use the location.raw["address"]. An example of location.raw is as

follows:
{'place_id': 3330757,
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
 'osm_type': 'node',
 'osm_id': 368381231,
 'boundingbox': ['39.6634459', '39.6934459', '-75.6213155', '-75.5913155'],
 'lat': '39.6784459',
 'lon': '-75.6063155',
 'display_name': 'New Castle County Airport, South Hollow Road, Manor Park, New Castle County, Delaware, 19721, United States of America',
 'class': 'aeroway',
 'type': 'aerodrome',
 'importance': 0.6780150946188139,
 'icon': 'https://nominatim.openstreetmap.org/images/mapicons/transport_airport2.p.20.png'}

Instead I used location.address.split(",")[-2] which worked for me.




回答3:


Pass addressdetails=True to Nominatim to get this information (https://geopy.readthedocs.io/en/stable/#nominatim)

location = geolocator.geocode(query={'postalcode':'95119'}, addressdetails=True)
print(location)
location.raw

produces

San Jose, Santa Clara County, California, 95119, United States of America
{'place_id': 237756323,
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
 'boundingbox': ['37.072728062914',
  '37.392728062914',
  '-121.94612695296',
  '-121.62612695296'],
 'lat': '37.23272806291426',
 'lon': '-121.78612695295647',
 'display_name': 'San Jose, Santa Clara County, California, 95119, United States of America',
 'class': 'place',
 'type': 'postcode',
 'importance': 0.33499999999999996,
 'address': {'city': 'San Jose',
  'county': 'Santa Clara County',
  'state': 'California',
  'postcode': '95119',
  'country': 'United States of America',
  'country_code': 'us'}}


来源:https://stackoverflow.com/questions/47824915/how-to-use-geopy-to-obtain-the-zip-code-from-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!