Zones


In [ ]:
## Extract Zone Shapes
MS_CORRI = down_extract_zip ( 'https://opendata.arcgis.com/datasets/8e602ef5080e4343913702452ddf71d4_16.zip')
GS_CORRI = down_extract_zip ('https://opendata.arcgis.com/datasets/2ccac3f2585f487c9d14919d02d66740_13.zip')
HIS_UND = down_extract_zip ('https://opendata.arcgis.com/datasets/3060b61b0f9c444dbbd873bfae5ffa16_21.zip')
DDOT = down_extract_zip ('https://opendata.arcgis.com/datasets/32143ca8983d4476b64f4202162bf61e_12.zip')
NINV = down_extract_zip ('https://opendata.arcgis.com/datasets/6da0781990494f678e9f5722e1c09a67_23.zip')
IRB = down_extract_zip ('https://opendata.arcgis.com/datasets/eac41c3a46274a6bbe6184c6bbf412e8_15.zip')
DE = down_extract_zip ('https://opendata.arcgis.com/datasets/27221bef23ed4f5aa6e8e51d51edede9_19.zip')
ED = down_extract_zip ('https://opendata.arcgis.com/datasets/a3aefd57db394fd68d739556253dc44d_18.zip')
SVZ = down_extract_zip ('https://opendata.arcgis.com/datasets/67da6b571c5e41ec8c21b5ce54080434_1.zip')
DRZ = down_extract_zip ('https://opendata.arcgis.com/datasets/900148002b174f13881ecd3961119d9c_17.zip')
DTDEV = down_extract_zip ('https://opendata.arcgis.com/datasets/ab5cc72b8eb3494abea8c848855e22c7_3.zip')

## Read Zone Shapes In
MS_CORRI = gpd.read_file('/home/jovyan/group_e/data/8e602ef5080e4343913702452ddf71d4_16', crs='EPSG:4326')
GS_CORRI = gpd.read_file ('/home/jovyan/group_e/data/2ccac3f2585f487c9d14919d02d66740_13', crs='EPSG:4326')
DDOT = gpd.read_file ('/home/jovyan/group_e/data/32143ca8983d4476b64f4202162bf61e_12', crs='EPSG:4326')
NINV = gpd.read_file ('/home/jovyan/group_e/data/6da0781990494f678e9f5722e1c09a67_23', crs='EPSG:4326')
IRB = gpd.read_file ('/home/jovyan/group_e/data/eac41c3a46274a6bbe6184c6bbf412e8_15', crs='EPSG:4326')
DE = gpd.read_file ('/home/jovyan/group_e/data/27221bef23ed4f5aa6e8e51d51edede9_19', crs='EPSG:4326')
ED = gpd.read_file ('/home/jovyan/group_e/data/a3aefd57db394fd68d739556253dc44d_18', crs='EPSG:4326')
SVZ = gpd.read_file ('/home/jovyan/group_e/data/67da6b571c5e41ec8c21b5ce54080434_1', crs='EPSG:4326')
DRZ = gpd.read_file ('/home/jovyan/group_e/data/900148002b174f13881ecd3961119d9c_17', crs='EPSG:4326')
DTDEV = gpd.read_file ('/home/jovyan/group_e/data/ab5cc72b8eb3494abea8c848855e22c7_3', crs='EPSG:4326')

Features


In [ ]:
#Extract Feature Shapes 
LIQUOR = down_extract_zip ('https://opendata.arcgis.com/datasets/cabe9dcef0b344518c7fae1a3def7de1_5.zip')
PHARM = down_extract_zip ('https://opendata.arcgis.com/datasets/2335ba275c3f4320a3113f13181eab56_9.zip')
GROC = down_extract_zip ('https://opendata.arcgis.com/datasets/1d7c9d0e3aac49c1aa88d377a3bae430_4.zip')
AFH = down_extract_zip ('https://opendata.arcgis.com/datasets/34ae3d3c9752434a8c03aca5deb550eb_62.zip')
SCF = down_extract_zip ('https://opendata.arcgis.com/datasets/58562a06412e43b9acb2515010818b0a_28.zip')
FMKT = down_extract_zip ('https://opendata.arcgis.com/datasets/f2e1c2ef9eb44f2899f4a310a80ecec9_2.zip')
SVL = down_extract_zip ('https://opendata.arcgis.com/datasets/67da6b571c5e41ec8c21b5ce54080434_0.zip')
BANKS = down_extract_zip ('https://opendata.arcgis.com/datasets/dfc51a5bd29347d0a2399743d3144d31_0.zip')
CLUBS = down_extract_zip ('https://opendata.arcgis.com/datasets/4589d3e500404dc5a648dcdf3bc2732e_29.zip')
HOTELS = down_extract_zip ('https://opendata.arcgis.com/datasets/a3ed163dbf994792a010d742ef1f683d_6.zip')
SRV = down_extract_zip ('https://opendata.arcgis.com/datasets/4a84fa926f234916b129cbc022ec4935_29.zip')
COFA = down_extract_zip ('https://opendata.arcgis.com/datasets/ec634c5d3ce64a07bad5f74558acafcc_9.zip')
metro = down_extract_zip('https://opendata.arcgis.com/datasets/54018b7f06b943f2af278bbe415df1de_52.zip')

#Read Feature Shapes In
LIQUOR = gpd.read_file (LIQUOR, crs='EPSG:4326')
PHARM = gpd.read_file (PHARM, crs='EPSG:4326')
GROC = gpd.read_file (GROC, crs='EPSG:4326')
AFH = gpd.read_file (AFH, crs='EPSG:4326')
SCF = gpd.read_file (SCF, crs='EPSG:4326')
FMKT = gpd.read_file (FMKT, crs='EPSG:4326')
SVL = gpd.read_file (SVL, crs='EPSG:4326')
BANKS = gpd.read_file (BANKS, crs='EPSG:4326')
CLUBS = gpd.read_file (CLUBS, crs='EPSG:4326')
HOTELS = gpd.read_file (HOTELS, crs='EPSG:4326')
SRV = gpd.read_file (SRV, crs='EPSG:4326')
COFA = gpd.read_file (COFA, crs='EPSG:4326')
metro = gpd.read_file(metro, crs='EPSG:4326')

In [ ]:
ftr = [LIQOUR, PHARM, GROC, AFH, SCF, FMKT, SVL, BANKS, CLUBS, HOTELS, SRV, COFA, metro]
def concentration_pct(shp_gdf, raw, ftr, bufr=None):
    #Flag properties within distance "bufr" of featured locations 
    
    if not bufr:
        bufr = 1/250 #Hard to say what a good buffer is.
    
    assert isinstance(bufr, float) #buffer must be float!
    
    #Frame up the buffer shapes
    
    ftr.geometry = zone.geometry.buffer(bufr)
    ftr['flag'] = 1
    ftr.drop(['NAME'], axis=1, inplace=True)
    
    #Frame up the raw address points data
    pointy         = raw[['NAME', 'Points', 'dummy_counter']]
    pointy         = gpd.GeoDataFrame(pointy, crs=ftr.crs, 
                                      geometry=pointy.Points)
    pointy         = gpd.sjoin(pointy, zone, 
                               how='left', op='intersects')
    
    denom = pointy.groupby('NAME').sum()
    denom = denom.dummy_counter
    
    numer = pointy.groupby('NAME').sum()
    numer = numer.flag
    
    pct_ftr_coverage    = pd.DataFrame(numer/denom)
        
    pct_ftr_coverage.columns   = [
        'pct_metro_coverage'
    ]
    
    pct_ftr_coverage.fillna(0, inplace=True)
    
    pct_ftr_coverage.crs = pointy.crs
    shp_gdf = shp_gdf.merge(pct_metro_coverage,
                        how="left", left_on='NAME', right_index=True)
    return shp_gdf

In [ ]:
zone = [MS_CORRI, GS_CORRI, DDOT, NINV, IRB, DE, ED, SVZ, DRZ]

#This doesn't work b
#shp_df=dex[1]

def PointInZone(shp_gdf, raw, zone):
    
    zone['flag'] = 1
    zone.drop(['NAME'], axis=1, inplace=True)
    
    #Frame up the raw address points data
    pointy         = raw[['NAME', 'Points', 'dummy_counter']]
    pointy         = gpd.GeoDataFrame(pointy, crs=zone.crs, 
                                      geometry=pointy.Points)
    pointy         = gpd.sjoin(pointy, zone, 
                               how='left', op='intersects')
    
    inzone = pointy.groupby('NAME').sum()
    inzone = denom.dummy_counter
    
    flaginzone   = pd.DataFrame(inzone)
        
    flaginzone.columns   = [
        'flaginzone'
    ]
    
    flaginzone.fillna(0, inplace=True)
    
    flaginzone.crs = pointy.crs
    shp_gdf = shp_gdf.merge(flaginzone,
                        how="left", left_on='NAME', right_index=True)
    return shp_gdf