您的位置:首页 > 编程语言 > Python开发

Python+R进行amap进行数据分析

2016-07-19 00:00 309 查看
摘要: 用Python+R进行数据分析展示

上海政府开放了数据服务网,从上面可以查询下载公共数据,下面的例子是污水处理厂的展示

原来想用R进行数据处理,并展示,但是发现R从amap下载下来的信息是空白,用Python就没有问题,不知道是什么原回。

A.从上海市政府数据服务网http://www.datashanghai.gov.cn/home!toHomePage.action下载所需要的数据,并且最好另存为txt格式(最好用utf-8)作为Python读入

B.用Python通过amap获取相关的经纬度信息

#__author__='ZHENGT'
# -*- coding: utf-8 -*-
#this module use amap(高德) api to get lon and lat
import os
import urllib
import pandas as pd,numpy as np
import json

#http://www.datashanghai.gov.cn/home!toHomePage.action
df = pd.read_table(r'C:\Users\zhengt\Desktop\WWT_Facility.txt')  # 读取目标文件

def getHtml(url):          #<---获取网页信息--->
page = urllib.urlopen(url_amap)                #访问网页
data = page.readline()                         #读入数据
data_dic = json.loads(data)                    #转换成python->字典
data_dic_geocodes = data_dic['geocodes'][0]   #获取geocodes信息,也是以字典存储
data__dic_location = data_dic_geocodes['location']  # 获取location信息
location = str(data__dic_location).split(",")   #处理locaiton成为List
print location
return location                                #返回List

def write2file(df_1,df_2,df_3,amap_location):   #<---数据写入文件--->
#df1-->名称
#df2-->地址
#data_amap-->经纬度(list)
print df_1,df_2,df_3,amap_location
f1 = open('result.txt', 'a')                  #固定写入此文件
f1.write(df_1 + ",")
f1.write(df_2+",")
f1.write(str(df_3)+",")
for i in amap_location:
f1.write(i + ",")
f1.write("\n")                             #回车符
f1.close()                                 #关闭文件

#主程序
if __name__=='__main__':
if os.path.exists('result.txt'):         #目标原文件是否存在
os.remove('result.txt')              #则删除

countdf=len(df)                           #df的行号,用以循环
i=0                                       #初始化i
while i<=countdf:                        #循环
df_1=df.ix[i,1]                     #名称
df_2=df.ix[i,2]                     #地址
df_3=df.ix[i,5]                     #处理量
i+=1
#         temp_address=df_2.encode('utf-8')        #转码
# http://lbs.amap.com/api/webservice/reference/georegeo/#t5 amap = r'http://restapi.amap.com/v3/geocode/geo?address=%s&city=上海&output=JSON&key=<yourKey>'
url_amap=amap % df_2
print url_amap
getLocation=getHtml(url_amap)             #获取数据
write2file(df_1,df_2,df_3,getLocation)         #输出至文件

至此会获得一个包含经纬度的txt文件

白龙港污水处理厂,上海市浦东新区龙东支路1号,79360.7,121.742971,31.247911,
竹园第一污水处理厂,上海市浦东新区东电路160号,57213.0,121.616690,31.348883,
天山污水处理厂,上海市天山路31号,2689.13,121.378517,31.215158,
曲阳污水处理厂,上海市东体育会路430号,1676.0,121.485668,31.279075,
东区水质净化厂,上海市杨浦区河间路1283号,94.0,121.548700,31.275780,
桃浦污水处理厂,上海市祁安路255号,2205.0,121.385788,31.288082,
泗塘污水处理厂,上海市宝山区爱辉路721号,809.0,121.447696,31.345512,
吴淞污水处理厂,上海市宝山区海江路1号,1433.0,121.503176,31.399408,
龙华水质净化厂,上海市徐汇区龙漕路180号,3509.0,121.440938,31.169522,
长桥水质净化厂,上海龙川北路625弄8号,803.0,121.439414,31.149020,
闵行水质净化厂,上海市闵行区江川东路757号,1794.0,121.432263,31.009608,

C.下面就是用R->googleMap来展示数据,有两种方法,一种是静态,一种是交互式

C0.首先理解一下源数据

#基本上高德地图amap的坐标和googleMap相符
library(maptools)
library(ggplot2)
library(ggmap)
library(googleVis)
amap.point<-read.table(file.choose(),head=F,fileEncoding="UTF-8",sep=",")
names(amap.point)<-c('name','address','treat_size','lon','lat','latlon')
amap.point$latlon<-with(amap.point, paste(lat, ":", lon, sep=""))

C1: ggmap->静态展示数据

#静态展示数据
gg<-ggmap(get_googlemap(center='shanghai',zoom=9,
maptype='terrain'),extent='device')
#添加点
gg<-gg+geom_point(aes(x=lon,y=lat,size=treat_size),color="red",
data=amap.point)
gg




C2: googleVis->交互式展示数据

ggMap<-gvisMap(amap.point,"latlon","treat_size",           #经纬表示
options=list(dataMode="makers",
showTip=TRUE,
showLine=TRUE,
enableScrollWheel=TRUE,
mapType='roadmap',
useMapTypeControl=TRUE))
plot(ggMap)

https://note.youdao.com/oldweb/list?notebook=%2F&sortMode=0¬e=%2FXRXFV1s%2Fweb1468915497477
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  高德地图 R Python