1414
1515from __future__ import annotations
1616
17+ from typing import Union
18+
19+ import shapely # type: ignore
20+
1721from bigframes import operations as ops
18- import bigframes .dtypes
1922import bigframes .geopandas
2023import bigframes .series
2124
2528"""
2629
2730
28- def st_area (series : bigframes .series .Series ) -> bigframes .series .Series :
31+ def st_area (
32+ series : Union [bigframes .series .Series , bigframes .geopandas .GeoSeries ],
33+ ) -> bigframes .series .Series :
2934 """
3035 Returns the area in square meters covered by the polygons in the input
3136 `GEOGRAPHY`.
@@ -85,6 +90,10 @@ def st_area(series: bigframes.series.Series) -> bigframes.series.Series:
8590 4 0.0
8691 dtype: Float64
8792
93+ Args:
94+ series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries):
95+ A series containing geography objects.
96+
8897 Returns:
8998 bigframes.pandas.Series:
9099 Series of float representing the areas.
@@ -95,7 +104,10 @@ def st_area(series: bigframes.series.Series) -> bigframes.series.Series:
95104
96105
97106def st_difference (
98- series : bigframes .series .Series , other : bigframes .series .Series
107+ series : Union [bigframes .series .Series , bigframes .geopandas .GeoSeries ],
108+ other : Union [
109+ bigframes .series .Series , bigframes .geopandas .GeoSeries , shapely .Geometry
110+ ],
99111) -> bigframes .series .Series :
100112 """
101113 Returns a `GEOGRAPHY` that represents the point set difference of
@@ -166,44 +178,23 @@ def st_difference(
166178 5 None
167179 dtype: geometry
168180
169- We can also check difference of single shapely geometries:
170-
171- >>> polygon_s1 = bigframes.geopandas.GeoSeries(
172- ... [
173- ... Polygon([(0, 0), (10, 0), (10, 10), (0, 0)])
174- ... ]
175- ... )
176- >>> polygon_s2 = bigframes.geopandas.GeoSeries(
177- ... [
178- ... Polygon([(4, 2), (6, 2), (8, 6), (4, 2)])
179- ... ]
180- ... )
181-
182- >>> polygon_s1
183- 0 POLYGON ((0 0, 10 0, 10 10, 0 0))
184- dtype: geometry
185-
186- >>> polygon_s2
187- 0 POLYGON ((4 2, 6 2, 8 6, 4 2))
188- dtype: geometry
189-
190- >>> bbq.st_difference(polygon_s1, polygon_s2)
191- 0 POLYGON ((0 0, 10 0, 10 10, 0 0), (8 6, 6 2, 4...
192- dtype: geometry
193-
194181 Additionally, we can check difference of a GeoSeries against a single shapely geometry:
195182
196- >>> bbq.st_difference(s1, polygon_s2)
197- 0 POLYGON ((0 0, 2 2, 0 2, 0 0))
198- 1 None
199- 2 None
200- 3 None
201- 4 None
183+ >>> polygon = Polygon([(0, 0), (10, 0), (10, 10), (0, 0)])
184+ >>> bbq.st_difference(s1, polygon)
185+ 0 POLYGON ((1.97082 2.00002, 0 2, 0 0, 1.97082 2...
186+ 1 POLYGON ((1.97082 2.00002, 0 2, 0 0, 1.97082 2...
187+ 2 GEOMETRYCOLLECTION EMPTY
188+ 3 LINESTRING (0.99265 1.00781, 0 2)
189+ 4 POINT (0 1)
202190 dtype: geometry
203191
204192 Args:
205- other (bigframes.series.Series or geometric object):
206- The GeoSeries (elementwise) or geometric object to find the difference to.
193+ series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries):
194+ A series containing geography objects.
195+ other (bigframes.pandas.Series | bigframes.geopandas.GeoSeries | shapely.Geometry):
196+ The series or geometric object to subtract from the geography
197+ objects in ``series``.
207198
208199 Returns:
209200 bigframes.series.Series:
@@ -213,8 +204,86 @@ def st_difference(
213204 return series ._apply_binary_op (other , ops .geo_st_difference_op )
214205
215206
207+ def st_distance (
208+ series : Union [bigframes .series .Series , bigframes .geopandas .GeoSeries ],
209+ other : Union [
210+ bigframes .series .Series , bigframes .geopandas .GeoSeries , shapely .Geometry
211+ ],
212+ * ,
213+ use_spheroid : bool = False ,
214+ ) -> bigframes .series .Series :
215+ """
216+ Returns the shortest distance in meters between two non-empty
217+ ``GEOGRAPHY`` objects.
218+
219+ **Examples:**
220+
221+ >>> import bigframes as bpd
222+ >>> import bigframes.bigquery as bbq
223+ >>> import bigframes.geopandas
224+ >>> from shapely.geometry import Polygon, LineString, Point
225+ >>> bpd.options.display.progress_bar = None
226+
227+ We can check two GeoSeries against each other, row by row.
228+
229+ >>> s1 = bigframes.geopandas.GeoSeries(
230+ ... [
231+ ... Point(0, 0),
232+ ... Point(0.00001, 0),
233+ ... Point(0.00002, 0),
234+ ... ],
235+ ... )
236+ >>> s2 = bigframes.geopandas.GeoSeries(
237+ ... [
238+ ... Point(0.00001, 0),
239+ ... Point(0.00003, 0),
240+ ... Point(0.00005, 0),
241+ ... ],
242+ ... )
243+
244+ >>> bbq.st_distance(s1, s2, use_spheroid=True)
245+ 0 1.113195
246+ 1 2.22639
247+ 2 3.339585
248+ dtype: Float64
249+
250+ We can also calculate the distance of each geometry and a single shapely geometry:
251+
252+ >>> bbq.st_distance(s2, Point(0.00001, 0))
253+ 0 0.0
254+ 1 2.223902
255+ 2 4.447804
256+ dtype: Float64
257+
258+ Args:
259+ series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries):
260+ A series containing geography objects.
261+ other (bigframes.pandas.Series | bigframes.geopandas.GeoSeries | shapely.Geometry):
262+ The series or geometric object to calculate the distance in meters
263+ to from the geography objects in ``series``.
264+ use_spheroid (optional, default ``False``):
265+ Determines how this function measures distance. If ``use_spheroid``
266+ is False, the function measures distance on the surface of a perfect
267+ sphere. If ``use_spheroid`` is True, the function measures distance
268+ on the surface of the `WGS84 spheroid
269+ <https://cloud.google.com/bigquery/docs/geospatial-data>`_. The
270+ default value of ``use_spheroid`` is False.
271+
272+ Returns:
273+ bigframes.pandas.Series:
274+ The Series (elementwise) of the smallest distance between
275+ each aligned geometry with other.
276+ """
277+ return series ._apply_binary_op (
278+ other , ops .GeoStDistanceOp (use_spheroid = use_spheroid )
279+ )
280+
281+
216282def st_intersection (
217- series : bigframes .series .Series , other : bigframes .series .Series
283+ series : Union [bigframes .series .Series , bigframes .geopandas .GeoSeries ],
284+ other : Union [
285+ bigframes .series .Series , bigframes .geopandas .GeoSeries , shapely .Geometry
286+ ],
218287) -> bigframes .series .Series :
219288 """
220289 Returns a `GEOGRAPHY` that represents the point set intersection of the two
@@ -284,18 +353,20 @@ def st_intersection(
284353
285354 We can also do intersection of each geometry and a single shapely geometry:
286355
287- >>> bbq.st_intersection(s1, bigframes.geopandas.GeoSeries([ Polygon([(0, 0), (1, 1), (0, 1)] )]))
356+ >>> bbq.st_intersection(s1, Polygon([(0, 0), (1, 1), (0, 1)]))
288357 0 POLYGON ((0 0, 0.99954 1, 0 1, 0 0))
289- 1 None
290- 2 None
291- 3 None
292- 4 None
358+ 1 POLYGON ((0 0, 0.99954 1, 0 1, 0 0))
359+ 2 LINESTRING (0 0, 0.99954 1)
360+ 3 GEOMETRYCOLLECTION EMPTY
361+ 4 POINT (0 1)
293362 dtype: geometry
294363
295364 Args:
296- other (GeoSeries or geometric object):
297- The Geoseries (elementwise) or geometric object to find the
298- intersection with.
365+ series (bigframes.pandas.Series | bigframes.geopandas.GeoSeries):
366+ A series containing geography objects.
367+ other (bigframes.pandas.Series | bigframes.geopandas.GeoSeries | shapely.Geometry):
368+ The series or geometric object to intersect with the geography
369+ objects in ``series``.
299370
300371 Returns:
301372 bigframes.geopandas.GeoSeries:
0 commit comments