-
Notifications
You must be signed in to change notification settings - Fork 16
[packages/calitp_data_analysis] Auth helpers for accessing GeoPandas geospatial data in Google Cloud Storage #4004
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
base: main
Are you sure you want to change the base?
Changes from all commits
2a6ec84
c6c303e
3d4006a
516bf11
92e492a
a2e1719
e2e6758
3c706ef
8fe14e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import gcsfs # type: ignore | ||
import geopandas # type: ignore | ||
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. Tiny nit - it's typical to use |
||
import google.auth # type: ignore | ||
Comment on lines
+1
to
+3
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. @vevetron How have you all typically been handling mypy errors like |
||
|
||
|
||
class GCSGeoPandas: | ||
""" | ||
GCSGeoPandas contains authentication helpers for interacting with Google Cloud Storage with GeoPandas | ||
""" | ||
|
||
def __init__(self): | ||
"""Sets instance token to credentials returned from Google auth request""" | ||
credentials, _ = google.auth.default() | ||
self.token = credentials | ||
|
||
def gcs_filesystem(self, **kwargs): | ||
"""Returns a Google Cloud Storage Filesystem""" | ||
return gcsfs.GCSFileSystem(token=self.token, **kwargs) | ||
|
||
def read_parquet(self, path, *args, **kwargs): | ||
"""Delegates to geopandas.read_parquet with storage option token | ||
|
||
Passes the auth credentials from Google auth as storage option token | ||
""" | ||
storage_options = kwargs.get("storage_options", {}) | {"token": self.token} | ||
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. I'm not sure whether this has the intended effect - if you do something like the below, you still get an error because it still passes
|
||
return geopandas.read_parquet( | ||
path, storage_options=storage_options, *args, **kwargs | ||
) | ||
|
||
def geo_data_frame_to_parquet(self, geo_data_frame, path, *args, **kwargs): | ||
"""Delegates to .to_parquet on the passed geo_data_frame, providing Google Cloud Storage file system | ||
|
||
Fetches gcsfs.GCSFileSystem instance | ||
|
||
Passes the auth credentials from Google Cloud Storage as storage option token | ||
""" | ||
gcs_filesystem = self.gcs_filesystem() | ||
return geo_data_frame.to_parquet( | ||
path, filesystem=gcs_filesystem, *args, **kwargs | ||
) |
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.
I made this match the Black version specified in toml files. Let me know if that's ok or not.