未加星标

Python/Flask Data Visualization & Interactive Maps

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Python/Flask Data Visualization & Interactive Maps
python/Flask Data Visualization & Interactive Maps

Have you ever wanted to create an interactive data visualization map? In my most recent side project, I created a pretty cool visualization for how a virus might spread across the United States. If you want to check out the finished site, you can click here:

http://ethanszombies.herokuapp.com

Now, I’ll walk through my thought process for creating this, as well as step by step instructions on how to create your own. I know there are probably better/more efficient ways to do this, so if you are reading this, and have a better idea, please feel free to share. Here’s a breakdown of how this process will work for me. I’m really dividing this project up into two parts. The first part is gathering the data. The second part is the user portion, so users can use the data we’ve processed in the first part. Here’s a flow chart of how this workflow might look:


Python/Flask Data Visualization & Interactive Maps
1 ― What do we want to create? ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― -

For this, while I do want to make a map that simulates a virus outbreak, the virus I’m going to focus on is the zombie virus. Based on hours and hours of very important research, I found a few key points about how the zombie virus usually spreads:

1. The Zombie virus spreads more quickly in densely populated areas like innercities.

2. Zombies are clumsy and uncoordinated, making unfavorable terrain difficult. Thus, areas that have a high elevation would make the zombie virus spread moreslowly.

3. While the standard spread of the zombie virus is limited to a zombie walking around and biting people, occasionally the virus can spread more quickly if an infected individual tries to flee in a car or other vehicle, making roads and interstate highways favorable for quickly spreading thevirus.

There is certainly ample data to consider, like the effect weather, climate or the military would play in the advance of the virus, however, before things get too crazy, we’ll start with just tracking these three data points, 1. Population Density, 2. Elevation, 3. interstate highways.

2. ― What considerations do we need to think about? ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― -

Let’s say that we have a map of the United States, and each area in the map is colored white. We could represent an infected person by changing the color of that pixel, or div to a different color.

a. Time Intervals

First, we would need to decide on an interval of time. This interval could be 1 second, or even less. Each interval of time would represent an actual measurement of real time, like an hour or a day since the infection.

b. Decision at eachinterval

We could then simulate the actions of that div. At each interval of time, we would need to decide where the infected block moves, how fast it moves there, and whether or not it infects anyone.

We could use the data about each area to make those decisions.

Infection Population Density would determine if and how many other people an infected person infects at each interval. If an infected block is in a highly populated area, it would infect many people. If not, it may not infect anyone. Speed Elevation would be a factor in speed. At each interval, an infected block would move a certain distance, but if it is a higher elevation, that distance would be less. Direction Interstates would affect mainly direction. We could set the standard movement of each infected block to a random pattern, but if the zombie stumbles on or near a highway, then it’s direction would remain on the highway for the some amount of time. 3. ― How would we get the data we want onto a map? ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― -

The first step would be to encode the map with the data we need. For me, it seems like one way to do this would be to create a map of a certain dimension. For my purposes, I think 1150px X 768px should work nicely.


Python/Flask Data Visualization & Interactive Maps

Next, we would need to determine how much space on the map one zombie would occupy. After some testing, I found that an area approximately 5px X 5px was adequate. Any more than that, and it seemed clunky. Any less, that there was so much data to process, that it constantly ate up resources.

After deciding on a proper map size, my next move is to create an overlay on top of the map. This overlay would contain buttons the size I need (5px * 5px each).


Python/Flask Data Visualization & Interactive Maps

For population density, to start with, I’m going to create a max population density of 10, and a minimum of 0. These numbers are arbitrary, and are just meant to represent variation in density, but don’t necessarily correspond to actual population numbers.


Python/Flask Data Visualization & Interactive Maps

Although the above examples aren’t to scale, you can see the idea here at least. By creating a grid to overlay on top of the map, we can encode data into each block of the grid, that would change the way the outbreak reacts depending on which blocks the outbreak reaches.


Python/Flask Data Visualization & Interactive Maps

My thought is that you could click each button, and on each click, the number (density) would increase by 1. If we have 40,000 buttons, clicking each one seems unreasonable, so instead, I may choose to bind that event to a mouseover to make the entry of data faster. It might also be a good idea to change the color on the mouseover event, so it’s easier to keep track of what divs contain what values. Now that we’ve thought about the pseudo code, it’s a good time to actually start setting up our file and writing some actual code, since we have a more clear idea of what we want to create, and how we’ll have to create it. In a lot of projects, I spend a lot of time drawing things out before I write any code, which tends to make the code more organized in the end.

File Setup ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― ― -

The file structure that we’ll eventually have will probably look something like this:


Python/Flask Data Visualization & Interactive Maps

To start off, we can create most of this using the command line. Some of the files, like the density.txt and densitydata.json will be created in the first portion of the project.

on the command line, do the following:

mkdir <project folder name> cd <project folder name>

Now, we’ll create all the initial files on the command line:

touch app.py .gitignore README.md requirements.txt runtime.txt mkdir templates mkdir static cd templates touch base.html cd .. cd static touch main.css

Next, if we want to create a git repository and deploy on heroku, this is a good place to do that.

git init

Then, if we set up a heroku account, we can create a new heroku project by running:

heroku create <project name> Before we st

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Python/Flask Data Visualization &amp; Interactive Maps
本站链接:https://www.codesec.net/view/611119.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(71)