Created
February 21, 2016 18:38
-
-
Save rafaelhenrique/5e5169ebb7ff6bb4bbf7 to your computer and use it in GitHub Desktop.
An example to get coordinates using Google API https://developers.google.com/maps/documentation/geocoding/intro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import json | |
from urllib import urlopen | |
import time | |
DEBUG = True | |
TEMPO = 5 | |
def get_latlon_completo(numero, rua, bairro, cidade, estado): | |
retorno = ['NULL', 'NULL'] | |
address = (rua.replace(' ', '+') + ",+" + | |
numero + ",+" + cidade.replace(' ', '+') + "+-+" + | |
estado) | |
pais = "country:BR" | |
estado = "administrative_area_level_1:" + estado | |
numero = "street_number:" + numero | |
bairro = "neighborhood:" + bairro.replace(' ', '+') | |
cidade = "administrative_area_level_2:" + cidade.replace(' ', '+') | |
route = "route:" + rua.replace(' ', '+') | |
components = (pais + "|" + | |
estado + "|" + | |
cidade + "|" + | |
bairro + "|" + numero) | |
url = ('https://maps.googleapis.com/maps/api/geocode/json?address=' + | |
address + '&sensor=true_or_false&components=' + components) | |
pagina = urlopen(url).read() | |
dicionario = json.loads(pagina.decode('utf-8')) | |
if DEBUG: | |
print("URL completa: %s" % url) | |
if dicionario['status'] == "OK": | |
geometry = dicionario['results'][0]['geometry']['location'] | |
formatted_address = dicionario['results'][0]['formatted_address'] | |
retorno = [geometry['lng'], geometry['lat']] | |
if DEBUG: | |
print("Endereco completo: %s " % formatted_address) | |
return retorno | |
def get_latlon_cidade(cidade, estado): | |
retorno = ['NULL', 'NULL'] | |
address = cidade.replace(' ', '+') + "+-+" + estado | |
pais = "country:BR" | |
estado = "administrative_area_level_1:" + estado | |
cidade = "administrative_area_level_2:" + cidade.replace(' ', '+') | |
components = pais + "|" + estado + "|" + cidade | |
url = ('https://maps.googleapis.com/maps/api/geocode/json?address=' + | |
address + '&sensor=true_or_false&components=' + components) | |
pagina = urlopen(url).read() | |
dicionario = json.loads(pagina.decode('utf-8')) | |
if DEBUG: | |
print("URL completa: %s" % url) | |
if dicionario['status'] == "OK": | |
geometry = dicionario['results'][0]['geometry']['location'] | |
formatted_address = dicionario['results'][0]['formatted_address'] | |
retorno = [geometry['lng'], geometry['lat']] | |
if DEBUG: | |
print("Endereco completo: %s " % formatted_address) | |
return retorno | |
if __name__ == "__main__": | |
locais = [ | |
("35", "CONDE DE PARNAIBA", "CENTRO", "MOGI MIRIM", "SP"), | |
("1101", "BARAO DE MONTE SANTO", "APARECIDA", "MOCOCA", "SP"), | |
("1900", "COMERCIO", "PRIMAVERA", "ROSANA", "SP"), | |
("77", "DA BANDEIRA", "CENTRO", "ITATIBA", "SP"), | |
("546", "VINTE", "CENTRO", "IBIA", "MG"), | |
("350", "AV 14 DE DEZEMBRO", "CENTRO", "NOVA ESPERANCA", "PR"), | |
("158", "RUA ARAUJO PINHO", "CENTRO", "SERRINHA", "BA"), | |
("45", "FAUSTO CARDOSO", "CENTRO", "ESTANCIA", "SE") | |
] | |
for local_ in locais: | |
numero = local_[0] | |
rua = local_[1] | |
bairro = local_[2] | |
cidade = local_[3] | |
estado = local_[4] | |
coord_completo = get_latlon_completo( | |
numero, rua, bairro, cidade, estado) | |
print("Longitude: " + str(coord_completo[0])) | |
print("Latitude: " + str(coord_completo[1])) | |
time.sleep(TEMPO) | |
coord_cidade = get_latlon_cidade(cidade, estado) | |
print("Longitude: " + str(coord_cidade[0])) | |
print("Latitude: " + str(coord_cidade[1])) | |
lon = abs(abs(coord_cidade[0]) - abs(coord_completo[0])) | |
lat = abs(abs(coord_cidade[1]) - abs(coord_completo[1])) | |
print("Resultado Lon: " + str(lon)) | |
print("Resultado Lat: " + str(lat)) | |
print("---------") | |
time.sleep(TEMPO) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment