Date
1  5 of 5
Scaling Geometry to Match Image File
katsonandrew3.5@...
Hi,
I know this is not a fiona specific question but I figure since this kind of covers a variety of modules written by the same people (or at least understood) that I might get an answer here. I am attempting to do a mask over a population image file using geometries but they are in different projections and different scales. Here are my modules below. I am using this set of land boundaries: https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_0_boundary_lines_land.zip and this population image file: https://ghsl.jrc.ec.europa.eu/download.php?ds=pop (with the 250 m resolution) Here is how I read in the population file: world_pop_image = Here is how I do my reprojection: Below are all my python packages Python 3.6 with




katsonandrew3.5@...
I did not finish my question. I have typed the rest.
Here is how I read in the population file: world_pop_image = rasterio.open(path_to_image, nodata=0) Here is how I read the boundaries file: world_boundaries = gpd.read_file(path_to_boundaries) Here is how I do my reprojection (the raster is in World Mollweide which is not explicitly supported so I found the below workaround): world_boundaries.to_crs('+proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs') Here is how I do my scaling: from shapely.affinity import affine_transformation population_image_affine = world_pop_image.transform shapely_affine_repr = [population_image_affine.a, population_image_affine.b, population_image_affine.d, population_image_affine.e, population_image_affine.xoff, population_image_affine.yoff] world_boundaries['geometry'] = world_boundaries['geometry].apply(lambda geometry: affine_transformation(geometry, shapely_affine_repr)




katsonandrew3.5@...
I do not know if this will be helpful at all but this is how I am doing my masks and population:
population_image_slice_arr, new_image_transform = rasterio.mask.mask(world_pop_image, [polygon_for_some_country], crop=True, nodata=0) population_image_slice_arr[population_image_slice_arr < 0] = 0 total_population = population_image_slice_arr.sum() I know this is incorrect because when i did manual scaling using the bounds of the image and shapely.ops.transform the population for say India would be 800 million when it should be 1.3 billion but then when I changed it to use the affine matrix the math came out to 0.0 population.




katsonandrew3.5@...
In case this helps. Here is how I did scaling it to get more accurate numbers originally before I switched but this was even flawed:
world_total_bounds = world_boundaries.total_bounds population_image_total_bounds = world_pop_image.bounds x_scale = (population_image_total_bounds.right  population_image_total_bounds.left) / (world_total_bounds[MAX_X]  world_total_bounds[MIN_X]) y_scale = (population_image_total_bounds.top  population_image_total_bounds.bottom) / (world_total_bounds[MAX_Y]  world_total_bounds[MIN_Y]) world_boundaries['geometry'] = world_boundaries['geometry'].apply(lambda geometry: shapely.ops.transform(lambda x, y, z=None: (x * x_scale, y * y_scale), geometry))




katsonandrew3.5@...
Update:
I saw the response to getting it in WGS84 and I will try that in a bit. But I think the major issue is that it actually does not include antarctic and cuts off greenland as shown in this representation (http://www.luminocity3d.org/WorldPopDen/#3/45.89/7.73)


