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 python API pairwise_point_polygon_distance #988

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
965b80a
initial
isVoid Feb 28, 2023
38af8e6
Merge branch 'branch-23.04' of https://github.com/rapidsai/cuspatial …
isVoid Mar 1, 2023
02f61fe
add pragma once for floating_point.cuh
isVoid Mar 7, 2023
7bd797f
add polygon_ref structure
isVoid Mar 7, 2023
0968c15
add multipolygon_ref class
isVoid Mar 7, 2023
a659eab
update multipolygon_range class
isVoid Mar 7, 2023
c274070
update multipoint_range class
isVoid Mar 7, 2023
12ffa53
update is_point_in_polygon usage with polygon_ref
isVoid Mar 7, 2023
7490333
update multilinestring_range
isVoid Mar 7, 2023
f665287
add point to polygon kernel
isVoid Mar 7, 2023
291f6e6
add segment deduction guide
isVoid Mar 7, 2023
efa6883
add owning object type to vector factories
isVoid Mar 7, 2023
23146ef
add tests
isVoid Mar 7, 2023
ead160a
add helper files
isVoid Mar 7, 2023
09bd35f
add more tests
isVoid Mar 8, 2023
92760d1
bug fixes
isVoid Mar 8, 2023
8acb5dc
cleanups
isVoid Mar 8, 2023
a2b94fe
fix tests
isVoid Mar 8, 2023
46a67fe
optimize single point range input
isVoid Mar 8, 2023
b725b52
docs, type checks in range ctor
isVoid Mar 8, 2023
cb5706a
Merge branch 'branch-23.04' into feature/polygon_distances
isVoid Mar 8, 2023
ab59e7d
use range based for loop in is_point_in_polygon
isVoid Mar 8, 2023
ddcd5d2
initial column API
isVoid Mar 9, 2023
b136c0b
Apply suggestions from code review
isVoid Mar 9, 2023
744f32f
add docs
isVoid Mar 9, 2023
bb6c637
style
isVoid Mar 9, 2023
756650b
Merge branch 'branch-23.04' of https://github.com/rapidsai/cuspatial …
isVoid Mar 9, 2023
ec23d6e
add column API tests, augment column_factories
isVoid Mar 10, 2023
8319e7c
fix bug in PiP tests
isVoid Mar 10, 2023
85fab66
Merge branch 'feature/polygon_distances' into feature/point_polygon_d…
isVoid Mar 10, 2023
43cc02a
add validation checks
isVoid Mar 10, 2023
2004b3a
add cython API
isVoid Mar 10, 2023
3d5964b
add python API and tests
isVoid Mar 10, 2023
baed5a3
add user guide
isVoid Mar 10, 2023
386ad1f
Merge branch 'branch-23.04' of https://github.com/rapidsai/cuspatial …
isVoid Mar 21, 2023
402a8e3
remove unused code pieces and files
isVoid Mar 21, 2023
67c931b
Merge branch 'branch-23.04' into feature/point_polygon_distance_colum…
isVoid Mar 21, 2023
3f1da02
address docs review
isVoid Mar 21, 2023
4b33176
Merge branch 'feature/point_polygon_distance_column_api' of github.co…
isVoid Mar 21, 2023
961f6ce
Merge branch 'branch-23.04' into feature/point_polygon_distance_colum…
isVoid Mar 21, 2023
50b6c6b
style
isVoid Mar 21, 2023
375fae1
Merge branch 'feature/point_polygon_distance_column_api' of github.co…
isVoid Mar 21, 2023
079f45c
Merge branch 'feature/point_polygon_distance_column_api' into feature…
isVoid Mar 21, 2023
c965734
Merge branch 'branch-23.04' of https://github.com/rapidsai/cuspatial …
isVoid Mar 21, 2023
fcc438b
revert user_guide
isVoid Mar 22, 2023
77cfd5a
add api doc
isVoid Mar 22, 2023
c22cb46
update docs
isVoid Mar 22, 2023
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
185 changes: 170 additions & 15 deletions docs/source/user_guide/cuspatial_api_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,161 @@
"print(gpu_polygons.head())"
]
},
{
"cell_type": "markdown",
"id": "008d320d-ca47-459f-9fff-8769494c8a61",
"metadata": {},
"source": [
"### cuspatial.pairwise_point_polygon_distance\n",
"\n",
"Using WGS 84 Pseudo-Mercator, distances are in meters."
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "258c9a8c-7fe3-4047-80b7-00878d9fb2f1",
"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>pop_est</th>\n",
" <th>continent</th>\n",
" <th>name</th>\n",
" <th>iso_a3</th>\n",
" <th>gdp_md_est</th>\n",
" <th>geometry</th>\n",
" <th>distance_from</th>\n",
" <th>distance</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>889953.0</td>\n",
" <td>Oceania</td>\n",
" <td>Fiji</td>\n",
" <td>FJI</td>\n",
" <td>5496</td>\n",
" <td>MULTIPOLYGON (((20037508.343 -1812498.413, 200...</td>\n",
" <td>Vatican City</td>\n",
" <td>1.969350e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>58005463.0</td>\n",
" <td>Africa</td>\n",
" <td>Tanzania</td>\n",
" <td>TZA</td>\n",
" <td>63177</td>\n",
" <td>POLYGON ((3774143.866 -105758.362, 3792946.708...</td>\n",
" <td>San Marino</td>\n",
" <td>5.929777e+06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>603253.0</td>\n",
" <td>Africa</td>\n",
" <td>W. Sahara</td>\n",
" <td>ESH</td>\n",
" <td>907</td>\n",
" <td>POLYGON ((-964649.018 3205725.605, -964597.245...</td>\n",
" <td>Vaduz</td>\n",
" <td>3.421172e+06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>37589262.0</td>\n",
" <td>North America</td>\n",
" <td>Canada</td>\n",
" <td>CAN</td>\n",
" <td>1736425</td>\n",
" <td>MULTIPOLYGON (((-13674486.249 6274861.394, -13...</td>\n",
" <td>Lobamba</td>\n",
" <td>1.296059e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>328239523.0</td>\n",
" <td>North America</td>\n",
" <td>United States of America</td>\n",
" <td>USA</td>\n",
" <td>21433226</td>\n",
" <td>MULTIPOLYGON (((-13674486.249 6274861.394, -13...</td>\n",
" <td>Luxembourg</td>\n",
" <td>8.174897e+06</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" pop_est continent name iso_a3 gdp_md_est \\\n",
"0 889953.0 Oceania Fiji FJI 5496 \n",
"1 58005463.0 Africa Tanzania TZA 63177 \n",
"2 603253.0 Africa W. Sahara ESH 907 \n",
"3 37589262.0 North America Canada CAN 1736425 \n",
"4 328239523.0 North America United States of America USA 21433226 \n",
"\n",
" geometry distance_from \\\n",
"0 MULTIPOLYGON (((20037508.343 -1812498.413, 200... Vatican City \n",
"1 POLYGON ((3774143.866 -105758.362, 3792946.708... San Marino \n",
"2 POLYGON ((-964649.018 3205725.605, -964597.245... Vaduz \n",
"3 MULTIPOLYGON (((-13674486.249 6274861.394, -13... Lobamba \n",
"4 MULTIPOLYGON (((-13674486.249 6274861.394, -13... Luxembourg \n",
"\n",
" distance \n",
"0 1.969350e+07 \n",
"1 5.929777e+06 \n",
"2 3.421172e+06 \n",
"3 1.296059e+07 \n",
"4 8.174897e+06 \n",
"(GPU)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cities = geopandas.read_file(geopandas.datasets.get_path(\"naturalearth_cities\")).to_crs(3857)\n",
"countries = geopandas.read_file(geopandas.datasets.get_path(\"naturalearth_lowres\")).to_crs(3857)\n",
"\n",
"gpu_cities = cuspatial.from_geopandas(cities)\n",
"gpu_countries = cuspatial.from_geopandas(countries)\n",
"\n",
"dist = cuspatial.pairwise_point_polygon_distance(\n",
" gpu_cities.geometry[:len(gpu_countries)], gpu_countries.geometry\n",
")\n",
"\n",
"gpu_countries[\"distance_from\"] = cities.name\n",
"gpu_countries[\"distance\"] = dist\n",
"\n",
"gpu_countries.head()"
]
},
{
"attachments": {
"351aea0c-f37e-4ab9-bad2-c67bce69b5c3.png": {
Expand All @@ -910,7 +1065,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"id": "d1ade9da-c9e2-45c4-9685-dffeda3fd358",
"metadata": {
"tags": []
Expand Down Expand Up @@ -975,7 +1130,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"id": "cc72a44d-a9bf-4432-9898-de899ac45869",
"metadata": {
"tags": []
Expand All @@ -993,7 +1148,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"id": "1125fd17-afe1-4b9c-b48d-8842dd3700b3",
"metadata": {
"tags": []
Expand All @@ -1002,15 +1157,15 @@
{
"data": {
"text/plain": [
"<cudf.core.column.numerical.NumericalColumn object at 0x7f7e750a5340>\n",
"<cudf.core.column.numerical.NumericalColumn object at 0x7f4a31428840>\n",
"[\n",
" 0,\n",
" 144\n",
"]\n",
"dtype: int32"
]
},
"execution_count": 19,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1023,7 +1178,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 21,
"id": "b281e3bb-42d4-4d60-9cb2-b7dcc20b4776",
"metadata": {
"tags": []
Expand All @@ -1046,7 +1201,7 @@
"Length: 144, dtype: geometry"
]
},
"execution_count": 20,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1058,7 +1213,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 22,
"id": "e19873b9-2614-4242-ad67-caa47f807d04",
"metadata": {
"tags": []
Expand Down Expand Up @@ -1117,7 +1272,7 @@
"0 [9, 10, 10, 11, 11, 28, 12, 12, 13, 13, 14, 15... "
]
},
"execution_count": 21,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -1150,7 +1305,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 23,
"id": "bf7b2256",
"metadata": {
"tags": []
Expand All @@ -1167,7 +1322,7 @@
"dtype: int64"
]
},
"execution_count": 22,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -1252,7 +1407,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 24,
"id": "e3a0a9a3-0bdd-4f05-bcb5-7db4b99a44a3",
"metadata": {
"tags": []
Expand Down Expand Up @@ -1316,7 +1471,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 25,
"id": "023bd25a-35be-435d-ab0b-ecbd7a47e147",
"metadata": {
"tags": []
Expand Down Expand Up @@ -1375,7 +1530,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 26,
"id": "784aff8e-c9ed-4a81-aa87-bf301b3b90af",
"metadata": {
"tags": []
Expand All @@ -1390,7 +1545,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 27,
"id": "fea24c78-cf5c-45c6-b860-338238e61323",
"metadata": {
"tags": []
Expand Down
1 change: 1 addition & 0 deletions python/cuspatial/cuspatial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
pairwise_point_distance,
pairwise_point_linestring_distance,
pairwise_point_linestring_nearest_points,
pairwise_point_polygon_distance,
point_in_polygon,
points_in_spatial_window,
polygon_bounding_boxes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2023, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr

from cudf._lib.cpp.column.column cimport column

from cuspatial._lib.cpp.column.geometry_column_view cimport (
geometry_column_view,
)


cdef extern from "cuspatial/distance/point_polygon_distance.hpp" \
namespace "cuspatial" nogil:
cdef unique_ptr[column] pairwise_point_polygon_distance(
const geometry_column_view & multipoints,
const geometry_column_view & multipolygons
) except +
44 changes: 42 additions & 2 deletions python/cuspatial/cuspatial/_lib/distance.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Copyright (c) 2022, NVIDIA CORPORATION.
# Copyright (c) 2022-2023, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr
from libcpp.memory cimport make_shared, shared_ptr, unique_ptr
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.cpp.column.column cimport column
from cudf._lib.cpp.column.column_view cimport column_view

from cuspatial._lib.cpp.column.geometry_column_view cimport (
geometry_column_view,
)
from cuspatial._lib.cpp.distance.linestring_distance cimport (
pairwise_linestring_distance as c_pairwise_linestring_distance,
)
Expand All @@ -16,7 +19,12 @@ from cuspatial._lib.cpp.distance.point_distance cimport (
from cuspatial._lib.cpp.distance.point_linestring_distance cimport (
pairwise_point_linestring_distance as c_pairwise_point_linestring_distance,
)
from cuspatial._lib.cpp.distance.point_polygon_distance cimport (
pairwise_point_polygon_distance as c_pairwise_point_polygon_distance,
)
from cuspatial._lib.cpp.optional cimport optional
from cuspatial._lib.cpp.types cimport collection_type_id, geometry_type_id
from cuspatial._lib.types cimport collection_type_py_to_c
from cuspatial._lib.utils cimport unwrap_pyoptcol


Expand Down Expand Up @@ -102,3 +110,35 @@ def pairwise_point_linestring_distance(
c_linestring_points_xy,
))
return Column.from_unique_ptr(move(c_result))


def pairwise_point_polygon_distance(
point_collection_type,
Column multipoints,
Column multipolygons
):

cdef collection_type_id point_multi_type = collection_type_py_to_c(
point_collection_type
)

cdef shared_ptr[geometry_column_view] c_multipoints = \
make_shared[geometry_column_view](
multipoints.view(),
point_multi_type,
geometry_type_id.POINT)

cdef shared_ptr[geometry_column_view] c_multipolygons = \
make_shared[geometry_column_view](
multipolygons.view(),
collection_type_id.MULTI,
geometry_type_id.POLYGON)

cdef unique_ptr[column] c_result

with nogil:
c_result = move(c_pairwise_point_polygon_distance(
c_multipoints.get()[0], c_multipolygons.get()[0]
))

return Column.from_unique_ptr(move(c_result))
Loading