Python+Google Geocoding
本文主要介绍使用Python调用Google Geocoding API进行地址到地理坐标的转换。
Google Geocoding参考https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN
Google Geocoding API 目前最新版为 Geocoding API (V3)
要通过 HTTPS 访问 Geocoding API,请使用以下形式:
HTTP://maps.googleapis.com/maps/geocode/out?parameters
这里out参数指定请求返回的数据的格式,可以是json或者xml两者之一,本文使用json进行数据获取。本例子中取官方文档中的地址进行解析:
1600 Amphitheatre Parkway, Mountain View, CA,相应的json响应为:
{
"results" : [
{
"address_components" : [
{
"long_name" : "",
"short_name" : "",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "",
"short_name" : "",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4219998,
"lng" : -122.0839596
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4233487802915,
"lng" : -122.0826106197085
},
"southwest" : {
"lat" : 37.4206508197085,
"lng" : -122.0853085802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
其中json数据中包含两个键results和status。
Demo源码:
import urllib.request
import json,io,os,base64
#获得json数据
resquest = urllib.request.urlopen('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false')
data = resquest.read().decode('utf-8')
jsonData = json.loads(data)
results = jsonData['results'] #从json文件中读取results的值(形式为列表)
address = results[0]['formatted_address']
lat_lng = results[0]['geometry']['location']
print(address,'的经纬度为',lat_lng)
results是一个只包含一个元素的元组,该元素(results[0])又是一个json格式的数据,包含4个键address_components、formatted_address、geometry和types。我们这里需要的地理坐标就在geometry中。
Python+Google Geocoding的更多相关文章
- Day6 google Geocoding API
在看机器学习实战中K-means一章,练习中需要调用Yahoo PlaceFinder API 为地点添加经纬度,语言是python.申请到了appid但调用好像还要收费,要填写银行卡号才能用,没管那 ...
- python google play
#!/usr/env python #-*- coding: utf-8 -*- import urllib import urllib2 import random import requests ...
- 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- 详解Python Google Protocol Buffer
为什么要使用PB? PB(Protocol Buffer)是 Google 开发的用于结构化数据交换格式,作为腾讯云日志服务标准写入格式.因此用于写入日志数据前,需要将日志原始数据序列化为 PB 数据 ...
- 吴裕雄--天生自然python Google深度学习框架:经典卷积神经网络模型
import tensorflow as tf INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE_SIZE = 28 NUM_CHANNELS = 1 NUM_LABEL ...
- 吴裕雄--天生自然python Google深度学习框架:图像识别与卷积神经网络
- 吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...
- 吴裕雄--天生自然python Google深度学习框架:深度学习与深层神经网络
- 吴裕雄--天生自然python Google深度学习框架:TensorFlow实现神经网络
http://playground.tensorflow.org/
随机推荐
- git学习2:版本库
创建版本库 版本库,又称仓库,英文名为repository,版本库内的所有文件都可以被Git管理起来,即每个文件的修改.删除,Git都能跟踪. 1,在目录中创建版本库 在目录中有两种创建版本库的方法, ...
- Hbase数据导入导出
平时用于从生产环境hbase到导出数据到测试环境. 导入数据: import java.io.BufferedReader; import java.io.File; import java.io.F ...
- Laravel 使用多个数据库的问题。
这几天在使用Laravel 开发一个系统.这个系统连2个数据库.一个名为blog,一个名为center. center 数据库的作用是作为用户中心.可能会有其他几个系统相连,属于公用数据库.主要是用来 ...
- iis网站发布相关问题
最近在公司的服务器上发布了一个简单的web应用,整个做下来到上线用了将近2天时间,期间出现了各种问题,现在发出来供大家参考: 1.iis上发布后出现访问网站,出现“IIS服务器被配置为不列出此目录的内 ...
- jave占用CPU较高
转自http://www.tuicool.com/articles/YFVbia Linux下java进程CPU占用率高-分析方法 时间 2014-01-04 12:18:44 IT社区推荐资讯 原文 ...
- Python单链表实现
class Node(): def __init__(self,InitDate): self.Date=InitDate self.next=None def setNext(self,newnex ...
- servlet filter和springMVC拦截器的区别
参考 http://blog.csdn.net/ggibenben1314/article/details/45341855
- expdp远程导出数据
环境: 源: os:windows db:11.2.0.1 ip:192.168.213.129 sid:orcl 远程机导出: os:centos 6.5 x64 db:11.2.0.4 ip:19 ...
- SQL Server 数据库备份
declare @filename varchar(1024) declare @SQLDB varchar(50) declare @path varchar(1024) set @path = N ...
- 关于jq+devexpress基础知识总结(随便的基础)
//获取某行某列的值 onSelectionChanged: function (selectedItems) { ]; if (data != null) postionno = data.POST ...