#!/usr/bin/env python # coding: utf-8 # # Compute distance to power substation # For each exit, compute the distance to the nearest power substation... # # 1. Fetch exit data, using arcgis package # 2. Fetch power substation data, using arcgis package # 3. Convert data to geopandas dataframes # 4. Compute nearest distance between exits and substations, using arcgis [find_nearest()](https://developers.arcgis.com/python/api-reference/arcgis.features.analysis.html#find-nearest) function # In[ ]: #Import packages from arcgis import GIS from arcgis.features import FeatureLayer, analysis import pandas as pd import geopandas as gpd import os # In[ ]: #Fetch exit data into a spatially enabled dataframe data_url = 'https://services7.arcgis.com/fqNd6NEGNf5qzQdv/arcgis/rest/services/TCI_NC_exits_2019_06_11_v1/FeatureServer/1' lyr_exits = FeatureLayer(data_url) sdf_exits = lyr_exits.query("State = 'NC'").sdf # In[ ]: #Save if not os.path.exists('./Data/MJBA'): os.mkdir('./Data/MJBA') sdf_exits.spatial.to_featureclass(location='/Data/MJBA/Exits.shp') # In[ ]: #Fetch power substation data data_url = 'https://services1.arcgis.com/Hp6G80Pky0om7QvQ/ArcGIS/rest/services/Electric_Substations_1/FeatureServer/0' sdf_substations = FeatureLayer(data_url).query("State = 'NC'").sdf # In[ ]: #Save if not os.path.exists('./Data/HILFD'): os.mkdir('./Data/HILFD') sdf_exits.spatial.to_featureclass(location='/Data/HILFD/Substations.shp') # In[ ]: sdfLyr = sdf_exits.spatial.to_featurelayer("Exits") # In[ ]: #Find the nearest substation to each exit foo = analysis.find_nearest(sdf_exits.to_featurelayer("Exits"))