Python - Analysis of reachable circle of multiple traffic modes

DataCharm 2021-02-22 23:08:55
python analysis reachable circle multiple


Lei always sent me this picture the other day , I can't help recalling the little things of my life in school , I really want to go back to Chengdu , I believe you are all like me !

This time, we will use python Call Baidu api Realization Construction of accessibility circle for different modes of transportation , My high school classmate village head asked me to help him use Baidu a few days ago api Climb up poi data , I did some research , Found this api It's good to use , Except for climbing poi It can also achieve path planning , Just write a little bit of code to play , Now share with you .


First of all, a brief introduction to Baidu api The function of path planning , We can enter the mode of transportation 、 The starting point 、 The latitude and longitude coordinates of the destination , Baidu will return to us a json file , It contains automatically planned paths 、 The total length of the path and the estimated travel time . therefore , We can analyze the reachable circle of various modes of transportation at a point , Simply put, it's starting at that point 、 In some form of travel 、 The range that can be reached at a certain time . Besides , If you can also know the number of jobs in each community , Then we can calculate the accessibility 了 , Of course , This is beyond the scope of this article .

What we want to do this time is to analyze the cycling circle of Tianfu Square , Here's the code .

First, switch , It's just the same , See my first article for details :

import pandas as pd
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Point
from fiona.crs import from_epsg
plt.rcParams['font.sans-serif']=['Arial Unicode MS']
plt.rcParams['axes.unicode_minus']=False
pd.set_option('display.max_columns', None)

And then draw a grid , I won't write the code here , I wrote about it in the article about car Hailing , You can go to the last article to see , The direct result .

The specific areas to be analyzed are shown in the figure above , It's probably within the Third Ring Road of Chengdu , It's painted 40*40 The grid of , What we need to calculate is the riding time from Tianfu Square to each grid center . How to create a central point in the network about the car that article can also be found , It is omitted here .

Next we're going to call api It's time to calculate . First import the crawler and parse json Files need packages :

import requests
import json

Then start calling api Calculation :

area['ride_time'] = 0 ### area It's the grid of creation
for i in range(len(area)):
ak = #### Yours ak
url = 'http://api.map.baidu.com/directionlite/v1/riding?origin=30.66012,104.06352&coord_type=wgs84&destination={},{}&ak={}'.format(area_point.iloc[i,0].y,area_point.iloc[i,0].x,ak)
data = requests.get(url).json()
if data['status'] == 0:
area.loc[i,'ride_time'] = data['result']['routes'][0]['duration']
else:
area.loc[i,'ride_time'] = 0

Here's an explanation , First of all, register as the developer of Baidu map , So you get a api key, abbreviation ak, With this ak You can call Baidu's api 了 . But Baidu's path planning api If you go whoring for nothing, you can call at most 2000 Time , Local tyrants can be members , If you are not a local tyrant, you can register a few more numbers

.

This is a api Parameters that need to be entered , You can find , There are a lot of options , except ak, The latitude and longitude of the starting point and the ending point must be input , You can also choose an ordinary bicycle or a small electric donkey , Very intimate .

After the input parameter is called api, Will return a json file , Specific content Baidu's development documents also have an explanation :

You can see , The first is the status code , And then there's all kinds of information , What we need is actually result Below duration, That's the line time , For returned json And then analyze it according to key Just extract it . The specific code is all on it .

But there's a problem that needs attention , Some routes return status codes that are not 0, Erroneous . My approach to this point is if something goes wrong , Just set the time consumption of this line to 0, And then do a simple interpolation :

for i in range(len(area1)):
if area1.iloc[i,1] == 0:
area1.iloc[i,1] = (area1.iloc[i-1,1]+area1.iloc[i+1,1])/2

Of course, this is only a very rough method , If you write a paper, you'll still be obedient arcgis Interpolation in space .

Above, we get the riding time from Tianfu Square to each grid , Now let's visualize ;

area1['ride_time'] = -area1['ride_time']

Here, I'll first take the opposite number of time spent , because python I can't find the lighter the color the higher the number is

, If you take the opposite number, the shorter the travel time is, the larger the value will be , The darker the color of the visualization , It's convenient for us to observe .

import contextily as ctx
chengdu = chengdu.to_crs(from_epsg(3857))
area1 = area1.to_crs(from_epsg(3857)) ## Project into web Mercator coordinates
base = area1.plot(column='ride_time',figsize=(10,10),cmap='Blues',scheme='fisher_jenks',k=7,alpha=0.55)
ctx.add_basemap(base)
plt.xlim(11579000,11590000)
plt.ylim(3583500,3595000)
plt.gca().xaxis.set_major_locator(plt.NullLocator()) # Get rid of x Axis scale
plt.gca().yaxis.set_major_locator(plt.NullLocator()) # last year y Axis scale
plt.title(' Tianfu Square can reach the circle by bike ',fontsize=20)
plt.savefig(' Tianfu Square can reach the circle by bike .jpg',dpi=300)

And that's the end result , The depth of the color represents the time spent riding from Tianfu Square , It's distributed in circles , Generally speaking, it is more realistic ( Of course , The grid size is too large, which makes it look a little ugly , You can draw more squares or divide them with hexagons , It looks more advanced ).

Next, let's take a look at starting from Tianfu Square 15 It's a 15 minute bike ride :

area2 = area1[area1['ride_time']>-900] ## Screening travel time less than 900s Lattice of 
base = area2.plot(color='Red',alpha=0.4,figsize=(10,10))
ctx.add_basemap(base)
plt.title(' Riding in Tianfu Square 15 It's a lap in a minute ',fontsize=20)
plt.gca().xaxis.set_major_locator(plt.NullLocator()) # Get rid of x Axis scale
plt.gca().yaxis.set_major_locator(plt.NullLocator()) # last year y Axis scale
plt.savefig(' Riding in Tianfu Square 15 It's a lap in a minute .jpg',dpi=300)

Quack , It can be seen that , If you ride hard , From Tianfu Square 15 Minutes can still kick a ring

.

Besides , Baidu also provides driving 、 transit 、 Walking and other modes of transportation Path planning of api, You can try it yourself , Because of my ak Reached the daily limit , I won't show you , You can refer to Baidu map api Try it yourself , Use the code above to put url Just change it .

This is the end of this article !Yuan It's the end of the three chapters , Thank you for your encouragement and support . The purpose of writing this official account is to share with you and make progress together , I think the most important thing about learning in the Internet age is the spirit of sharing , In fact, our bags are shared and maintained by all kinds of gods , Every time I think about this, I am deeply moved . therefore , If my article helps you , Please like it more and forward it more , Let more people see , This is also an encouragement to me , thank you

This article is from WeChat official account. - DataCharm(shujumeili)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-11-15

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[DataCharm]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210222130146286k.html

  1. 使用Python开发DeFi项目
  2. python 函数详解
  3. Python工程师是做什么的?前景如何?
  4. Python - zip() 函数
  5. 30 周年生日,Python 先驱是怎么评价这门语言的?
  6. python将excel自适应导入数据库
  7. 从小白到大师,这里有一份Pandas入门指南
  8. [Python] 茎叶图和复合饼图的画法
  9. [Python interface automation] - regular use case parameterization
  10. Translation: practical Python Programming 02_ 02_ Containers
  11. Two years of Java, to write Python and go
  12. Translation: practical Python Programming 02_ 02_ Containers
  13. Two years of Java, to write Python and go
  14. Python-geoplot 空间核密度估计图绘制
  15. Python-seaborn 经济学人经典图表仿制
  16. python空间绘图- regionmask掩膜操作示例
  17. Python 空间绘图 - Cartopy 经纬度添加
  18. Python-pykrige包-克里金(Kriging)插值计算及可视化绘制
  19. Python 批量重采样、掩膜、坡度提取
  20. python - 多种交通方式可达圈分析
  21. Python 空间绘图 - 房价气泡图绘制
  22. Translation: practical Python Programming 02_ 02_ Containers
  23. Research on Portfolio Optimization Based on particle swarm optimization
  24. Ubuntu deploying Django project
  25. Two years of Java, write Python and go without byte beating
  26. Translation: practical Python Programming 02_ 02_ Containers
  27. So learn python, grandfather learned! Introduction to super simple Python
  28. python3 多线程 与 mongo亿级消费日志数据 新鲜demo 【优化第一版】
  29. Summary of Chinese word segmentation based on Jieba
  30. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  31. Summary of Chinese word segmentation based on Jieba
  32. From movie art to Python code to realize God's reverse thinking mode
  33. Summary of Chinese word segmentation based on Jieba
  34. ARIMA模型预测CO2浓度时间序列-python实现
  35. Python belongs to back-end development or front-end development? Introduction to Python!
  36. python isinstance()
  37. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  38. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  39. 如何用Python一键修改上万个文件名
  40. One day quick start to Python
  41. Python 学习笔记: List
  42. 翻译:《实用的Python编程》02_03_Formatting
  43. Is there any age requirement for learning Python? Is 30 OK?
  44. Professor Tsinghua! The most complete Python tutorial in 12 hours (free sharing at the end of the article)
  45. Using Python to develop defi project
  46. Detailed explanation of Python function
  47. Python 可变类型作为函数默认参数时的副作用
  48. What do Python engineers do? What's their future?
  49. 这是我见过最好的Python教程:十分钟带你认识Python
  50. Python欢喜冤家:爬虫与反爬虫带着处理方案来给大家拜年了
  51. Python - zip() function
  52. 写Python会遇到如下的错误:ModuleNotFoundError: No module named 'email.mime'; 'email' is not a package
  53. Python类的调用以及私有和公有属性方法的调用
  54. Python类的专有方法
  55. Python基础之:数字字符串和列表
  56. How did Python pioneers evaluate this language on their 30th birthday?
  57. Python基础之:数字字符串和列表
  58. Python基础之:数字字符串和列表
  59. 窥探未来不是梦,python数据分析轻松实现
  60. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model