版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xxzj_zz2017/article/details/79396980 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<div class="htmledit_views" id="content_views">
<p>第一次写博客,写的不好之处,请多多谅解。</p><p>这也算从其他人那借鉴来的,为示尊重,附上原作者的链接:<a href="https://mp.weixin.qq.com/s/47pGdqVQNnPWJc5v2Dpkhg" rel="nofollow" target="_blank">点击打开链接</a></p><p>话不多说,进入正题。</p><p>一、问题:</p><p>在<a href="https://www.baidu.com/s?wd=%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd" target="_blank">百度地图</a>对房价进行可视化</p><p>二、数据源:</p><p>house_price.csv 注意:数据名不要用中文。</p><p>链家网的上海房屋租金数据。维度:xiaoqu;mean:房屋租金均值。</p><p style="text-align:left;"><img src="https://img-blog.csdn.net/20180228104403613?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHh6al96ejIwMTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p style="text-align:left;">三、注册百度地图:<a href="http://lbsyun.baidu.com/apiconsole/key" rel="nofollow" target="_blank">点击打开链接</a></p><p style="text-align:left;">1、注册开发者账户:</p><p style="text-align:left;"><img src="https://img-blog.csdn.net/20180228104748703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHh6al96ejIwMTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p style="text-align:left;">2、申请秘钥</p><p style="text-align:left;"><img src="https://img-blog.csdn.net/20180228105048571?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHh6al96ejIwMTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p style="text-align:left;">3、创建应用</p><p style="text-align:left;"><img src="https://img-blog.csdn.net/20180228110205414?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHh6al96ejIwMTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p style="text-align:left;">四、生成热力图:</p><p style="text-align:left;">1、将小区地址转换为地图上的经纬度信息。将房价数据和代码放在同一目录下。</p><p style="text-align:left;">代码如下:</p><pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment"># -*- coding: utf-8 -*-</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> json</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">from</span> urllib.request <span class="hljs-keyword">import</span> urlopen,quote</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> csv</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> traceback</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> os</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">#构造获取经纬度的函数</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getlnglat</span><span class="hljs-params">(address)</span>:</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> url = <span class="hljs-string">'http://api.map.baidu.com/geocoder/v2/?address='</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> output = <span class="hljs-string">'json'</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">#ak = '你的ak'#需填入自己申请应用后生成的ak</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> add = quote(address)<span class="hljs-comment">#本文城市变量为中文,为防止乱码,先用quote进行编码</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> url2 = url+add+<span class="hljs-string">'&output='</span>+output+<span class="hljs-string">"&ak="</span>+ak</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> req = urlopen(url2)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> res = req.read().decode()</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> temp = json.loads(res)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> temp</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">file = open(<span class="hljs-string">'经纬度.json'</span>,<span class="hljs-string">'w'</span>)<span class="hljs-comment">#建立json数据文件</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">data_1 = pd.read_csv(<span class="hljs-string">"house_price.csv"</span>)<span class="hljs-comment">#读取小区房价信息</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> data_1.values:</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">try</span>:</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> b = i[<span class="hljs-number">0</span>].strip()</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> c = str(i[<span class="hljs-number">1</span>]).strip()</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> lng = getlnglat(b)[<span class="hljs-string">'result'</span>][<span class="hljs-string">'location'</span>][<span class="hljs-string">'lng'</span>]<span class="hljs-comment">#获取经度</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> lat = getlnglat(b)[<span class="hljs-string">'result'</span>][<span class="hljs-string">'location'</span>][<span class="hljs-string">'lat'</span>]<span class="hljs-comment">#获取纬度</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="29"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> str_temp = <span class="hljs-string">'{"lat":'</span> + str(lat) + <span class="hljs-string">',"lng":'</span> + str(lng) +<span class="hljs-string">',"count":'</span>+str(c) +<span class="hljs-string">'},'</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="30"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> file.write(str_temp)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="31"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">except</span>:</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> f = open(<span class="hljs-string">"异常日志.txt"</span>,<span class="hljs-string">'a'</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> traceback.print_exc(file=f)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> f.flush()</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="35"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> f.close() </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="36"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">file.close()</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre><p>原理:通过百度地图提供的Geocoding API接口,获得小区的经纬度。</p><p>注意:</p><p><img src="https://img-blog.csdn.net/20180228201335331?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHh6al96ejIwMTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p>该接口在未认证情况下,每天只能访问6k次,认证通过的每天可以请求30w次,如果大家在未认证情况下,调试程序时最好先调试好一个小区数据,再跑所有的数据。</p><p>2、生成热力图</p><p>打开http://developer.baidu.com/map/jsdemo.htm#c1_15,将源代码复制到本地代码编辑器中。注意:不要直接在文本编辑器中修改,可能会造成乱码。<br></p><p>修改:</p><p>(1)填写自己的秘钥ak</p><pre onclick="hljs.copyCode(event)"><code class="language-html hljs xml"><span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"http://api.map.baidu.com/api?v=2.0&ak=您的密钥"</span>></span><span class="undefined"></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span></code><div class="hljs-button" data-title="复制"></div></pre><p>(2)地图中心改为<a href="https://www.baidu.com/s?wd=%E4%B8%8A%E6%B5%B7%E4%B8%AD%E5%BF%83&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd" target="_blank">上海中心</a>坐标</p><pre onclick="hljs.copyCode(event)"><code class="language-html hljs xml">var point = new BMap.Point(121.480237, 31.236305);</code><div class="hljs-button" data-title="复制"></div></pre><p>(3)将经纬度数据替换成之前获得的经纬度数据。保存在经纬度.json文件中,直接复制。数据最后的逗号要删掉。</p><pre onclick="hljs.copyCode(event)"><code class="language-html hljs xml">var points =[ ]</code><div class="hljs-button" data-title="复制"></div></pre><p>(4)数据最大值设置为50000</p><pre onclick="hljs.copyCode(event)"><code class="language-html hljs xml">heatmapOverlay.setDataSet({data:points,max:50000});</code><div class="hljs-button" data-title="复制"></div></pre><p>将源代码另存为html文件,使用浏览器打开。</p><p>结果如下:</p><p><img src="https://img-blog.csdn.net/20180228205654186?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHh6al96ejIwMTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p>可以看到,有一些噪音数据存在,感兴趣的朋友可以对经纬度数据进行去噪处理。</p><p>代码和数据文件放在github上:https://github.com/data-science-z/-</p><br><p><br></p><p></p><br><br><p><br></p> </div>
</div>
Python调用百度地图
最新推荐文章于 2024-07-06 21:20:02 发布