Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pairwise_linestring_intersection example in user guide #989

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 181 additions & 7 deletions docs/source/user_guide/cuspatial_api_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,180 @@
"With some careful grouping, one can reconstruct the original complete polygons that fall within the range."
isVoid marked this conversation as resolved.
Show resolved Hide resolved
isVoid marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"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": [],
"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",
"list_offsets, geometries, look_back_ids = pairwise_linestring_intersection(usa_boundary, canada_boundary)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1125fd17-afe1-4b9c-b48d-8842dd3700b3",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"<cudf.core.column.numerical.NumericalColumn object at 0x7f7e750a5340>\n",
"[\n",
" 0,\n",
" 144\n",
"]\n",
"dtype: int32"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>lhs_linestring_id</th>\n",
" <th>lhs_segment_id</th>\n",
" <th>rhs_linestring_id</th>\n",
" <th>rhs_segment_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>[8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, ...</td>\n",
" <td>[18, 16, 18, 15, 17, 137, 14, 16, 13, 15, 14, ...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" <td>[9, 10, 10, 11, 11, 28, 12, 12, 13, 13, 14, 15...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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"
]
},
{
"cell_type": "markdown",
"id": "a17bd64a",
Expand All @@ -976,7 +1150,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 22,
"id": "bf7b2256",
"metadata": {
"tags": []
Expand All @@ -993,7 +1167,7 @@
"dtype: int64"
]
},
"execution_count": 18,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -1078,7 +1252,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 23,
"id": "e3a0a9a3-0bdd-4f05-bcb5-7db4b99a44a3",
"metadata": {
"tags": []
Expand Down Expand Up @@ -1142,7 +1316,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 24,
"id": "023bd25a-35be-435d-ab0b-ecbd7a47e147",
"metadata": {
"tags": []
Expand Down Expand Up @@ -1201,7 +1375,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 25,
"id": "784aff8e-c9ed-4a81-aa87-bf301b3b90af",
"metadata": {
"tags": []
Expand All @@ -1216,7 +1390,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 26,
"id": "fea24c78-cf5c-45c6-b860-338238e61323",
"metadata": {
"tags": []
Expand Down Expand Up @@ -1301,7 +1475,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.9"
},
"vscode": {
"interpreter": {
Expand Down