{ "cells": [ { "cell_type": "markdown", "id": "386f1d9d637b8a26", "metadata": {}, "source": [ "# Comparison with ArcPy\n", "\n", "This notebook compares the usage of `footbridge` with `arcpy`." ] }, { "cell_type": "code", "execution_count": 1, "id": "d092f5c1f798da3f", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:43:49.931397Z", "start_time": "2025-08-31T22:43:42.978509Z" } }, "outputs": [], "source": [ "import os\n", "import arcpy\n", "import footbridge as ft\n", "from timer import Timer\n", "\n", "timer = Timer()\n", "\n", "# large dataset of US National Highway System roads, one feature class\n", "# https://hepgis-usdot.hub.arcgis.com/datasets/dce9f09392eb474c8ad8e6a78416279b_0\n", "gdb_path = os.path.abspath(\"NHS.gdb\")\n", "\n", "arcpy.env.overwriteOutput = True" ] }, { "cell_type": "markdown", "id": "c7b228efc15d702b", "metadata": { "ExecuteTime": { "end_time": "2025-08-24T18:51:12.449277Z", "start_time": "2025-08-24T18:51:12.440440Z" } }, "source": [ "## Load feature class into memory\n", "### ArcPy" ] }, { "cell_type": "code", "execution_count": 2, "id": "c8fb456d745ce40d", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:08.635736Z", "start_time": "2025-08-31T22:43:49.940533Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "18.6794 seconds\n" ] } ], "source": [ "timer.start()\n", "# ------------\n", "arcpy.env.workspace = gdb_path\n", "arcpy_fcs = arcpy.ListFeatureClasses()\n", "fc_name = arcpy_fcs[0]\n", "fc_path = os.path.join(gdb_path, fc_name)\n", "arcpy_fc = os.path.join(\"memory\", fc_name)\n", "arcpy.conversion.ExportFeatures(fc_path, arcpy_fc)\n", "# -----------\n", "timer.stop()" ] }, { "cell_type": "markdown", "id": "b4ee69dab232a892", "metadata": {}, "source": "### footbridge" }, { "cell_type": "code", "execution_count": 3, "id": "76fc771d99b08567", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:22.914934Z", "start_time": "2025-08-31T22:44:09.018110Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13.8833 seconds\n" ] } ], "source": [ "timer.start()\n", "# ------------\n", "ft_fc = ft.GeoDatabase(gdb_path)[0]\n", "# -----------\n", "timer.stop()" ] }, { "cell_type": "markdown", "id": "160b50b4e7557157", "metadata": {}, "source": [ "| | Fewer lines | More pythonic | Faster runtime | Overall |\n", "|----------:|:-----------:|:-------------:|:--------------:|:-------:|\n", "| ArcPy | ❌ | ❌ | ✅ | ❌ |\n", "| footbridge | ✅ | ✅ | ❌ | ✅ |" ] }, { "cell_type": "markdown", "id": "828baa83afe56539", "metadata": {}, "source": [ "## Count rows\n", "### ArcPy" ] }, { "cell_type": "code", "execution_count": 4, "id": "ada0cd9842f8d08b", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:23.072006Z", "start_time": "2025-08-31T22:44:22.956403Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.1106 seconds\n" ] }, { "data": { "text/plain": [ "'491781 rows'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timer.start()\n", "# ------------\n", "count_result = arcpy.management.GetCount(arcpy_fc)\n", "row_count = count_result[0]\n", "# -----------\n", "timer.stop()\n", "f\"{row_count} rows\"" ] }, { "cell_type": "markdown", "id": "5c22b3a9830c0fcf", "metadata": {}, "source": "### footbridge" }, { "cell_type": "code", "execution_count": 5, "id": "51935ca095c8472c", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:23.098605Z", "start_time": "2025-08-31T22:44:23.093504Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.001 seconds\n" ] }, { "data": { "text/plain": [ "'491781 rows'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timer.start()\n", "# ------------\n", "row_count = len(ft_fc)\n", "# -----------\n", "timer.stop()\n", "f\"{row_count} rows\"" ] }, { "cell_type": "markdown", "id": "17ccb2263c92aaaf", "metadata": {}, "source": [ "| | Fewer lines | More pythonic | Faster runtime | Overall |\n", "|----------:|:-----------:|:-------------:|:--------------:|:-------:|\n", "| ArcPy | ❌ | ❌ | ❌ | ❌ |\n", "| footbridge | ✅ | ✅ | ✅ | ✅ |" ] }, { "cell_type": "markdown", "id": "182b8db87ecd2cdd", "metadata": {}, "source": [ "## Filter rows\n", "### ArcPy" ] }, { "cell_type": "code", "execution_count": 6, "id": "53f409d3c2f70999", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:23.310319Z", "start_time": "2025-08-31T22:44:23.120699Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.1153 seconds\n" ] }, { "data": { "text/plain": [ "'24 rows'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timer.start()\n", "# ------------\n", "arcpy_fc_filtered = os.path.join(\"memory\", fc_name + \"_filtered\")\n", "arcpy.conversion.ExportFeatures(\n", " arcpy_fc, arcpy_fc_filtered, where_clause=\"ROUTEID = '50'\"\n", ")\n", "# -----------\n", "timer.stop()\n", "f\"{arcpy.management.GetCount(arcpy_fc_filtered)[0]} rows\"" ] }, { "cell_type": "markdown", "id": "f4288c3574aaa46e", "metadata": {}, "source": "### footbridge" }, { "cell_type": "code", "execution_count": 7, "id": "1b66cac88e4eeacd", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:23.382214Z", "start_time": "2025-08-31T22:44:23.339306Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0385 seconds\n" ] }, { "data": { "text/plain": [ "'24 rows'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timer.start()\n", "# ------------\n", "ft_fc_filtered = ft_fc.select_rows(\"ROUTEID == '50'\")\n", "# -----------\n", "timer.stop()\n", "f\"{len(ft_fc_filtered)} rows\"" ] }, { "cell_type": "markdown", "id": "ce1cbe647095ecbb", "metadata": {}, "source": [ "| | Fewer lines | More pythonic | Faster runtime | Overall |\n", "|----------:|:-----------:|:-------------:|:--------------:|:-------:|\n", "| ArcPy | ❌ | ❌ | ❌ | ❌ |\n", "| footbridge | ✅ | ✅ | ✅ | ✅ |" ] }, { "cell_type": "markdown", "id": "d4c42016d36e9ff", "metadata": {}, "source": [ "## Get the last row\n", "### ArcPy" ] }, { "cell_type": "code", "execution_count": 8, "id": "1de222bba389f6a", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:25.745604Z", "start_time": "2025-08-31T22:44:23.402812Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.3388 seconds\n" ] }, { "data": { "text/plain": [ "(491781,\n", " (-16685208.940766193, 8677951.279447716),\n", " '2025.03.27',\n", " 2022.0,\n", " 2.0,\n", " 20.0,\n", " '2281321F013',\n", " 0.0,\n", " 0.354,\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " 4.0,\n", " 1.0,\n", " ' ',\n", " ' ',\n", " ' ',\n", " 0.0,\n", " ' ',\n", " 3.0,\n", " 4.0,\n", " 1.0,\n", " 0.0,\n", " 0.0,\n", " 2305.0,\n", " 1744.0,\n", " 0.0,\n", " 0.0,\n", " 0.0,\n", " None,\n", " 0.354,\n", " datetime.datetime(2025, 3, 5, 0, 0),\n", " 'Add: STRAHNET Connector',\n", " 'AK_HPMS_FULL_2022')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timer.start()\n", "# ------------\n", "with arcpy.da.SearchCursor(arcpy_fc, field_names=[\"*\"]) as s_cursor:\n", " for row in s_cursor:\n", " this_row = row\n", "# -----------\n", "timer.stop()\n", "this_row" ] }, { "cell_type": "markdown", "id": "8a09198761fe5427", "metadata": {}, "source": "### footbridge" }, { "cell_type": "code", "execution_count": 9, "id": "60126af89fe0a051", "metadata": { "ExecuteTime": { "end_time": "2025-08-31T22:44:25.817824Z", "start_time": "2025-08-31T22:44:25.790682Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0039 seconds\n" ] }, { "data": { "text/html": [ "
| \n", " | VERSION | \n", "YEAR | \n", "STFIPS | \n", "CTFIPS | \n", "ROUTEID | \n", "BEGINPOINT | \n", "ENDPOINT | \n", "SIGN1 | \n", "SIGNT1 | \n", "SIGNN1 | \n", "... | \n", "AADT_COM | \n", "AADT_SINGL | \n", "FUT_AADT | \n", "FUT_YEAR | \n", "MILES | \n", "UPDATE_DAT | \n", "NHS_ACTION | \n", "FILE_NAME | \n", "SHAPE_Length | \n", "geometry | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ObjectID | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
| 491780 | \n", "2025.03.27 | \n", "2022.0 | \n", "2.0 | \n", "20.0 | \n", "2281321F013 | \n", "0.0 | \n", "0.354 | \n", "\n", " | \n", " | \n", " | ... | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "NaT | \n", "0.354 | \n", "2025-03-05 00:00:00+00:00 | \n", "Add: STRAHNET Connector | \n", "AK_HPMS_FULL_2022 | \n", "1176.089478 | \n", "MULTILINESTRING ((-16685133.238 8677491.246, -16685113.534 8677517.377, -16685110.974 8677534.721, -16685108.636 8677551.14, -16685105.964 8677567.558, -16685103.181 8677583.977, -16685100.398 8677600.396, -16685097.504 8677616.584, -16685094.721 8677633.003, -16685091.938 8677649.422, -16685089.155 8677665.841, -16685086.372 8677682.029, -16685083.478 8677698.679, -16685080.695 8677714.867, -16685077.801 8677731.286, -16685075.018 8677747.937, -16685072.123 8677764.125, -16685069.34 8677780.544, -16685066.446 8677796.964, -16685063.552 8677813.383, -16685060.658 8677829.803, -16685057.763 8677846.222, -16685054.98 8677862.642, -16685052.197 8677879.292, -16685049.748 8677895.712, -16685047.856 8677912.363, -16685046.743 8677929.245, -16685046.743 8677945.896, -16685047.744 8677962.778, -16685050.194 8677979.429, -16685053.756 8677995.849, -16685058.654 8678012.038, -16685064.665 8678027.764, -16685071.901 8678043.027, -16685080.361 8678057.597, -16685089.935 8678071.473, -16685100.51 8678084.656, -16685111.865 8678097.144, -16685123.998 8678108.707, -16685137.023 8678119.346, -16685151.049 8678128.828, -16685165.632 8678137.385, -16685180.66 8678144.554, -16685196.245 8678150.799, -16685212.386 8678155.655, -16685228.75 8678159.355, -16685245.337 8678161.899, -16685262.035 8678163.056, -16685278.955 8678163.287, -16685295.542 8678162.593, -16685312.24 8678160.974, -16685328.938 8678158.893, -16685345.302 8678156.118, -16685361.666 8678152.88, -16685377.918 8678149.179, -16685394.171 8678145.248, -16685410.312 8678141.085, -16685457.289 8678123.74, -16685473.43 8678118.883, -16685489.349 8678114.258, -16685505.379 8678109.401, -16685521.52 8678104.776, -16685537.662 8678100.613, -16685554.026 8678097.144, -16685570.501 8678094.369, -16685587.421 8678093.212, -16685604.231 8678093.675, -16685621.93 8678081.88)) | \n", "
1 rows × 35 columns
\n", "| \n", " | VERSION | \n", "YEAR | \n", "STFIPS | \n", "CTFIPS | \n", "ROUTEID | \n", "BEGINPOINT | \n", "ENDPOINT | \n", "SIGN1 | \n", "SIGNT1 | \n", "SIGNN1 | \n", "... | \n", "AADT_COM | \n", "AADT_SINGL | \n", "FUT_AADT | \n", "FUT_YEAR | \n", "MILES | \n", "UPDATE_DAT | \n", "NHS_ACTION | \n", "FILE_NAME | \n", "SHAPE_Length | \n", "geometry | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ObjectID | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
| 400000 | \n", "2025.03.27 | \n", "2020.0 | \n", "42.0 | \n", "43.0 | \n", "1 22 0322 - 4031 | \n", "4.862 | \n", "4.973 | \n", "U322 | \n", "U | \n", "322 | \n", "... | \n", "553.0 | \n", "593.0 | \n", "0.0 | \n", "NaT | \n", "0.109283 | \n", "NaT | \n", "\n", " | PA_NHS_2021 | \n", "230.148704 | \n", "MULTILINESTRING ((-8541219.173 4903933.712, -8541021.692 4903935.025, -8540989.076 4903933.274)) | \n", "
1 rows × 35 columns
\n", "