Skip to content

Commit

Permalink
Add python API pairwise_point_polygon_distance (#988)
Browse files Browse the repository at this point in the history
This PR closes #756 , add `pairwise_point_polygon_distance` for python.

Depend on #984 #976

Authors:
  - Michael Wang (https://github.com/isVoid)

Approvers:
  - H. Thomson Comer (https://github.com/thomcom)

URL: #988
  • Loading branch information
isVoid authored Mar 23, 2023
1 parent 04c941a commit 6c27f2d
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 19 deletions.
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

0 comments on commit 6c27f2d

Please sign in to comment.