|
1 | 1 | from pathlib import Path
|
2 | 2 | import json
|
3 | 3 | import geopandas
|
| 4 | +import pandas as pd |
4 | 5 | from datetime import datetime
|
5 |
| -from shapely.geometry import Polygon |
| 6 | +from shapely.geometry import Polygon, Point |
6 | 7 | from cropharvest.columns import RequiredColumns, NullableColumns
|
| 8 | +from cropharvest.config import EXPORT_END_MONTH, EXPORT_END_DAY |
7 | 9 |
|
8 | 10 | from .utils import export_date_from_row
|
9 | 11 | from ..utils import DATASET_PATH
|
|
18 | 20 | "Safflower": "oilseeds",
|
19 | 21 | "White Sorghum": "cereals",
|
20 | 22 | "Yellow Maize": "cereals",
|
| 23 | + "rice": "cereals", |
| 24 | + "maize": "cereals", |
21 | 25 | }
|
22 | 26 |
|
23 | 27 |
|
| 28 | +def convert_date(date_str): |
| 29 | + date_str = date_str.split("T")[0] |
| 30 | + date_str = date_str.split("-") |
| 31 | + year = date_str[0] |
| 32 | + month = date_str[1] |
| 33 | + day = date_str[2] |
| 34 | + return datetime(int(year), int(month), int(day)) |
| 35 | + |
| 36 | + |
24 | 37 | def _load_single_stac(path_to_stac: Path) -> List[Tuple[Polygon, str, datetime, datetime]]:
|
25 | 38 | with (path_to_stac / "labels.geojson").open("r") as f:
|
26 | 39 | label_json = json.load(f)
|
@@ -85,3 +98,73 @@ def load_tanzania():
|
85 | 98 | df = df.reset_index(drop=True)
|
86 | 99 | df[RequiredColumns.INDEX] = df.index
|
87 | 100 | return df
|
| 101 | + |
| 102 | + |
| 103 | +def load_tanzania_ecaas(): |
| 104 | + |
| 105 | + ecaas_files = (DATASET_PATH / "tanzania" / "tanzania_rice_ecaas").glob("*.csv") |
| 106 | + |
| 107 | + gdfs: List[geopandas.GeoDataFrame] = [] |
| 108 | + for file_path in ecaas_files: |
| 109 | + gdf = geopandas.GeoDataFrame(crs="EPSG:4326") |
| 110 | + df = pd.read_csv(file_path) |
| 111 | + |
| 112 | + # replace NaN with Rice |
| 113 | + df["consent_given/field_planted/primary_crop"].fillna("rice", inplace=True) |
| 114 | + # lat and long |
| 115 | + gdf[RequiredColumns.LAT] = df["consent_given/_field_center_latitude"] |
| 116 | + gdf[RequiredColumns.LON] = df["consent_given/_field_center_longitude"] |
| 117 | + gdf[RequiredColumns.GEOMETRY] = gdf.apply( |
| 118 | + lambda row: Point(row[RequiredColumns.LON], row[RequiredColumns.LAT]), axis=1 |
| 119 | + ) |
| 120 | + # collection date |
| 121 | + gdf[RequiredColumns.COLLECTION_DATE] = df["end"].apply(convert_date) |
| 122 | + |
| 123 | + # export date |
| 124 | + gdf[RequiredColumns.EXPORT_END_DATE] = datetime(2022, EXPORT_END_MONTH, EXPORT_END_DAY) |
| 125 | + |
| 126 | + # label and classification label |
| 127 | + gdf[NullableColumns.LABEL] = df["consent_given/field_planted/primary_crop"] |
| 128 | + gdf[NullableColumns.CLASSIFICATION_LABEL] = gdf.apply( |
| 129 | + lambda row: LABEL_TO_CLASSIFICATION[row[NullableColumns.LABEL]], axis=1 |
| 130 | + ) |
| 131 | + # manual inputs |
| 132 | + gdf[RequiredColumns.IS_CROP] = 1 |
| 133 | + # fill the NANs in the harvest and planting date columns with one of their values |
| 134 | + df["consent_given/field_planted/planting_date"].fillna( |
| 135 | + "2022-01-20T00:00:00.000+03:00", inplace=True |
| 136 | + ) |
| 137 | + |
| 138 | + df["consent_given/field_planted/harvesting_date"].fillna( |
| 139 | + "2022-05-01T00:00:00.000+03:00", inplace=True |
| 140 | + ) |
| 141 | + gdf[NullableColumns.HARVEST_DATE] = df[ |
| 142 | + "consent_given/field_planted/harvesting_date" |
| 143 | + ].apply(convert_date) |
| 144 | + gdf[NullableColumns.PLANTING_DATE] = df["consent_given/field_planted/planting_date"].apply( |
| 145 | + convert_date |
| 146 | + ) |
| 147 | + |
| 148 | + gdfs.append(gdf) |
| 149 | + |
| 150 | + df = pd.concat(gdfs) |
| 151 | + |
| 152 | + df = df.groupby([RequiredColumns.LON, RequiredColumns.LAT]).agg( |
| 153 | + { |
| 154 | + RequiredColumns.LAT: "first", |
| 155 | + RequiredColumns.LON: "first", |
| 156 | + RequiredColumns.GEOMETRY: "first", |
| 157 | + RequiredColumns.COLLECTION_DATE: "first", |
| 158 | + RequiredColumns.EXPORT_END_DATE: "first", |
| 159 | + NullableColumns.LABEL: "first", |
| 160 | + NullableColumns.CLASSIFICATION_LABEL: "first", |
| 161 | + RequiredColumns.IS_CROP: "first", |
| 162 | + NullableColumns.HARVEST_DATE: "first", |
| 163 | + NullableColumns.PLANTING_DATE: "first", |
| 164 | + } |
| 165 | + ) |
| 166 | + |
| 167 | + df = df.reset_index(drop=True) |
| 168 | + df[RequiredColumns.INDEX] = df.index |
| 169 | + |
| 170 | + return df |
0 commit comments