File size: 3,456 Bytes
7e30528 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | """Export the 13-band input stack for each city from Google Earth Engine (free for research).
pip install earthengine-api pyyaml
earthengine authenticate
python scripts/gee_export.py --config config.yaml --project <your-gee-project> --cities mumbai pune
Bands: Sentinel-2 L2A dry-season median (B2 B3 B4 B5 B6 B7 B8 B8A B11 B12, reflectance x1e4)
+ Open Buildings 2.5D Temporal: building presence (mean), height (mean, m), count (sum)
Output: one GeoTIFF per city in Google Drive folder `slum_india` (UTM projection, 10 m).
"""
import argparse
import ee
import yaml
S2_BANDS = ["B2", "B3", "B4", "B5", "B6", "B7", "B8", "B8A", "B11", "B12"]
def utm_epsg(lon, lat):
zone = int((lon + 180) // 6) + 1
return f"EPSG:{32600 + zone if lat >= 0 else 32700 + zone}"
def s2_composite(region, start, end, cs_thresh):
s2 = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED").filterBounds(region).filterDate(start, end)
cs = ee.ImageCollection("GOOGLE/CLOUD_SCORE_PLUS/V1/S2_HARMONIZED")
s2 = s2.linkCollection(cs, ["cs_cdf"]).map(
lambda img: img.updateMask(img.select("cs_cdf").gte(cs_thresh)))
return s2.select(S2_BANDS).median().toFloat()
def buildings(region, year, crs):
col = (ee.ImageCollection("GOOGLE/Research/open-buildings-temporal/v1")
.filterBounds(region).filterDate(f"{year}-01-01", f"{year}-12-31"))
# mosaic() drops the native projection; restore it so reduceResolution knows the input scale
ob = col.mosaic().setDefaultProjection(col.first().projection())
proj = ee.Projection(crs).atScale(10)
pres = ob.select("building_presence").reduceResolution(ee.Reducer.mean(), maxPixels=1024).reproject(proj)
hgt = ob.select("building_height").reduceResolution(ee.Reducer.mean(), maxPixels=1024).reproject(proj)
# reduceResolution(sum) behaves like a mean here, so take the mean and multiply by the number of
# native 0.5 m cells per 10 m pixel (20 x 20 = 400) -> buildings per 10 m pixel
cnt = ob.select("building_fractional_count").reduceResolution(ee.Reducer.mean(), maxPixels=1024) \
.reproject(proj).multiply(400)
return ee.Image.cat([pres.rename("bldg_presence"), hgt.rename("bldg_height"),
cnt.rename("bldg_count")]).unmask(0).toFloat()
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--config", default="config.yaml")
ap.add_argument("--project", required=True, help="your Earth Engine cloud project id")
ap.add_argument("--cities", nargs="*")
ap.add_argument("--folder", default="slum_india")
a = ap.parse_args()
cfg = yaml.safe_load(open(a.config)); im = cfg["imagery"]
ee.Initialize(project=a.project)
for city in a.cities or cfg["cities"]:
x0, y0, x1, y1 = cfg["cities"][city]["bbox"]
region = ee.Geometry.Rectangle([x0, y0, x1, y1])
crs = utm_epsg((x0 + x1) / 2, (y0 + y1) / 2)
img = s2_composite(region, im["s2_start"], im["s2_end"], im["cloud_score_threshold"]) \
.addBands(buildings(region, im["buildings_year"], crs))
task = ee.batch.Export.image.toDrive(
image=img.clip(region), description=f"slum_{city}", folder=a.folder,
fileNamePrefix=city, region=region, scale=10, crs=crs, maxPixels=1e10,
fileFormat="GeoTIFF")
task.start()
print(f"started export for {city} ({crs}) -> Drive/{a.folder}/{city}.tif")
if __name__ == "__main__":
main()
|