Commit 80c78fb8 authored by Nicolas Garcia Ospina's avatar Nicolas Garcia Ospina
Browse files

Changed babelgrid for mercantile in polyfill

parent ccef3a2c
Pipeline #26277 passed with stage
in 3 minutes and 13 seconds
......@@ -20,7 +20,7 @@ import logging
import sys
import geopandas as gpd
import argparse
from babelgrid import Babel
import mercantile
from multiprocessing import Pool
import numpy as np
from shapely.geometry import Polygon
......@@ -75,15 +75,14 @@ args = parser.parse_args()
# Get a country polygon by name from the GADM dataset and do small buffer
country_name = args.country.replace("_", " ")
print(country_name)
world = gpd.read_file(
os.path.join("..", os.path.dirname(__file__), "data", "gadm36_0.shp")
).to_crs("epsg:4326")
logger.info(country_name)
country = world[world["NAME_0"] == country_name]
logger.info("Setting a buffer on target geometry")
geom = country.unary_union.buffer(0.05)
del world, country
geom = country.unary_union
del world
country = gpd.GeoDataFrame(crs="epsg:4326", geometry=[geom])
# Build the batches from griding the input polygon
......@@ -111,7 +110,9 @@ for x in cols:
grid = gpd.GeoDataFrame({"geometry": polygons}, crs="epsg:4326")
gridded_polygon = gpd.overlay(grid, country, how="intersection")
batch_list = gridded_polygon.geometry.tolist()
logger.info("{} to be processed".format(len(batch_list)))
logger.info(
"{} batches to be processed with {} parallel jobs".format(len(batch_list), args.n_cores)
)
def get_tiles_from_polygon(in_geometry):
......@@ -123,34 +124,26 @@ def get_tiles_from_polygon(in_geometry):
Returns:
tiles_ids (list): List of quadkeys as strings.
"""
in_geometry = in_geometry.buffer(0.01)
tiles = []
if in_geometry.geom_type == "Polygon":
try:
tiles.extend(Babel("bing").polyfill(in_geometry, resolution=args.zoom))
except ZeroDivisionError:
logger.info(
"No quadkeys with zoom level {} fit the geometry {}.".format(
args.zoom, in_geometry
)
)
sw_lon, sw_lat, ne_lon, ne_lat = in_geometry.bounds
tiles_generator = mercantile.tiles(sw_lon, sw_lat, ne_lon, ne_lat, args.zoom)
tiles.extend([mercantile.quadkey(tile) for tile in tiles_generator])
elif in_geometry.geom_type == "MultiPolygon":
geometries = list(in_geometry)
for geometry in geometries:
try:
tiles.extend(Babel("bing").polyfill(geometry, resolution=args.zoom))
except ZeroDivisionError:
logger.info(
"Quadkeys with zoom level {} are bigger than the geometry {}".format(
args.zoom, in_geometry
)
)
sw_lon, sw_lat, ne_lon, ne_lat = geometry.bounds
tiles_generator = mercantile.tiles(sw_lon, sw_lat, ne_lon, ne_lat, args.zoom)
tiles.extend([mercantile.quadkey(tile) for tile in tiles_generator])
else:
raise IOError("Input is not a geometry.")
raise IOError("Input is not a valid geometry.")
if len(tiles) > 0:
tiles_ids = [tile.to_dict()["tile_id"] for tile in tiles]
logger.info("Found {} tiles in batch".format(len(tiles_ids)))
return tiles_ids
logger.info("Found {} tiles in batch".format(len(tiles)))
return tiles
else:
logger.info("No quadkeys found in batch")
return tiles
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment