In [ ]:
# GNU LESSER GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007.
# Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>.
# Everyone is permitted to copy and distribute verbatim copies of this 
# license document, but changing it is not allowed.

Creating Voronoi Tesselations

Automated Simulation in GIS


James Gaboardi, 2016


In [1]:
import numpy as np
import pysal as ps
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib as mpl
import multiprocessing as mp
import shapely
import time
from scipy.spatial import Voronoi, voronoi_plot_2d
%matplotlib inline

mpl.rcParams['figure.figsize'] = 20,20  #set the default map size
mpl.rcParams['patch.linewidth'] = 0.5  #set default polygon line width

np.random.seed(352)

# Local path on user's machine
path = '/Users/jgaboardi/Algorithms_etc./Data/'


/Users/jgaboardi/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

In [ ]:
def sq(x):
    for i in range(1000):
        return x*x

In [ ]:
cores = mp.cpu_count()
print cores,' Cores in each Node '
p = mp.Pool(processes=cores)


t1_multi = time.time()            # multi
output_mult = p.map(sq, Ai)
t2_multi = time.time() - t1_multi




Map_v_Multi = pd.DataFrame(index=count)
Map_v_Multi['Map'] = map_time
Map_v_Multi['Multi'] = multi_time
Map_v_Multi.index.name = 'Count'
Map_v_Multi.to_csv('MP_Sing.csv')

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [2]:
t1_shp_reads = time.time()

# Waverly Hills streets and bounding box
streets = gpd.read_file(path+'Waverly_Trim/Waverly.shp')
streets.to_crs(epsg=2779, inplace=True) # NAD83(HARN) / Florida North
streets.to_file(path+'waverly/waverly.shp')
shp_waverly = ps.open(path+'waverly/waverly.shp')
b_box = shp_waverly.bbox

'''
# Leon County streets and bounding box
streets = gpd.read_file(path+'Leon_County/LCSTSEG.shp')
streets.to_crs(epsg=2779, inplace=True) # NAD83(HARN) / Florida North
streets.to_file(path+'leon/leon.shp')
shp_Leon = ps.open(path+'leon/leon.shp')
b_box = shp_Leon.bbox
'''
t2_shp_reads = time.time()-t1_shp_reads
print t2_shp_reads


0.377574920654

In [3]:
# Individual street buffers in meters
intial_buffer = streets.buffer(50)  
intial_buffer[:5]


Out[3]:
0    POLYGON ((623124.0407875385 164212.5102460865,...
1    POLYGON ((622546.5762248621 163255.235891574, ...
2    POLYGON ((623559.7869314271 165528.3883790929,...
3    POLYGON ((623200.6855916528 164368.5069398382,...
4    POLYGON ((623558.3641320666 165459.8080567809,...
dtype: object

In [4]:
# Union of individual buffers
union_buffer = intial_buffer.unary_union  
union_buffer = gpd.GeoSeries(union_buffer)
union_buffer.crs = streets.crs
union_buffer = gpd.GeoDataFrame(union_buffer, crs=streets.crs)
union_buffer.columns = ['geometry']

In [5]:
# Create convex hull buffer union
convex_hull_of_union = gpd.GeoDataFrame(union_buffer.convex_hull)
convex_hull_of_union.columns = ['geometry']

In [6]:
# Generate (n) simulated households
n = 100
t1_allpoints = time.time()
np.random.seed(352)
x = np.random.uniform(b_box[0], b_box[2], n)
np.random.seed(850)
y = np.random.uniform(b_box[1], b_box[3], n)  
simulated_coords = zip(x,y)
simulated_coord_points = [shapely.geometry.Point(xy_coord) for xy_coord in simulated_coords]
simulated_households = gpd.GeoDataFrame(simulated_coord_points, crs=streets.crs)
simulated_households.columns = ['geometry']
t2_allpoints = time.time()-t1_allpoints
print t2_allpoints, 'seconds to simulate households in Waverly'


0.00458812713623 seconds to simulate households in Waverly

In [7]:
# No Multi Processing

# Identify households within the buffer
t1_intersection_no_multi = time.time()
intersection = [union_buffer['geometry'].intersection(hh) for hh in simulated_households['geometry']]
t2_intersection_no_multi = time.time()-t1_intersection_no_multi
print t2_intersection_no_multi, 'seconds to identify households within the 50m street buffer No MultiProcessing'
intersection = gpd.GeoDataFrame(intersection, crs=streets.crs)
intersection.columns = ['geometry']

# Ignore households not in the buffer --> shapely.geometry.collection.GeometryCollection
households_gdf = intersection[intersection.geom_type != 'GeometryCollection']
households_gdf[:5]


0.781038045883 seconds to identify households within the 50m street buffer No MultiProcessing
Out[7]:
geometry
2 POINT (621913.3153830337 164903.3379650218)
3 POINT (621612.0857702466 164217.1288344094)
5 POINT (621854.4787915241 163374.7302612905)
7 POINT (622860.0605300277 163653.4038665046)
8 POINT (621477.7574525437 164085.8951444005)

In [8]:
# Multi Processing

def point_in(simulated_coords):
    for hh in simulated_coords:
        return hh*2

cores = mp.cpu_count()
print cores,' Cores in each Node '
p = mp.Pool(processes=cores)


t1_intersection_multi = time.time()
multi_processing = p.map(point_in, simulated_coords)
t2_intersection_multi = time.time()-t1_intersection_multi


print t2_intersection_multi, 'seconds to identify households within the 50m street buffer with MULTI Processing'


4  Cores in each Node 
0.00734996795654 seconds to identify households within the 50m street buffer with MULTI Processing


In [9]:
multi_processing


Out[9]:
[1246776.9852811196,
 1245344.8185179709,
 1243826.6307660674,
 1243224.1715404931,
 1246829.7965350493,
 1243708.9575830481,
 1243310.2821786182,
 1245720.1210600554,
 1242955.5149050874,
 1244626.058365738,
 1246190.0002885114,
 1245234.8631695821,
 1242849.0517517556,
 1244381.1538365413,
 1244689.2528745129,
 1244992.6625946797,
 1244225.6900824239,
 1246318.2371632787,
 1245728.5317600335,
 1247288.8849162031,
 1244155.156243538,
 1246865.2663970443,
 1246069.9081882988,
 1245645.2315646056,
 1246690.8488781892,
 1243535.9010876028,
 1243903.1094580961,
 1246449.5115138483,
 1245936.0534389783,
 1246741.8866164552,
 1245599.5080708873,
 1245919.4743146154,
 1246286.551907609,
 1246921.9111613983,
 1243669.2872965306,
 1246593.6451259241,
 1246881.8160035796,
 1247325.4720569674,
 1246116.8352370372,
 1247070.3516201552,
 1244724.8953985756,
 1243243.9162871891,
 1247167.0838025727,
 1247057.0469263219,
 1243103.0690153481,
 1246249.7663301094,
 1244662.7246700807,
 1246841.2002314546,
 1243505.9770480422,
 1245505.3347935083,
 1246449.8079715339,
 1245634.38361922,
 1245897.4584575251,
 1245686.8420875024,
 1245549.8166074248,
 1243984.6856846511,
 1243120.3413816884,
 1246657.3206665213,
 1246043.7095950891,
 1246167.5446179903,
 1245042.4345661551,
 1247269.0215134274,
 1246082.526798625,
 1245032.172881779,
 1245037.4873886264,
 1244203.8738616223,
 1245824.1602796477,
 1242787.4597821783,
 1243953.4663547333,
 1243691.1893528332,
 1245005.3654941295,
 1247347.7798840378,
 1245230.7967700101,
 1243552.8726569661,
 1246196.1465971086,
 1246528.0836744548,
 1243103.8441626115,
 1243052.4632752764,
 1242986.9960668513,
 1244959.0905040468,
 1246255.7901323219,
 1243178.1360806711,
 1242767.5504321777,
 1247286.7041626167,
 1244441.2444302074,
 1243361.17926608,
 1246481.582084191,
 1245945.6693498672,
 1244981.1946634382,
 1244179.9476389429,
 1243686.8957084089,
 1245605.5972030221,
 1245670.3910019705,
 1243091.0887268761,
 1246978.2999433519,
 1243988.5237671745,
 1242976.3637127476,
 1246178.2571060776,
 1243845.5089854475,
 1244986.5431193118]

In [ ]: