Python地理位置信息库geopy的使用(一):基本使用

geopy是Python关于地理位置的一个第三方库,用这个库来进行地址位置信息的查询和转换非常方便,本文介绍关于geopy的常用的几种用法

geopy的安装

pip install geopy

根据地址查询坐标及详细信息

>>> import json, logging
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("北京天安门")
>>> print location.address
天安门, 1, 西长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国
>>> print (location.latitude, location.longitude)
(39.90733345, 116.391244079988)
>>> print json.dumps(location.raw, indent=4, ensure_ascii=False, encoding='utf8')
{
    "display_name": "天安门, 1, 西长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国", 
    "importance": 0.00025, 
    "place_id": "74005413", 
    "lon": "116.391244079988", 
    "lat": "39.90733345", 
    "osm_type": "way", 
    "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright", 
    "osm_id": "25097203", 
    "boundingbox": [
        "39.9072273", 
        "39.9075343", 
        "116.3906566", 
        "116.3918428"
    ], 
    "type": "yes", 
    "class": "building"
}

根据坐标信息查询地址

>>> import json, logging
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.reverse("39.90733345,116.391244079988")
>>> print location.address
天安门, 1, 西长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国
>>> print json.dumps(location.raw, indent=4, ensure_ascii=False, encoding='utf8')
{
    "display_name": "天安门, 1, 西长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国", 
    "place_id": "74005413", 
    "lon": "116.391244079988", 
    "boundingbox": [
        "39.9072273", 
        "39.9075343", 
        "116.3906566", 
        "116.3918428"
    ], 
    "osm_type": "way", 
    "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright", 
    "osm_id": "25097203", 
    "lat": "39.90733345", 
    "address": {
        "building": "天安门", 
        "city": "北京市", 
        "house_number": "1", 
        "country": "中国", 
        "suburb": "东城区", 
        "state": "北京市", 
        "postcode": "100010", 
        "country_code": "cn", 
        "road": "西长安街"
    }
}
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值