From 9934234276998b6a2c25ce7048628ab9ea0edfce Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Fri, 10 Mar 2023 17:40:55 -0800 Subject: [PATCH 1/2] add intersection example --- .../user_guide/cuspatial_api_examples.ipynb | 102 ++++++++++++++++-- 1 file changed, 95 insertions(+), 7 deletions(-) diff --git a/docs/source/user_guide/cuspatial_api_examples.ipynb b/docs/source/user_guide/cuspatial_api_examples.ipynb index 56b5b4410..899f94043 100644 --- a/docs/source/user_guide/cuspatial_api_examples.ipynb +++ b/docs/source/user_guide/cuspatial_api_examples.ipynb @@ -954,6 +954,94 @@ "With some careful grouping, one can reconstruct the original complete polygons that fall within the range." ] }, + { + "cell_type": "markdown", + "id": "3b33ce2b-965f-42a1-a89e-66d7ca80d907", + "metadata": {}, + "source": [ + "## Set Operations" + ] + }, + { + "cell_type": "markdown", + "id": "d73548f3-c9bb-43ff-9788-858f3b7d08e4", + "metadata": {}, + "source": [ + "### Linestring Intersections\n", + "\n", + "cuSpatial provides a linestring-linestring intersection algorithm to compute the overlapping geometries between two linestrings.\n", + "The API also returns the ids for each returned geometry to help user to trace back the source geometry." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "cc72a44d-a9bf-4432-9898-de899ac45869", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(\n", + "[\n", + " 0,\n", + " 144\n", + "]\n", + "dtype: int32, 0 POINT (-130.53611 54.80275)\n", + "1 POINT (-130.53611 54.80278)\n", + "2 POINT (-130.53611 54.80275)\n", + "3 POINT (-129.98000 55.28500)\n", + "4 POINT (-130.53611 54.80278)\n", + " ... \n", + "139 LINESTRING (-113.00000 49.00000, -113.00000 49...\n", + "140 LINESTRING (-83.89077 46.11693, -83.61613 46.1...\n", + "141 LINESTRING (-116.04818 49.00000, -116.04818 49...\n", + "142 LINESTRING (-120.00000 49.00000, -117.03121 49...\n", + "143 LINESTRING (-122.84000 49.00000, -120.00000 49...\n", + "Length: 144, dtype: geometry, lhs_linestring_id \\\n", + "0 [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, ... \n", + "\n", + " lhs_segment_id \\\n", + "0 [18, 16, 18, 15, 17, 137, 14, 16, 13, 15, 14, ... \n", + "\n", + " rhs_linestring_id \\\n", + "0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", + "\n", + " rhs_segment_id \n", + "0 [9, 10, 10, 11, 11, 28, 12, 12, 13, 13, 14, 15... )\n" + ] + } + ], + "source": [ + "from cuspatial.core.binops.intersection import pairwise_linestring_intersection\n", + "\n", + "host_dataframe = geopandas.read_file(geopandas.datasets.get_path(\"naturalearth_lowres\"))\n", + "usa_boundary = cuspatial.from_geopandas(host_dataframe[host_dataframe.name == \"United States of America\"].geometry.boundary)\n", + "canada_boundary = cuspatial.from_geopandas(host_dataframe[host_dataframe.name == \"Canada\"].geometry.boundary)\n", + "\n", + "boundary_intersections = pairwise_linestring_intersection(usa_boundary, canada_boundary)\n", + "\n", + "print(boundary_intersections)" + ] + }, + { + "cell_type": "markdown", + "id": "63f651fd-99e0-4c62-953c-9d1caf9145bf", + "metadata": {}, + "source": [ + "The result contains 3 series/dataframes.\n", + "\n", + "The first integer series shows that the first row of the result contains 144 geometries.\n", + "\n", + "The second element is a geoseries that contains the intersecting geometries, including points and linestrings.\n", + "\n", + "The third element is a dataframe that contains IDs to the input segments and linestrings, 4 for each result row.\n", + "Each represents ids to lhs, rhs linestring and segment ids." + ] + }, { "cell_type": "markdown", "id": "a17bd64a", @@ -976,7 +1064,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "id": "bf7b2256", "metadata": { "tags": [] @@ -993,7 +1081,7 @@ "dtype: int64" ] }, - "execution_count": 18, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -1078,7 +1166,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "id": "e3a0a9a3-0bdd-4f05-bcb5-7db4b99a44a3", "metadata": { "tags": [] @@ -1142,7 +1230,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "id": "023bd25a-35be-435d-ab0b-ecbd7a47e147", "metadata": { "tags": [] @@ -1201,7 +1289,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 22, "id": "784aff8e-c9ed-4a81-aa87-bf301b3b90af", "metadata": { "tags": [] @@ -1216,7 +1304,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 23, "id": "fea24c78-cf5c-45c6-b860-338238e61323", "metadata": { "tags": [] @@ -1301,7 +1389,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.10.9" }, "vscode": { "interpreter": { From f9c14449b432d3b6278158f772301369694d472e Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 14 Mar 2023 14:29:31 -0700 Subject: [PATCH 2/2] address review comments --- .../user_guide/cuspatial_api_examples.ipynb | 196 +++++++++++++----- 1 file changed, 141 insertions(+), 55 deletions(-) diff --git a/docs/source/user_guide/cuspatial_api_examples.ipynb b/docs/source/user_guide/cuspatial_api_examples.ipynb index 899f94043..761b11831 100644 --- a/docs/source/user_guide/cuspatial_api_examples.ipynb +++ b/docs/source/user_guide/cuspatial_api_examples.ipynb @@ -980,41 +980,7 @@ "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(\n", - "[\n", - " 0,\n", - " 144\n", - "]\n", - "dtype: int32, 0 POINT (-130.53611 54.80275)\n", - "1 POINT (-130.53611 54.80278)\n", - "2 POINT (-130.53611 54.80275)\n", - "3 POINT (-129.98000 55.28500)\n", - "4 POINT (-130.53611 54.80278)\n", - " ... \n", - "139 LINESTRING (-113.00000 49.00000, -113.00000 49...\n", - "140 LINESTRING (-83.89077 46.11693, -83.61613 46.1...\n", - "141 LINESTRING (-116.04818 49.00000, -116.04818 49...\n", - "142 LINESTRING (-120.00000 49.00000, -117.03121 49...\n", - "143 LINESTRING (-122.84000 49.00000, -120.00000 49...\n", - "Length: 144, dtype: geometry, lhs_linestring_id \\\n", - "0 [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, ... \n", - "\n", - " lhs_segment_id \\\n", - "0 [18, 16, 18, 15, 17, 137, 14, 16, 13, 15, 14, ... \n", - "\n", - " rhs_linestring_id \\\n", - "0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", - "\n", - " rhs_segment_id \n", - "0 [9, 10, 10, 11, 11, 28, 12, 12, 13, 13, 14, 15... )\n" - ] - } - ], + "outputs": [], "source": [ "from cuspatial.core.binops.intersection import pairwise_linestring_intersection\n", "\n", @@ -1022,24 +988,144 @@ "usa_boundary = cuspatial.from_geopandas(host_dataframe[host_dataframe.name == \"United States of America\"].geometry.boundary)\n", "canada_boundary = cuspatial.from_geopandas(host_dataframe[host_dataframe.name == \"Canada\"].geometry.boundary)\n", "\n", - "boundary_intersections = pairwise_linestring_intersection(usa_boundary, canada_boundary)\n", - "\n", - "print(boundary_intersections)" + "list_offsets, geometries, look_back_ids = pairwise_linestring_intersection(usa_boundary, canada_boundary)" ] }, { - "cell_type": "markdown", - "id": "63f651fd-99e0-4c62-953c-9d1caf9145bf", - "metadata": {}, + "cell_type": "code", + "execution_count": 19, + "id": "1125fd17-afe1-4b9c-b48d-8842dd3700b3", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\n", + "[\n", + " 0,\n", + " 144\n", + "]\n", + "dtype: int32" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "The result contains 3 series/dataframes.\n", - "\n", - "The first integer series shows that the first row of the result contains 144 geometries.\n", - "\n", - "The second element is a geoseries that contains the intersecting geometries, including points and linestrings.\n", - "\n", - "The third element is a dataframe that contains IDs to the input segments and linestrings, 4 for each result row.\n", - "Each represents ids to lhs, rhs linestring and segment ids." + "# The first integer series shows that the result contains 1 row (since we only have 1 pair of linestrings as input).\n", + "# This row contains 144 geometires.\n", + "list_offsets" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "b281e3bb-42d4-4d60-9cb2-b7dcc20b4776", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0 POINT (-130.53611 54.80275)\n", + "1 POINT (-130.53611 54.80278)\n", + "2 POINT (-130.53611 54.80275)\n", + "3 POINT (-129.98000 55.28500)\n", + "4 POINT (-130.53611 54.80278)\n", + " ... \n", + "139 LINESTRING (-113.00000 49.00000, -113.00000 49...\n", + "140 LINESTRING (-83.89077 46.11693, -83.61613 46.1...\n", + "141 LINESTRING (-116.04818 49.00000, -116.04818 49...\n", + "142 LINESTRING (-120.00000 49.00000, -117.03121 49...\n", + "143 LINESTRING (-122.84000 49.00000, -120.00000 49...\n", + "Length: 144, dtype: geometry" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# The second element is a geoseries that contains the intersecting geometries, with 144 rows, including points and linestrings.\n", + "geometries" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "e19873b9-2614-4242-ad67-caa47f807d04", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
lhs_linestring_idlhs_segment_idrhs_linestring_idrhs_segment_id
0[8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, ...[18, 16, 18, 15, 17, 137, 14, 16, 13, 15, 14, ...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...[9, 10, 10, 11, 11, 28, 12, 12, 13, 13, 14, 15...
\n", + "
" + ], + "text/plain": [ + " lhs_linestring_id \\\n", + "0 [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, ... \n", + "\n", + " lhs_segment_id \\\n", + "0 [18, 16, 18, 15, 17, 137, 14, 16, 13, 15, 14, ... \n", + "\n", + " rhs_linestring_id \\\n", + "0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", + "\n", + " rhs_segment_id \n", + "0 [9, 10, 10, 11, 11, 28, 12, 12, 13, 13, 14, 15... " + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# The third element is a dataframe that contains IDs to the input segments and linestrings, 4 for each result row.\n", + "# Each represents ids to lhs, rhs linestring and segment ids.\n", + "look_back_ids" ] }, { @@ -1064,7 +1150,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 22, "id": "bf7b2256", "metadata": { "tags": [] @@ -1081,7 +1167,7 @@ "dtype: int64" ] }, - "execution_count": 19, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -1166,7 +1252,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 23, "id": "e3a0a9a3-0bdd-4f05-bcb5-7db4b99a44a3", "metadata": { "tags": [] @@ -1230,7 +1316,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 24, "id": "023bd25a-35be-435d-ab0b-ecbd7a47e147", "metadata": { "tags": [] @@ -1289,7 +1375,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 25, "id": "784aff8e-c9ed-4a81-aa87-bf301b3b90af", "metadata": { "tags": [] @@ -1304,7 +1390,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 26, "id": "fea24c78-cf5c-45c6-b860-338238e61323", "metadata": { "tags": []