Skip to content

Commit

Permalink
Move optional spatial support to own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexamici committed Apr 3, 2016
1 parent d10822c commit 72552d8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
27 changes: 7 additions & 20 deletions elevation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

import elevation
from . import util
# NOTE: GDAL/OGR bindings are not supported on Pypy so the -r/--reference option is disabled
try:
from .spatial import import_bounds
except ImportError:
def import_bounds(reference):
raise click.BadOptionUsage("-r/--reference disabled, to enable it install rasterio and fiona.")


# disable overzealous warning
Expand Down Expand Up @@ -69,31 +75,12 @@ def seed(**kwargs):
elevation.seed(**kwargs)


try:
# NOTE: GDAL/OGR bindings are not supported on Pypy
import rasterio
import fiona

def import_bounds(reference):
# 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
return bounds
except ImportError:
def import_bounds(reference):
raise click.BadOptionUsage("-r/--reference disabled, to enable it install rasterio and fiona.")


def ensure_bounds(wrapped):
@functools.wraps(wrapped)
def wrapper(bounds, reference, **kwargs):
if not bounds:
if not reference:
raise ValueError("bounds are not defined.")
raise click.BadOptionUsage("One of --bounds or --reference must be supplied.")
else:
bounds = import_bounds(reference)
return wrapped(bounds=bounds, **kwargs)
Expand Down
32 changes: 32 additions & 0 deletions elevation/spatial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 B-Open Solutions srl - http://bopen.eu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# python 2 support via python-future
from __future__ import absolute_import, division, print_function, unicode_literals

import rasterio
import fiona


def import_bounds(reference):
# 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
return bounds

0 comments on commit 72552d8

Please sign in to comment.