Skip to content

Commit

Permalink
implement diff res of wp data downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolepaul committed May 17, 2021
1 parent 75f3d0e commit b3bdb4f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions _config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@
# In the same units as the desired_crs (e.g. degrees for EPSG:4326)
# Cannot be equal to or less than the original resolution of the downloaded
# data
res = 0.05
res = 0.01

# Desired coordinate reference system
desired_crs = "EPSG:4326"

# Input shapefile parameters - field_name needs to exist in both the input
# shapefile and the input exposure CSV file. Directly replace field_name
# with the field of interest
adm_level = 1
adm_level = 3
field_name = f"ID_{adm_level}"

# Threshold for checking that sampled asset counts match original input
Expand Down Expand Up @@ -97,7 +97,7 @@
wp_directory = os.path.join("data", "worldpop")

# File locations - inputs
name = "Austria"
name = "Portugal"
shp_file = f"Adm{adm_level}_{name}.shp"

# Directory locations - outputs
Expand Down
Binary file modified data/worldpop/prt_ppp_2020.tif
Binary file not shown.
Binary file modified data/worldpop/resampled_prt_ppp_2020.tif
Binary file not shown.
13 changes: 9 additions & 4 deletions download_worldpop.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@


# Parse function with grid; FIXME: Support other resolutions from WP beyond 1km
def download_worldpop(iso, year=2020, res=1):
def download_worldpop(iso, year=2020, res="1km"):
''' This function downloads a raster file of population estimates from
WorldPop. At the moment, this will download the 2020 population estimate
at a 1km resolution for the desired country. This can be later expanded to
download the 100m dataset, or (where possible) a direct estimate of the
number of buildings.'''

# Arrange url
url = f'ftp://ftp.worldpop.org.uk/GIS/Population/Global_2000_{year}/{year}/{iso.upper()}/{iso.lower()}_ppp_{year}.tif'
# Arrange url; defeault - 1km, UN adj
url = f'ftp://ftp.worldpop.org.uk/GIS/Population/Global_2000_2020_1km_UNadj/{year}/{iso.upper()}/{iso.lower()}_ppp_2020_1km_Aggregated_UNadj.tif'
if res == "100m":
# If requested, use 100m, UN adj
url = f'ftp://ftp.worldpop.org.uk/GIS/Population/Global_2000_2020/{year}/{iso.upper()}/{iso.lower()}_ppp_{year}_UNadj.tif'

# Submit request
r = urllib.request.urlopen(url)
Expand All @@ -37,7 +40,9 @@ def download_worldpop(iso, year=2020, res=1):
# Parse arguments (group name)
iso = sys.argv[1]
year = 2020
res = 1
res = "1km"
if len(sys.argv) > 2:
res = sys.argv[2]

# Call function
download_worldpop(iso, year, res)
10 changes: 8 additions & 2 deletions util/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ def associate_grid_to_bounds(raster, adm_level, mapped_field,
if type(geom) == shapely.geometry.polygon.Polygon:
geom = [geom]
# Perform mask on raster data
mask_error = False
with rasterio.open(raster) as src:
out_image, out_transform = rasterio.mask.mask(src, geom, crop=True)
no_data = src.nodata
# Mask to boundary
try:
out_image, out_transform = rasterio.mask.mask(src, geom, crop=True)
no_data = src.nodata
except ValueError:
mask_error = True
print_yellow(f"Could not find raster data for {adm[mapped_field]}, will skip spatial disaggregation for that boundary")
# Extract data from masked image
data = out_image[0] # get first (only) band
# Remove nodata values
Expand Down

0 comments on commit b3bdb4f

Please sign in to comment.