{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute distance to pipeline\n", "* Read in a number of locations (geocode if needed)\n", "* Compute the distance to the nearest pipeline, in km." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Imports\n", "import arcpy, os\n", "\n", "#Set environments\n", "arcpy.env.workspace = '..\\\\scratch'\n", "arcpy.env.scratchWorkspace = '..\\\\scratch'\n", "arcpy.env.overwriteOutput = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Data ingestion\n", "* Read in the natural gas pipeline dataset (won't work w/o permission)\n", "* Read the csv file (with coordinates) into a feature class" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Read in the pipeline data\n", "gdb_NG = '\\\\\\\\ns-leviathan.win.duke.edu\\\\biogas_GIS\\\\Rextag_Zone2\\\\Rextag_Zone2_Natural_Gas.gdb\\\\Natural_Gas'\n", "fc_NaturalGasPipelines = os.path.join(gdb_NG,'NaturalGasPipelines')\n", "if not arcpy.Exists(fc_NaturalGasPipelines):\n", " print(\"Error finding pipeline dataset!\")\n", "else:\n", " sr = arcpy.Describe(fc_NaturalGasPipelines).spatialReference\n", " srName = sr.name\n", " srCode = sr.GCSCode\n", " arcpy.env.outputCoordinateSystem=sr\n", " print(\"Natural gas pipeline feature class found.\")\n", " print(\"Spatial reference set to {} (code:{})\".format(srName,srCode))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Set path to address data csv file\n", "farmFile = '..\\\\data\\\\raw\\\\DairyFarms_18July2019.csv'\n", "\n", "#Read the data into a feature layer\n", "farmPts = arcpy.management.XYTableToPoint(in_table=farmFile,\n", " out_feature_class='..\\\\scratch\\\\farmPts',\n", " x_field='Longitude',\n", " y_field='Latitude',\n", " coordinate_system=arcpy.SpatialReference(4269))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Compute the distances from each farm feature to the nearest pipeline feature\n", "nearTable = '..\\\\scratch\\\\Distances.csv'\n", "nearTbl = arcpy.GenerateNearTable_analysis(in_features=farmPts,\n", " near_features=fc_NaturalGasPipelines,\n", " out_table=nearTable,\n", " method='GEODESIC')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Read the address file and the near table produced above and join them\n", "dfFarms = pd.read_csv(addressFile)\n", "dfNear = pd.read_csv(nearTable)\n", "dfOut = pd.merge(dfFarms,dfNear,left_index=True,right_index=True)[['Name','NEAR_DIST']]\n", "dfOut.columns = ('Name','Distance_m')\n", "dfOut['Distance_m'] = dfOut['Distance_m'].round().astype('int')\n", "dfOut.to_csv('..\\\\scratch\\\\Distances.csv',index=False)\n", "dfOut" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Copy farms to new FC\n", "farmsPts2 = arcpy.CopyFeatures_management(farmPts,\"..\\\\scratch\\\\farmsPts2.shp\")\n", "#Add FID of nearest pipeline\n", "arcpy.Near_analysis(in_features=farmsPts2,near_features=fc_NaturalGasPipelines,method='GEODESIC')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Join the pipe attributes to the farmsPts2 table\n", "arcpy.JoinField_management(in_data=farmsPts2,\n", " in_field='NEAR_FID',\n", " join_table=fc_NaturalGasPipelines,\n", " join_field='OBJECTID',fields=['Name','Operator','Diameter'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Export the table to a csv file\n", "arcpy.TableToTable" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }