|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from bigframes import operations as ops |
| 18 | +import bigframes.geopandas |
| 19 | +import bigframes.series |
| 20 | + |
| 21 | +""" |
| 22 | +Search functions defined from |
| 23 | +https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +def st_area(self) -> bigframes.series.Series: |
| 28 | + """ |
| 29 | + Returns the area in square meters covered by the polygons in the input |
| 30 | + GEOGRAPHY. |
| 31 | +
|
| 32 | + If geography_expression is a point or a line, returns zero. If |
| 33 | + geography_expression is a collection, returns the area of the polygons |
| 34 | + in the collection; if the collection doesn't contain polygons, returns zero. |
| 35 | +
|
| 36 | +
|
| 37 | + ..note:: |
| 38 | + BigQuery's Geography functions, like `st_area`, interpet the geomtry |
| 39 | + data type as a point set on the Earth's surface. A point set is a set |
| 40 | + of points, lines, and polygons on the WGS84 reference spheroid, with |
| 41 | + geodesic edges. See: https://cloud.google.com/bigquery/docs/geospatial-data |
| 42 | +
|
| 43 | +
|
| 44 | + **Examples:** |
| 45 | +
|
| 46 | + >>> import bigframes.geopandas |
| 47 | + >>> import bigframes.pandas as bpd |
| 48 | + >>> import bigframes.bigquery as bbq |
| 49 | + >>> from shapely.geometry import Polygon, LineString, Point |
| 50 | + >>> bpd.options.display.progress_bar = None |
| 51 | +
|
| 52 | + >>> series = bigframes.geopandas.GeoSeries( |
| 53 | + ... [ |
| 54 | + ... Polygon([(0.0, 0.0), (0.1, 0.1), (0.0, 0.1)]), |
| 55 | + ... Polygon([(0.10, 0.4), (0.9, 0.5), (0.10, 0.5)]), |
| 56 | + ... Polygon([(0.1, 0.1), (0.2, 0.1), (0.2, 0.2)]), |
| 57 | + ... LineString([(0, 0), (1, 1), (0, 1)]), |
| 58 | + ... Point(0, 1), |
| 59 | + ... ] |
| 60 | + ... ) |
| 61 | + >>> series |
| 62 | + 0 POLYGON ((0 0, 0.1 0.1, 0 0.1, 0 0)) |
| 63 | + 1 POLYGON ((0.1 0.4, 0.9 0.5, 0.1 0.5, 0.1 0.4)) |
| 64 | + 2 POLYGON ((0.1 0.1, 0.2 0.1, 0.2 0.2, 0.1 0.1)) |
| 65 | + 3 LINESTRING (0 0, 1 1, 0 1) |
| 66 | + 4 POINT (0 1) |
| 67 | + dtype: geometry |
| 68 | +
|
| 69 | + >>> bbq.st_area(series) |
| 70 | + 0 61821689.855985 |
| 71 | + 1 494563347.88721 |
| 72 | + 2 61821689.855841 |
| 73 | + 3 0.0 |
| 74 | + 4 0.0 |
| 75 | + dtype: Float64 |
| 76 | +
|
| 77 | + Use `round()` to round the outputed areas to the neares ten millions |
| 78 | +
|
| 79 | + >>> bbq.st_area(series).round(-7) |
| 80 | + 0 60000000.0 |
| 81 | + 1 490000000.0 |
| 82 | + 2 60000000.0 |
| 83 | + 3 0.0 |
| 84 | + 4 0.0 |
| 85 | + dtype: Float64 |
| 86 | +
|
| 87 | + Returns: |
| 88 | + bigframes.pandas.Series: |
| 89 | + Series of float representing the areas. |
| 90 | + """ |
| 91 | + series = self._apply_unary_op(ops.geo_area_op) |
| 92 | + series.name = None |
| 93 | + return series |
0 commit comments