generated from kamangir/blue-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ingest refactors - kamangir/bolt#746
- Loading branch information
Showing
7 changed files
with
110 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ def help_ingest( | |
) | ||
|
||
args = [ | ||
"[--dryrun 1]", | ||
"[--overwrite 1]", | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,113 @@ | ||
from blueness import module | ||
from typing import Dict | ||
import requests | ||
import geopandas as gpd | ||
import pandas as pd | ||
from shapely.geometry import Point | ||
|
||
from blueness import module | ||
from blue_objects import host | ||
from blue_objects import metadata, file, objects | ||
from blue_geo import NAME | ||
from blue_geo.logger import logger | ||
|
||
NAME = module.name(__file__, NAME) | ||
|
||
url: Dict[str, str] = { | ||
"home": "https://datasets.wri.org/datasets/global-power-plant-database", | ||
"metadata": "https://datasets.wri.org/api/3/action/resource_show?id=66bcdacc-3d0e-46ad-9271-a5a76b1853d2", | ||
} | ||
|
||
|
||
def ingest( | ||
object_name: str, | ||
version: str = "v1", | ||
dryrun: bool = False, | ||
overwrite: bool = False, | ||
log: bool = True, | ||
verbose: bool = False, | ||
) -> bool: | ||
full_object_name = f"{object_name}-{version}" | ||
full_object_path = objects.object_path( | ||
full_object_name, | ||
create=True, | ||
) | ||
|
||
logger.info( | ||
"{}.ingest({}-{}) {}".format( | ||
"{}.ingest({}) {}".format( | ||
NAME, | ||
object_name, | ||
version, | ||
",".join( | ||
(["dryrun"] if dryrun else []) + (["overwrite"] if overwrite else []) | ||
), | ||
full_object_name, | ||
",".join((["overwrite"] if overwrite else [])), | ||
) | ||
) | ||
|
||
logger.info("🪄") | ||
response = requests.get(url["metadata"]) | ||
# https://chat.openai.com/c/6deb94d0-826a-48de-b5ef-f7d8da416c82 | ||
# response.raise_for_status() | ||
if response.status_code // 100 != 2: | ||
logger.error(f"failed to access metadata.") | ||
return False | ||
if verbose: | ||
logger.info(response.json()) | ||
|
||
return True | ||
download_url = response.json().get("result", {}).get("url", "") | ||
if not download_url: | ||
logger.error(f"failed to get download_url.") | ||
return False | ||
|
||
zip_filename = objects.path_of( | ||
download_url.split("/")[-1], | ||
full_object_name, | ||
) | ||
if not file.download( | ||
url=download_url, | ||
filename=zip_filename, | ||
log=log, | ||
overwrite=overwrite, | ||
): | ||
return False | ||
|
||
if not host.unzip(zip_filename, log=log): | ||
return False | ||
|
||
success, df = file.load_dataframe( | ||
objects.path_of( | ||
"global_power_plant_database.csv", | ||
full_object_name, | ||
), | ||
log=log, | ||
) | ||
if not success: | ||
return False | ||
|
||
df["latitude"] = pd.to_numeric(df["latitude"], errors="coerce") | ||
df["longitude"] = pd.to_numeric(df["longitude"], errors="coerce") | ||
|
||
df = df.dropna(subset=["latitude", "longitude"]) | ||
|
||
df["geometry"] = df.apply( | ||
lambda row: Point(row["longitude"], row["latitude"]), | ||
axis=1, | ||
) | ||
|
||
gdf = gpd.GeoDataFrame( | ||
df, | ||
geometry="geometry", | ||
crs="EPSG:4326", | ||
) | ||
|
||
if not file.save_geojson( | ||
objects.path_of( | ||
"global_power_plant_database.geojson", | ||
full_object_name, | ||
), | ||
gdf, | ||
log=log, | ||
): | ||
return False | ||
|
||
return metadata.post_to_object( | ||
full_object_name, | ||
"ingest", | ||
{ | ||
"api-response": response.json(), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters