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

Strict version of some dependencies and dataframe helper #5

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ packages = [{include = "utils"}]
python = ">=3.9, <3.13"

pnnl-buildingid = {git = "https://github.com/seed-platform/buildingid.git", rev = "master"}
usaddress = "^0.5.10"
street-address = "^0.4.0"
usaddress = "0.5.10"
street-address = "0.4.0"
geopandas = "^0.14.3"
mercantile = "^1.2.1"
python-dotenv = "^1.0.1"
Expand Down
2 changes: 1 addition & 1 deletion utils/normalize_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re

import usaddress
from streetaddress import StreetAddressFormatter
from streetaddress.streetaddress import StreetAddressFormatter


def _normalize_subaddress_type(subaddress_type):
Expand Down
40 changes: 40 additions & 0 deletions utils/ubid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

from buildingid.code import decode, encode
from geopandas import GeoDataFrame
from openlocationcode.openlocationcode import PAIR_CODE_LENGTH_
from shapely.geometry import Point, Polygon

Expand Down Expand Up @@ -36,3 +37,42 @@ def bounding_box(ubid: str) -> Polygon:
def centroid(ubid: str) -> Point:
code_area = decode(ubid)
return Point(code_area.centroid.longitudeCenter, code_area.centroid.latitudeCenter)


def add_ubid_to_geodataframe(
gdf: GeoDataFrame,
footprint_column: str = "geometry",
additional_ubid_columns_to_create: list[str] = ["ubid_centroid", "ubid_bbox"],
) -> GeoDataFrame:
"""Add UBID and related fields to a GeoDataFrame

Args:
gdf (GeoDataFrame): Incoming data frame
footprint_column (str, optional): Where is the footprint polygon defined. Defaults to "geometry".
additional_ubid_columns_to_create (list[str], optional): Which fields to create.
Defaults to ['ubid_centroid', 'ubid_bbox'].

Returns:
GeoDataFrame: New data frame with UBID and related fields
"""
# only apply to the fields that have footprints
filter_str = gdf[footprint_column].notna()

# make sure the columns are created
if "ubid" not in gdf.columns:
gdf["ubid"] = None

for column in additional_ubid_columns_to_create:
if column not in gdf.columns:
gdf[column] = None

# UBID is always calculated and stored in 'ubid'
gdf.loc[filter_str, "ubid"] = gdf[filter_str].apply(lambda x: encode_ubid(x[footprint_column]), axis=1)

if "ubid_centroid" in additional_ubid_columns_to_create:
gdf.loc[filter_str, "ubid_centroid"] = gdf[filter_str].apply(lambda x: centroid(x["ubid"]), axis=1)

if "ubid_bbox" in additional_ubid_columns_to_create:
gdf.loc[filter_str, "ubid_bbox"] = gdf[filter_str].apply(lambda x: bounding_box(x["ubid"]), axis=1)

return gdf