input:
shape data which contain the block informaiton (.shp)
car data (.csv)
output:
heat map
point map
The images conbined the 0-23 hours of flux in / flux out and the sensor block data
You may need first install geopandas package by clone it on github
pip install geopandas
or
git clone https://github.com/geopandas/geopandas.git
cd geopandas
python setup.py install
This script is written by Yuxuan Cheng
yxcheng@uw.edu
In [1]:
#import the packages
import shapely
import geopandas as gpd
In [2]:
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
#plt.style.use('ggplot')
In [3]:
import pysal
from scipy import ndimage
In [4]:
#read the shape file
boros = gpd.GeoDataFrame.from_file('../../data/SelectedBlocks.shp')
#boros = gpd.GeoDataFrame.from_file('project/Output/OwithBG.shp')
#boros.plot()
In [8]:
boros.head()
Out[8]:
In [9]:
#show the plot of geopandas
boros.geometry[1245]
Out[9]:
In [ ]:
#read and rename the car data
flux = pd.read_csv('../../data/influxmap.csv')
#flux1 = pd.read_csv('project/Output/influxmap_blockgroup.csv')
flux = flux.rename(columns={'Unnamed: 0':'OBJECTID'})
In [ ]:
#show data
flux.head()
In [ ]:
#split the data - flux in vs flux out
flux_in = flux.where(flux>0,0)
flux_out = -flux.where(flux<0,0)
In [ ]:
#transplant the ID
flux_out['OBJECTID'] = flux['OBJECTID']
flux_out.head()
In [13]:
#merge the data by OBJECTID
flux_map = pd.merge(boros, flux, on=['OBJECTID'])
In [14]:
#continue to merge
flux_map_in = pd.merge(boros, flux_in, on=['OBJECTID'])
flux_map_out = pd.merge(boros, flux_out, on=['OBJECTID'])
In [15]:
#this function used for plotting the map
def plot_flux(hour,size,flux_map,direction):
"""
plot the map and save the images to current folder
direction 'in' or 'out'
hour range (0-23)
size 60,80 can be used
flux_map should be geopanda data
"""
if direction == 'out':
cmapstr = 'Reds'
filename1 = 'fluxmapOut'
else:
if direction == 'in':
cmapstr = 'Blues'
filename1 = 'fluxmapIn'
fig2 = flux_map.plot(column=str(hour), cmap=cmapstr,figsize=(size))#viridis
filename = filename1 + str(hour) + '.png'
plt.savefig(filename)
plt.show()
In [16]:
flux_map.columns
Out[16]:
In [17]:
#fig1 = flux_map_in.plot(column='0', cmap='Blues',figsize=(40,50))
#plt.show()
In [ ]:
#write the image files by a loop (0-23 hours)
for i in range(0,24):
plot_flux(i,(80,100),flux_map_in,'in')
In [22]:
#write the image files
for i in range(0,24):
plot_flux(i,(80,100),flux_map_out,'out')
In [18]:
#plot the histogram of one block
#plt.style.use('ggplot')
flux.loc[0].plot.bar(legend=False,figsize=(10,6))
Out[18]:
In [ ]:
#fig1 = flux_map_out.plot(column='0', cmap='Reds',figsize=(40,50))
#plt.show()
In [4]:
#try to draw the map
temp_shp = gpd.GeoDataFrame.from_file('project/kc_bg_10.shp')
In [7]:
temp_shp.plot(figsize=(50,60))
Out[7]:
In [1]:
#waiting for further research
#call the google map package
#to be continue
#import gmaps
#import gmaps.datasets
#gmaps.configure(api_key=AIzaSyBTiCtthOgV4O-ZY2T5bYlqOaH_5he4YnM)
In [6]:
#data = gmaps.datasets.load_dataset('taxi_rides')
In [ ]:
#m = gmaps.Map()
#m.add_layer(gmaps.Heatmap(data=data))
#m
In [ ]:
In [ ]: