Skip to content

Commit

Permalink
Add -m/--margin option to control bounds form data source extent. Closes
Browse files Browse the repository at this point in the history
 #14.
  • Loading branch information
alexamici committed Apr 2, 2016
1 parent e40e6da commit b2a251b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
0.9.10 (unreleased)
-------------------

- Added -r/--reference option to get bounds from a GDAL/OGR data source.
- Added ``-r/--reference`` and ``-m/--margin`` options to define the bounds from a GDAL/OGR data source.
Issue `#14 <https://github.com/bopen/elevation/issues/14>`_.


Expand Down
3 changes: 3 additions & 0 deletions elevation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def seed(ctx, **kwargs):
help="Path to output file. Existing files will be overwritten.")
@click.option('--bounds', nargs=4, type=float, default=None,
help='Output bounds: left bottom right top.')
@click.option('-m', '--margin', default=elevation.DEFAULT_MARGIN,
help="Decimal degree margin added to the bounds. Use '%%' for percent margin. "
"Defaults to %r" % elevation.DEFAULT_MARGIN)
@click.option('-r', '--reference',
help="Use the extent of a reference GDAL/OGR data source as output bounds.")
@click.pass_context
Expand Down
34 changes: 23 additions & 11 deletions elevation/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
# declare public all API functions and constants
__all__ = [
'info', 'seed', 'clip', 'clean', 'distclean',
'CACHE_DIR', 'DEFAULT_PRODUCT', 'PRODUCTS', 'DEFAULT_OUTPUT', 'MAKE_FLAGS', 'TOOLS',
'CACHE_DIR', 'DEFAULT_PRODUCT', 'PRODUCTS', 'DEFAULT_OUTPUT', 'MAKE_FLAGS', 'DEFAULT_MARGIN', 'TOOLS',
]

CACHE_DIR = appdirs.user_cache_dir('elevation', 'bopen')
MAKE_FLAGS = '-k'
DEFAULT_OUTPUT = 'out.tif'
DEFAULT_MARGIN = '10%'


def srtm1_tile_ilonlat(lon, lat):
Expand Down Expand Up @@ -140,23 +141,34 @@ def seed(cache_dir=CACHE_DIR, product=DEFAULT_PRODUCT, bounds=None, max_donwload
return datasource_root


def ensure_bounds(bounds, reference=None):
def make_bounds(reference, margin=DEFAULT_MARGIN):
# ASSUMPTION: rasterio and fiona bounds are given in geodetic WGS84 crs
try:
with rasterio.open(reference) as datasource:
left, bottom, right, top = datasource.bounds
except:
with fiona.open(reference) as datasource:
left, bottom, right, top = datasource.bounds
if margin.endswith('%'):
margin_percent = float(margin[:-1])
margin_lon = (right - left) * margin_percent / 100
margin_lat = (top - bottom) * margin_percent / 100
else:
margin_lon = margin_lat = float(margin)
return (left - margin_lon, bottom - margin_lat, right + margin_lon, top + margin_lat)


def ensure_bounds(bounds, reference=None, **kwargs):
if not bounds:
if not reference:
raise ValueError("bounds are not defined.")
else:
# ASSUMPTION: rasterio and fiona bounds are given in geodetic WGS84 crs
try:
with rasterio.open(reference) as datasource:
bounds = datasource.bounds
except:
with fiona.open(reference) as datasource:
bounds = datasource.bounds
bounds = make_bounds(reference, **kwargs)
return bounds


def clip(output=DEFAULT_OUTPUT, bounds=None, reference=None, **kwargs):
bounds = ensure_bounds(bounds, reference=reference)
def clip(output=DEFAULT_OUTPUT, bounds=None, reference=None, margin=DEFAULT_MARGIN, **kwargs):
bounds = ensure_bounds(bounds, reference=reference, margin=margin)
datasource_root = seed(bounds=bounds, **kwargs)
do_clip(datasource_root, bounds, output, **kwargs)

Expand Down

0 comments on commit b2a251b

Please sign in to comment.