data processing and produce the images

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

imprort all the packages


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]:
ALAND10 AWATER10 BLOCKCE10 COUNTYFP10 FUNCSTAT10 GEOID10 INTPTLAT10 INTPTLON10 MTFCC10 NAME10 OBJECTID STATEFP10 Shape_Area Shape_Leng TRACTCE10 TRACT_10 TRBL UACE10 UR10 geometry
0 121128.0 0.0 1001 033 S 530330067001001 +47.6319178 -122.3399002 G5040 Block 1001 1 53 1.449595e-05 0.027448 006700 6700 6700.1001 None None POLYGON ((-122.3406299995747 47.6324870001751,...
1 20412.0 0.0 1009 033 S 530330069001009 +47.6368455 -122.3629779 G5040 Block 1009 2 53 2.443019e-06 0.006327 006900 6900 6900.1009 None None POLYGON ((-122.362319999768 47.63593299990816,...
2 16082.0 0.0 5021 033 S 530330116005021 +47.5204486 -122.3920438 G5040 Block 5021 4 53 1.920568e-06 0.005893 011600 11600 11600.5021 None None POLYGON ((-122.3917790003005 47.51974199989354...
3 2697.0 0.0 5023 033 S 530330116005023 +47.5205365 -122.3899013 G5040 Block 5023 5 53 3.220622e-07 0.003135 011600 11600 11600.5023 None None POLYGON ((-122.390215000122 47.52080000012006,...
4 16320.0 0.0 2007 033 S 530330115002007 +47.5272927 -122.3806803 G5040 Block 2007 18 53 1.949297e-06 0.006836 011500 11500 11500.2007 None None POLYGON ((-122.3793320000716 47.5276230000016,...

These data have the polygon 'geometry' informaiton


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()

merge the car2go data with the block data


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]:
Index([   u'ALAND10',   u'AWATER10',  u'BLOCKCE10', u'COUNTYFP10',
       u'FUNCSTAT10',    u'GEOID10', u'INTPTLAT10', u'INTPTLON10',
          u'MTFCC10',     u'NAME10',   u'OBJECTID',  u'STATEFP10',
       u'Shape_Area', u'Shape_Leng',  u'TRACTCE10',   u'TRACT_10',
             u'TRBL',     u'UACE10',       u'UR10',   u'geometry',
                u'0',          u'1',          u'2',          u'3',
                u'4',          u'5',          u'6',          u'7',
                u'8',          u'9',         u'10',         u'11',
               u'12',         u'13',         u'14',         u'15',
               u'16',         u'17',         u'18',         u'19',
               u'20',         u'21',         u'22',         u'23'],
      dtype='object')

In [17]:
#fig1 = flux_map_in.plot(column='0', cmap='Blues',figsize=(40,50))
#plt.show()

ploting


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]:
<matplotlib.axes._subplots.AxesSubplot at 0x14b11d30>

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]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a03c400>

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 [ ]: