Skip to content

Commit

Permalink
Fix vsizip extract error
Browse files Browse the repository at this point in the history
  • Loading branch information
bendnorman committed Sep 25, 2024
1 parent 30577ed commit 5f49401
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/dbcp/extract/fips_tables.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Extract canonical state and county FIPS tables from the addfips library."""

import shutil
import tempfile
from functools import lru_cache
from importlib.resources import files
from pathlib import Path
from typing import Dict

import addfips
Expand All @@ -14,6 +18,22 @@
TRIBAL_LANDS_URI = "gs://dgm-archive/census/tl_2021_us_aiannh.zip"


def extract_zipped_shapefile(path: Path) -> gpd.GeoDataFrame:
"""
Create a temporary file from a zipped shapefile and return a GeoDataFrame.
vsizip doesn't like the '#' in the path so my workaround is to copy the file to a temporary file.
Args:
path: path to zipped shapefile
Returns:
GeoDataFrame
"""
with tempfile.NamedTemporaryFile(delete=True, suffix=".zip") as temp_file:
shutil.copyfile(path, temp_file.name)
return gpd.read_file(temp_file.name)


@lru_cache(maxsize=1) # county boundaries are also used in some transform modules
def _extract_census_counties(census_uri: str) -> pd.DataFrame:
"""Extract canonical county FIPS tables from census data.
Expand All @@ -22,8 +42,7 @@ def _extract_census_counties(census_uri: str) -> pd.DataFrame:
census_uri: path to zipped shapefiles.
"""
path = dbcp.extract.helpers.cache_gcs_archive_file_locally(census_uri)
counties = gpd.read_file(path)
return counties
return extract_zipped_shapefile(path)


def extract_census_tribal_land(archive_uri: str) -> pd.DataFrame:
Expand All @@ -36,8 +55,7 @@ def extract_census_tribal_land(archive_uri: str) -> pd.DataFrame:
output dataframes of county-level info.
"""
path = dbcp.extract.helpers.cache_gcs_archive_file_locally(archive_uri)
counties = gpd.read_file(path)
return counties
return extract_zipped_shapefile(path)


def _extract_state_fips() -> pd.DataFrame:
Expand Down

0 comments on commit 5f49401

Please sign in to comment.