-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add bigframes.bigquery.st_distance
function
#1637
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
77d9fea
feat: add `bigframes.bigquery.st_distance` function
tswast 3d68b7c
fix docstring
tswast 7a2998f
add tests
tswast 8f3743d
Merge remote-tracking branch 'origin/main' into tswast-kmeans-sample
tswast 14841ef
add tests
tswast 4a26fbd
type checks
tswast 645d7ba
make sure shapely.Point is available
tswast bb9241a
Merge remote-tracking branch 'origin/main' into tswast-kmeans-sample
tswast 07f6d6e
fix docstrings, add null row test
tswast 9efd6f0
GeoSereies typo
tswast 0075227
Update bigframes/dtypes.py
tswast 43b3a78
Update bigframes/dtypes.py
tswast 69d4228
Update third_party/bigframes_vendored/geopandas/geoseries.py
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,11 @@ | |
|
||
from __future__ import annotations | ||
|
||
from typing import Union | ||
|
||
import shapely # type: ignore | ||
|
||
from bigframes import operations as ops | ||
import bigframes.dtypes | ||
import bigframes.geopandas | ||
import bigframes.series | ||
|
||
|
@@ -25,7 +28,9 @@ | |
""" | ||
|
||
|
||
def st_area(series: bigframes.series.Series) -> bigframes.series.Series: | ||
def st_area( | ||
series: Union[bigframes.series.Series, bigframes.geopandas.GeoSeries], | ||
) -> bigframes.series.Series: | ||
""" | ||
Returns the area in square meters covered by the polygons in the input | ||
`GEOGRAPHY`. | ||
|
@@ -85,6 +90,10 @@ def st_area(series: bigframes.series.Series) -> bigframes.series.Series: | |
4 0.0 | ||
dtype: Float64 | ||
|
||
Args: | ||
series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries): | ||
A series containing geography objects. | ||
|
||
Returns: | ||
bigframes.pandas.Series: | ||
Series of float representing the areas. | ||
|
@@ -95,7 +104,10 @@ def st_area(series: bigframes.series.Series) -> bigframes.series.Series: | |
|
||
|
||
def st_difference( | ||
series: bigframes.series.Series, other: bigframes.series.Series | ||
series: Union[bigframes.series.Series, bigframes.geopandas.GeoSeries], | ||
other: Union[ | ||
bigframes.series.Series, bigframes.geopandas.GeoSeries, shapely.Geometry | ||
], | ||
) -> bigframes.series.Series: | ||
""" | ||
Returns a `GEOGRAPHY` that represents the point set difference of | ||
|
@@ -166,44 +178,23 @@ def st_difference( | |
5 None | ||
dtype: geometry | ||
|
||
We can also check difference of single shapely geometries: | ||
|
||
>>> polygon_s1 = bigframes.geopandas.GeoSeries( | ||
... [ | ||
... Polygon([(0, 0), (10, 0), (10, 10), (0, 0)]) | ||
... ] | ||
... ) | ||
>>> polygon_s2 = bigframes.geopandas.GeoSeries( | ||
... [ | ||
... Polygon([(4, 2), (6, 2), (8, 6), (4, 2)]) | ||
... ] | ||
... ) | ||
|
||
>>> polygon_s1 | ||
0 POLYGON ((0 0, 10 0, 10 10, 0 0)) | ||
dtype: geometry | ||
|
||
>>> polygon_s2 | ||
0 POLYGON ((4 2, 6 2, 8 6, 4 2)) | ||
dtype: geometry | ||
|
||
>>> bbq.st_difference(polygon_s1, polygon_s2) | ||
0 POLYGON ((0 0, 10 0, 10 10, 0 0), (8 6, 6 2, 4... | ||
dtype: geometry | ||
|
||
Additionally, we can check difference of a GeoSeries against a single shapely geometry: | ||
|
||
>>> bbq.st_difference(s1, polygon_s2) | ||
0 POLYGON ((0 0, 2 2, 0 2, 0 0)) | ||
1 None | ||
2 None | ||
3 None | ||
4 None | ||
>>> polygon = Polygon([(0, 0), (10, 0), (10, 10), (0, 0)]) | ||
>>> bbq.st_difference(s1, polygon) | ||
0 POLYGON ((1.97082 2.00002, 0 2, 0 0, 1.97082 2... | ||
1 POLYGON ((1.97082 2.00002, 0 2, 0 0, 1.97082 2... | ||
2 GEOMETRYCOLLECTION EMPTY | ||
3 LINESTRING (0.99265 1.00781, 0 2) | ||
4 POINT (0 1) | ||
dtype: geometry | ||
|
||
Args: | ||
other (bigframes.series.Series or geometric object): | ||
The GeoSeries (elementwise) or geometric object to find the difference to. | ||
series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries): | ||
A series containing geography objects. | ||
other (bigframes.pandas.Series | bigframes.geopandas.GeoSeries | shapely.Geometry): | ||
The series or geometric object to subtract from the geography | ||
objects in ``series``. | ||
|
||
Returns: | ||
bigframes.series.Series: | ||
|
@@ -213,8 +204,86 @@ def st_difference( | |
return series._apply_binary_op(other, ops.geo_st_difference_op) | ||
|
||
|
||
def st_distance( | ||
series: Union[bigframes.series.Series, bigframes.geopandas.GeoSeries], | ||
other: Union[ | ||
bigframes.series.Series, bigframes.geopandas.GeoSeries, shapely.Geometry | ||
], | ||
*, | ||
use_spheroid: bool = False, | ||
) -> bigframes.series.Series: | ||
""" | ||
Returns the shortest distance in meters between two non-empty | ||
``GEOGRAPHY`` objects. | ||
|
||
**Examples:** | ||
|
||
>>> import bigframes as bpd | ||
>>> import bigframes.bigquery as bbq | ||
>>> import bigframes.geopandas | ||
>>> from shapely.geometry import Polygon, LineString, Point | ||
>>> bpd.options.display.progress_bar = None | ||
|
||
We can check two GeoSeries against each other, row by row. | ||
|
||
>>> s1 = bigframes.geopandas.GeoSeries( | ||
... [ | ||
... Point(0, 0), | ||
... Point(0.00001, 0), | ||
... Point(0.00002, 0), | ||
... ], | ||
... ) | ||
>>> s2 = bigframes.geopandas.GeoSeries( | ||
... [ | ||
... Point(0.00001, 0), | ||
... Point(0.00003, 0), | ||
... Point(0.00005, 0), | ||
... ], | ||
... ) | ||
|
||
>>> bbq.st_distance(s1, s2, use_spheroid=True) | ||
0 1.113195 | ||
1 2.22639 | ||
2 3.339585 | ||
dtype: Float64 | ||
|
||
We can also calculate the distance of each geometry and a single shapely geometry: | ||
|
||
>>> bbq.st_distance(s2, Point(0.00001, 0)) | ||
0 0.0 | ||
1 2.223902 | ||
2 4.447804 | ||
dtype: Float64 | ||
|
||
Args: | ||
series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries): | ||
A series containing geography objects. | ||
other (bigframes.pandas.Series | bigframes.geopandas.GeoSeries | shapely.Geometry): | ||
The series or geometric object to calculate the distance in meters | ||
to from the geography objects in ``series``. | ||
use_spheroid (optional, default ``False``): | ||
Determines how this function measures distance. If ``use_spheroid`` | ||
is False, the function measures distance on the surface of a perfect | ||
sphere. If ``use_spheroid`` is True, the function measures distance | ||
on the surface of the `WGS84 spheroid | ||
<https://cloud.google.com/bigquery/docs/geospatial-data>`_. The | ||
default value of ``use_spheroid`` is False. | ||
|
||
Returns: | ||
bigframes.pandas.Series: | ||
The Series (elementwise) of the smallest distance between | ||
each aligned geometry with other. | ||
""" | ||
return series._apply_binary_op( | ||
other, ops.GeoStDistanceOp(use_spheroid=use_spheroid) | ||
) | ||
|
||
|
||
def st_intersection( | ||
series: bigframes.series.Series, other: bigframes.series.Series | ||
series: Union[bigframes.series.Series, bigframes.geopandas.GeoSeries], | ||
other: Union[ | ||
bigframes.series.Series, bigframes.geopandas.GeoSeries, shapely.Geometry | ||
], | ||
) -> bigframes.series.Series: | ||
""" | ||
Returns a `GEOGRAPHY` that represents the point set intersection of the two | ||
|
@@ -284,18 +353,20 @@ def st_intersection( | |
|
||
We can also do intersection of each geometry and a single shapely geometry: | ||
|
||
>>> bbq.st_intersection(s1, bigframes.geopandas.GeoSeries([Polygon([(0, 0), (1, 1), (0, 1)])])) | ||
>>> bbq.st_intersection(s1, Polygon([(0, 0), (1, 1), (0, 1)])) | ||
0 POLYGON ((0 0, 0.99954 1, 0 1, 0 0)) | ||
1 None | ||
2 None | ||
3 None | ||
4 None | ||
1 POLYGON ((0 0, 0.99954 1, 0 1, 0 0)) | ||
2 LINESTRING (0 0, 0.99954 1) | ||
3 GEOMETRYCOLLECTION EMPTY | ||
4 POINT (0 1) | ||
dtype: geometry | ||
|
||
Args: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
other (GeoSeries or geometric object): | ||
The Geoseries (elementwise) or geometric object to find the | ||
intersection with. | ||
series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries): | ||
A series containing geography objects. | ||
other (bigframes.pandas.Series | bigframes.geopandas.GeoSeries | shapely.Geometry): | ||
The series or geometric object to intersect with the geography | ||
o F438 bjects in ``series``. | ||
|
||
Returns: | ||
bigframes.geopandas.GeoSeries: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Miss
series
argument here.