python实现逆地理转码

来源:互联网 发布:java 数组长度 long 编辑:程序博客网 时间:2024/05/02 03:08

用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 Nominatimgeolocator = Nominatim()location = geolocator.reverse("51.43, 126.39")print(location.address)
2.2地理编码(城市对应到经纬度)
from geopy.geocoders import Nominatimgeolocator = 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 importsleep

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 inf:
    time.sleep(0.1#暂停0.1
   
n = n + 1

   
l = line.strip().split(' ')
    l = [i for i inl 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")

 
 

 


0 0