python2.7 调用百度地图API 显示2017年中国楼市热度分布

本文主要参考:http://www.jianshu.com/p/773ff5f08a2c

年初,国家统计局公布了70个大中城市住宅销售价格变动情况
其中有一项为价格指数表:


image.png

为了直观显示热度分布,这里直接用了百度官网的API,恰好里面有heatmap.js。
网址为:
http://developer.baidu.com/map/jsdemo.htm#c1_15
从中可以看出,数组points的规范为:

var points =[
    {"lng":116.418261,"lat":39.921984,"count":50},
    {"lng":116.423332,"lat":39.916532,"count":51},
    ......
    ];

因此,我们最终python程序要将数据规整为上面points的表现形式。

这里我们采用同比数据,如下csv文件所示:

北京,127.0
天津,124.4
石家庄,118.9
太原,103.0
呼和浩特,101.0
沈阳,103.2
大连,102.5
长春,104.4
哈尔滨,102.1
上海,128.3
南京,137.3
杭州,127.6
宁波,111.2
合肥,144.2
福州,125.7
厦门,138.8
南昌,114.5
济南,119.0
青岛,113.2
郑州,127.7
武汉,124.2
长沙,118.4
广州,124.2
深圳,118.4
南宁,111.2
海口,106.5
重庆,107.7
成都,105.3
贵阳,105.3
昆明,104.2
西安,107.6
兰州,103.2
西宁,102.3
银川,102.4
乌鲁木齐,99.1
唐山,103.2
秦皇岛,107.0
包头,100.2
丹东,99.8
锦州,97.7
吉林,102.6
牡丹江,99.3
无锡,134.4
扬州,110.3
徐州,109.6
温州,104.4
金华,106.8
蚌埠,109.8
安庆,107.7
泉州,109.8
九江,112.2
赣州,114.0
烟台,105.5
济宁,102.0
洛阳,105.0
平顶山,103.5
宜昌,105.9
襄阳,103.2
岳阳,105.8
常德,103.3
惠州,124.7
湛江,109.3
韶关,108.7
桂林,103.8
北海,104.5
三亚,106.3
泸州,103.0
南充,102.0
遵义,102.0
大理,102.6

接下来我们便希望得到上述城市在地图中对应的经纬度,也就是points变量里面的lng、lat以及count信息。

在撰写python程序之前需要去官网获取ak码,登陆http://lbsyun.baidu.com/,在该网页最下方即可申请密匙。(ip可以写成0.0.0.0/0)

对应的python程序如下:

# -*- coding: UTF-8 -*-

#import requests
#from lxml import etree
#import pandas as pd

import csv
import json

import urllib
import sys
reload(sys)

sys.setdefaultencoding('utf8')


def getlonlat(address):
    ak = '你的百度api 的 key'
    ori_url ='http://api.map.baidu.com/geocoder/v2/'
    url = ori_url +'?address='+address+'&output=json'+'&ak='+ak
    req = urllib.urlopen(url)
    #urlopen返回 一个类文件对象(基础网页爬虫知识啦)
    res = req.read()
    temp = json.loads(res)
    # print req
    # print res
    # print temp
    return temp

# add ='山西'
# getlonlat(add)

file=open('longlat.json','w')
#在本地生成爬取的经纬度信息,以json格式保存
with open('price.csv','r') as csvfile:
#打开本地的price.csv文件
    reader = csv.reader(csvfile)
    for line in reader:
        city = line[0].strip()
        pricenum = line[1].strip()
        # print city,pricenum
        lon = getlonlat(city)['result']['location']['lng']
        lat = getlonlat(city)['result']['location']['lat']
        str_temp = '{"lat":' + str(lat) + ',"lng":' + str(lon) + ',"count":' + str(pricenum) +'},'
        print str_temp
#规整后的数据
        # print '"city":'+str(city)+',"lat":' + str(lat) + ',"lng":' + str(lon) + ',"count":' + str(pricenum) +''
        file.write(str_temp+'\n')
file.close()

最终得到数据如下:

{"lat":39.9299857781,"lng":116.395645038,"count":127.0},
{"lat":39.1439299033,"lng":117.210813092,"count":124.4},
{"lat":38.0489583146,"lng":114.522081844,"count":118.9},
{"lat":37.890277054,"lng":112.550863589,"count":103.0},
{"lat":40.8283188731,"lng":111.66035052,"count":101.0},
{"lat":41.8086447835,"lng":123.432790922,"count":103.2},
{"lat":38.9487099383,"lng":121.593477781,"count":102.5},
{"lat":43.8983376071,"lng":125.313642427,"count":104.4},
{"lat":45.7732246332,"lng":126.657716855,"count":102.1},
{"lat":31.24916171,"lng":121.487899486,"count":128.3},
{"lat":32.0572355018,"lng":118.778074408,"count":137.3},
{"lat":30.2592444615,"lng":120.219375416,"count":127.6},
{"lat":29.8852589659,"lng":121.579005973,"count":111.2},
{"lat":31.8669422607,"lng":117.282699092,"count":144.2},
{"lat":26.0471254966,"lng":119.330221107,"count":125.7},
{"lat":24.4892306125,"lng":118.103886046,"count":138.8},
{"lat":28.6895780001,"lng":115.893527546,"count":114.5},
{"lat":36.6827847272,"lng":117.024967066,"count":119.0},
{"lat":36.1052149013,"lng":120.384428184,"count":113.2},
{"lat":34.7566100641,"lng":113.64964385,"count":127.7},
{"lat":30.5810841269,"lng":114.316200103,"count":124.2},
{"lat":28.2134782309,"lng":112.979352788,"count":118.4},
{"lat":23.1200491021,"lng":113.307649675,"count":124.2},
{"lat":22.5460535462,"lng":114.025973657,"count":118.4},
{"lat":22.8064929356,"lng":108.297233556,"count":111.2},
{"lat":20.022071277,"lng":110.330801848,"count":106.5},
{"lat":29.5446061089,"lng":106.530635013,"count":107.7},
{"lat":30.6799428454,"lng":104.067923463,"count":105.3},
{"lat":26.6299067414,"lng":106.709177096,"count":105.3},
{"lat":25.0491531005,"lng":102.714601139,"count":104.2},
{"lat":42.9863649464,"lng":125.150148579,"count":107.6},
{"lat":36.064225525,"lng":103.823305441,"count":103.2},
{"lat":36.640738612,"lng":101.76792099,"count":102.3},
{"lat":38.5026210119,"lng":106.206478608,"count":102.4},
{"lat":43.8403803472,"lng":87.5649877411,"count":99.1},
{"lat":39.6505309225,"lng":118.183450598,"count":103.2},
{"lat":39.9454615659,"lng":119.604367616,"count":107.0},
{"lat":40.6471194257,"lng":109.846238532,"count":100.2},
{"lat":40.1290228266,"lng":124.338543115,"count":99.8},
{"lat":41.1308788759,"lng":121.147748738,"count":97.7},
{"lat":43.8719883344,"lng":126.564543989,"count":102.6},
{"lat":44.5885211528,"lng":129.608035396,"count":99.3},
{"lat":31.5700374519,"lng":120.305455901,"count":134.4},
{"lat":32.4085052546,"lng":119.427777551,"count":110.3},
{"lat":34.2715534311,"lng":117.188106623,"count":109.6},
{"lat":28.002837594,"lng":120.690634734,"count":104.4},
{"lat":29.1028991054,"lng":119.652575704,"count":106.8},
{"lat":32.9294989067,"lng":117.357079866,"count":109.8},
{"lat":30.5378978174,"lng":117.058738772,"count":107.7},
{"lat":24.901652384,"lng":118.600362343,"count":109.8},
{"lat":29.7196395261,"lng":115.999848022,"count":112.2},
{"lat":25.8452955363,"lng":114.935909079,"count":114.0},
{"lat":37.5365615629,"lng":121.30955503,"count":105.5},
{"lat":35.4021216643,"lng":116.600797625,"count":102.0},
{"lat":34.6573678177,"lng":112.447524769,"count":105.0},
{"lat":33.7453014565,"lng":113.300848978,"count":103.5},
{"lat":30.732757818,"lng":111.310981092,"count":105.9},
{"lat":32.2291685915,"lng":112.250092848,"count":103.2},
{"lat":29.3780070755,"lng":113.146195519,"count":105.8},
{"lat":29.0121488552,"lng":111.653718137,"count":103.3},
{"lat":23.1135398524,"lng":114.41065808,"count":124.7},
{"lat":21.2574631038,"lng":110.365067263,"count":109.3},
{"lat":24.8029603119,"lng":113.594461107,"count":108.7},
{"lat":25.262901246,"lng":110.260920147,"count":103.8},
{"lat":21.472718235,"lng":109.122627919,"count":104.5},
{"lat":18.2577759149,"lng":109.522771281,"count":106.3},
{"lat":28.8959298039,"lng":105.443970289,"count":103.0},
{"lat":30.8009651682,"lng":106.105553984,"count":102.0},
{"lat":27.6999613771,"lng":106.931260316,"count":102.0},
{"lat":25.6939666225,"lng":100.219208954,"count":102.6},

将以上数据填充在百度热力图编辑器里面的points变量里即可得到最终的房价涨幅热力图数据:

image.png
image.png

关于heatmap.js
其中的参数可以自行调整。

backgroundColor 画板的背景颜色设置,支持rgb(a),颜色名称,但必须要用引号

gradient 设置热点图的光圈颜色,数值为[0,1],数值大的在光圈内侧,数值相等则靠下的生效,数值设置不分大小顺序,并可以同时设置很多颜色

radius 设置光圈的半径大小,值>=0,=0取得是默认值

opacity 光圈透明度设置[0,1],如果值设置了,会重写maxOpacity和minOpacity的值

很惭愧就做了一点微小的整理!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容