<jupyter_start><jupyter_text>Drive time analysis for opioid epidemic This notebook performs drive time analysis for clinics of opioid epidemic treatment/prevention/both in the county of Oakland, Michigan.   1. Reading in clinics and Oakland county census block group (CBG) boundaries<jupyter_code>import arcgis
from arcgis.features import FeatureLayer
# Layer URLS for the Opioid Epidemic Clinics in Oakland County and Census Block Groups of Oakland County
clinic_url = 'https://services8.arcgis.com/TWfU0bgvgUkCgHLa/arcgis/rest/services/Oakland_County_Licensed_Program_Listings/FeatureServer/0/'
oakland_cbg = 'https://services.arcgis.com/bkrWlSKcjUDFDtgw/arcgis/rest/services/Oakland_2010_census_bg/FeatureServer/0/'

# create feature layers
clinic_layer = FeatureLayer(clinic_url)
oakland_layer = FeatureLayer(oakland_cbg)
# Query all clonic and census block groups as FeatureSets
clinic_features = clinic_layer.query()
print('Total number of rows in the clinic dataset: ')
print(len(clinic_features.features))

oakland_features = oakland_layer.query()
print('Total number of rows in the oakland dataset: ')
print(len(oakland_features.features))
# Convert to SpatialDataFrames
clinic = clinic_features.df
clinic.head()
oakland = oakland_features.df
oakland.head()<jupyter_output><empty_output><jupyter_text>2. Drive time analysis<jupyter_code># Connect to GIS
from arcgis.gis import GIS
gis = GIS('http://dcdev.maps.arcgis.com/', 'username', 'password')<jupyter_output><empty_output><jupyter_text>Service areas around the clinics need to be generated twice for two different sets of drive time inputs:> 1. __`[5, 10, 15, 20, 25]`__ that is needed to get the drive time required from each CBG to the closest clinic> 2. __`[5]`__ that is needed purely for visualization purposes  Service area generation for the [5, 10, 15, 20, 25] minutes layer<jupyter_code># Generate service areas
result = arcgis.features.use_proximity.create_drive_time_areas(clinic_url, [5, 10, 15, 20, 25], output_name="DriveTimeToClinics_All", overlap_policy='Dissolve')
# Check type of result
type(result)
# Share results for public consumption
result.share(everyone=True)
# Convert it to a FeatureSet
result_url = str(result.url)+'/0/'
drive_times_all_layer = FeatureLayer(result_url)
driveAll_features = drive_times_all_layer.query()
print('Total number of rows in the service area (upto 25 mins) dataset: ')
print(len(driveAll_features.features))
# Get results as a SpatialDataFrame
driveAll = driveAll_features.df
driveAll.head()<jupyter_output><empty_output><jupyter_text>Service area generation for the visible [5] minutes layer<jupyter_code># Generate service areas
visible_result = arcgis.features.use_proximity.create_drive_time_areas(clinic_url, [5], output_name="DriveTimeToClinics_5mins", overlap_policy='Dissolve')
# Grant 'visible_result' public access
visible_result.share(everyone=True)
# Generate the URL for this layer (later used for visualization)
visible_result_url = str(visible_result.url)+'/0'
visible_result_url<jupyter_output><empty_output><jupyter_text>3. Spatially join the `result_url` layer with `oakland_cbg` layer to obtain drive times for each census block group<jupyter_code># Perform spatial join between CBG layer and the service areas created for all time durations
cbg_with_driveTime = arcgis.features.analysis.join_features(oakland_cbg, result_url, spatial_relationship='Intersects', output_name='Oakland_CBG_allDriveTimes')
# Grant 'cbg_with_driveTime' public access
cbg_with_driveTime.share(everyone=True)
# Converting it to a FeatureSet
cbg_with_driveTime_url = str(cbg_with_driveTime.url)+'/0/'
drive_times_cbg = FeatureLayer(cbg_with_driveTime_url)
drive_times_features = drive_times_cbg.query()
print('Total number of rows in the drive time (upto 25 mins) dataset: ')
print(len(drive_times_features.features))
# Convert it to a SpatialDataFrame
drive_time_cbg_df = drive_times_features.df
drive_time_cbg_df.head()<jupyter_output><empty_output><jupyter_text>4. Enrich census block groups with suitable data The 3 geo-enrichment factors that we will be including in this study are:> 1. 2017 Total Population (Analysis Variable - TOTPOP_CY)> 2. 2017 Health Insurance (Analysis Variable - X8002_X)> 3. 2017 Education : Bachelor's Degree (Analysis Variable - BACHDEG_CY)<jupyter_code># Perform data enrichment
enriched_drive_time_cbg = arcgis.features.enrich_data.enrich_layer(drive_times_cbg, analysis_variables=['TOTPOP_CY', 'X8002_X', 'BACHDEG_CY'], output_name="Enriched_DriveTime_CBG")
# Check type of 'enriched_drive_time_cbg'
type(enriched_drive_time_cbg)
# Grant 'enriched_drive_time_cbg' public access
enriched_drive_time_cbg.share(everyone=True)
# Convert it to a Feature Set
enriched_url = str(enriched_drive_time_cbg.url)+'/0/'
enriched_driveTimes_layer = FeatureLayer(enriched_url)
enriched_driveTimes_features = enriched_driveTimes_layer.query()
print('Total number of rows in the drive time (upto 25 mins) dataset: ')
print(len(enriched_driveTimes_features.features))
# Convert it to a SpatialDataFrame
enriched_df = enriched_driveTimes_features.df
enriched_df.head()
# Print column names for ease of use during visualization
enriched_df.columns
# Format URL for visualization
enriched_lyr_url = enriched_url[:-1]
enriched_lyr_url<jupyter_output><empty_output><jupyter_text>5. Visualize results<jupyter_code># Create map of Oakland county
map1 = gis.map('Oakland, Michigan', zoomlevel=10)
map1<jupyter_output><empty_output><jupyter_text>![pythonresultmap](https://user-images.githubusercontent.com/13968196/36760882-94954672-1bea-11e8-94fd-6f68aaee313a.PNG)<jupyter_code># Add next layer for each CBG viualized based on its health insurance spending (X8002_2)
map1.add_layer({"type":"FeatureLayer",
                "url": enriched_lyr_url,
                "renderer":"ClassedSizeRenderer",
                "field_name":"X8002_X",
                "opacity":0.6
               })
# Add layer for service areas reachable within a 5 min drive time of the clinics
map1.add_layer({"type":"FeatureLayer",
                "url": visible_result_url,
                "opacity":0.75
               })
# Add layer of clinics
map1.add_layer({"type":"FeatureLayer",
                "url": clinic_url,
                "opacity":0.75
               })<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08989834815756036, 'fraction_numerical': 0.02255400254129606, 'mean_word_length': 6.213058419243986, 'pattern_counts': {'":': 11, '<': 33, '<?xml version=': 0, '>': 38, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '158429', 'license': 'Apache-2.0', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2008, 'n_words': 517, 'path': 'samples/04_gis_analysts_data_scientists/drive_time_analysis_for_opioid_epidemic.ipynb', 'repo_name': '2016102050016/arcgis-python-api', 'size': '6296'}