Skip to content

Commit

Permalink
Add --same-as CLI option as per #14. Add rasterio e and fiona depende…
Browse files Browse the repository at this point in the history
…ncies (and drop PyPy support for now).
  • Loading branch information
alexamici committed Apr 2, 2016
1 parent d124778 commit 9b54c64
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ language: python
python: 3.5
env:
- TOX_ENV=quality-check
- TOX_ENV=pypy
- TOX_ENV=py35
- TOX_ENV=py34
- TOX_ENV=py33
Expand Down
1 change: 1 addition & 0 deletions elevation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ 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('--same-as', help="Use the extent of a GDAL/OGR supported data source as output bounds.")
@click.pass_context
def clip(ctx, **kwargs):
if ctx.parent and ctx.parent.params:
Expand Down
26 changes: 22 additions & 4 deletions elevation/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import pkgutil

import appdirs
import rasterio
import fiona

from . import util

Expand Down Expand Up @@ -119,11 +121,11 @@ def ensure_setup(cache_dir, product, force=True):
return datasource_root, spec


def do_clip(path, bounds, output, **kwargs):
def do_clip(path, bounds, output, make_flags='', **kwargs):
left, bottom, right, top = bounds
projwin = '%s %s %s %s' % (left, top, right, bottom)
variables_items = [('output', output), ('projwin', projwin)]
return util.check_call_make(path, targets=['clip'], variables=variables_items, **kwargs)
return util.check_call_make(path, targets=['clip'], variables=variables_items, make_flags=make_flags)


def seed(cache_dir=CACHE_DIR, product=DEFAULT_PRODUCT, bounds=None, max_donwload_tiles=9, **kwargs):
Expand All @@ -138,8 +140,24 @@ def seed(cache_dir=CACHE_DIR, product=DEFAULT_PRODUCT, bounds=None, max_donwload
return datasource_root


def clip(cache_dir=CACHE_DIR, product=DEFAULT_PRODUCT, bounds=None, output=DEFAULT_OUTPUT, **kwargs):
datasource_root = seed(cache_dir, product, bounds, **kwargs)
def ensure_bounds(bounds, datasourcename=None):
if not bounds:
if not datasourcename:
raise ValueError("bounds are not defined.")
else:
# ASSUMPTION: rasterio and fiona bounds are given in geodetic WGS84 crs
try:
with rasterio.open(datasourcename) as datasource:
bounds = datasource.bounds
except:
with fiona.open(datasourcename) as datasource:
bounds = datasource.bounds
return bounds


def clip(output=DEFAULT_OUTPUT, bounds=None, same_as=None, **kwargs):
bounds = ensure_bounds(bounds, datasourcename=same_as)
datasource_root = seed(bounds=bounds, **kwargs)
do_clip(datasource_root, bounds, output, **kwargs)


Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
future==0.15.2
appdirs==1.4.0
rasterio==0.33.0
Fiona==1.6.3.post1
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def read(fname):
'appdirs',
'fasteners',
'click',
'rasterio',
'fiona',
],
zip_safe=True,
classifiers=[
Expand All @@ -58,7 +60,6 @@ def read(fname):
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Operating System :: OS Independent',
'License :: OSI Approved :: Apache Software License',
'Topic :: Scientific/Engineering :: GIS',
Expand Down

0 comments on commit 9b54c64

Please sign in to comment.