python实现逆地理转码

用python 实现把经纬度坐标对应到各个城市的方法

参考文献地址http://www.cnblogs.com/giserliu/p/4982187.html

1.安装geopy

(1)可以用pip install geopy

(2)pycharm中选择file-setting-Projectuntitled-project Interpreter

2.程序代码

2.1反地理编码(将经纬度对应到城市)

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("51.43, 126.39")
print(location.address)
2.2地理编码(城市对应到经纬度)
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location1 = geolocator.geocode("175 5th Avenue NYC")
print((location1.latitude, location1.longitude))
问题二:
1.Python 中出现编译错误

address[i] =location.address.encode('utf')

借助百度api反地理编码

#coding=utf-8
import urllib2

import json
import os
import time
from time import sleep

url0 = 'http://api.map.baidu.com/geocoder/v2/?ak=你的密钥=renderReverse&location=%s,%s&output=json&pois=1'  # % (lng,lat)
f = open('D:\\Max\\python2.12\\new\\201601.TXT')
head = f.readline()
n=0
p = open('E:\\pressure_201601.txt', 'w')
p.write(head.strip() + ' ' + 'address' + '\n')
p.close()
for line in f:
    time.sleep(0.1#暂停0.1
   
n = n + 1

   
l = line.strip().split(' ')
    l = [i for i in l if i != '']
    lng = str(float(l[2]) / 100)
    lat = str(float(l[1]) / 100)
    url = url0 % (lat, lng)
    html = urllib2.urlopen(url)
    try:
        html = html.read().decode('utf-8')
        index1 = html.find('(')
        html = html[index1 + 1:-1]
        html = json.loads(html)
        address = html['result']['formatted_address']
        print n
    except:
        address = 'wrong'
   
address1 = address.encode("utf8")
    #print address
   
p = open('E:\\pressure_201601.txt', 'a')
    p.write(line.strip() + ' ' + address1+ '\n')
    p.close()
print("ok")

 
 

 


你可能感兴趣的:(python实现逆地理转码)