Skip to content

Commit

Permalink
Merge pull request #1 from mapbox/its-a-setup
Browse files Browse the repository at this point in the history
It's a setup
  • Loading branch information
dnomadb authored Aug 3, 2016
2 parents 1ebee1d + 6f43f13 commit 1eb8f65
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 1 deletion.
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# OS X
.DS_Store

# Pyenv
.python-version

.*.swp

docs/notebooks/.ipynb_checkpoints/*
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
language: python
sudo: false
cache:
directories:
- ~/.cache/pip
env:
global:
- PIP_WHEEL_DIR=$HOME/.cache/pip/wheels
- PIP_FIND_LINKS=file://$HOME/.cache/pip/wheels
addons:
apt:
packages:
- libgdal1h
- gdal-bin
- libgdal-dev
- libatlas-dev
- libatlas-base-dev
- gfortran
python:
- "2.7"
- "3.4"
- "3.5"
before_install:
- "pip install -U pip"
- "pip install wheel"
install:
- "pip wheel numpy"
- "pip install --use-wheel numpy"
- "pip wheel -r requirements.txt"
- "pip install --use-wheel -r requirements.txt"
- "pip wheel -r requirements-dev.txt"
- "pip install --use-wheel -r requirements-dev.txt"
- "pip install -e .[test]"
script:
- py.test --cov rio_rgbify --cov-report term-missing
after_success:
- coveralls

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2016 Mapbox

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
# rio-rgbify
Encoded arbitrary bit depth rasters in psuedo base-256
Encode arbitrary bit depth rasters in psuedo base-256 as RGB

[![Build Status](https://travis-ci.org/mapbox/rio-rgbify.svg)](https://travis-ci.org/mapbox/rio-rgbify)[![Coverage Status](https://coveralls.io/repos/github/mapbox/rio-rgbify/badge.svg?branch=its-a-setup)](https://coveralls.io/github/mapbox/rio-rgbify)

## CLI usage

```
Usage: rio rgbify [OPTIONS] SRC_PATH DST_PATH
Options:
-b, --base-val FLOAT The base value of which to base the output encoding
on [DEFAULT=0]
-i, --interval FLOAT Describes the precision of the output,by incrementing
interval [DEFAULT=1]
--bidx INTEGER Band to encode [DEFAULT=1]
-j, --workers INTEGER Workers to run [DEFAULT=4]
-v, --verbose
--co NAME=VALUE Driver specific creation options.See the
documentation for the selected output driver for more
information.
--help Show this message and exit.
```
11 changes: 11 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
affine
cligj
coveralls>=0.4
delocate
enum34
numpy>=1.8.0
snuggs>=1.2
pytest
raster-tester
setuptools>=0.9.8
wheel
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
click
rasterio
rio-mucho>=0.2.1
6 changes: 6 additions & 0 deletions rio_rgbify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import logging

__version__ = "0.1.0"

log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
62 changes: 62 additions & 0 deletions rio_rgbify/encoders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from __future__ import division
import numpy as np

def data_to_rgb(data, baseval, interval):
'''
Given an arbitrary (rows x cols) ndarray,
encode the data into uint8 RGB from an arbitrary
base and interval
Parameters
-----------
data: ndarray
(rows x cols) ndarray of data to encode
baseval: float
the base value of the RGB numbering system.
will be treated as zero for this encoding
interval: float
the interval at which to encode
Returns
--------
ndarray: rgb data
a uint8 (3 x rows x cols) ndarray with the
data encoded
'''
data = data.astype(np.float64)
data -= baseval
data /= interval

rows, cols = data.shape

datarange = data.max() - data.min()

if _range_check(datarange):
raise ValueError('Data of {} larger than 256 ** 3'.format(datarange))

rgb = np.zeros((3, rows, cols), dtype=np.uint8)

rgb[2] = (((data / 256) - (data // 256)) * 256)
rgb[1] = ((((data // 256) / 256) - ((data // 256) // 256)) * 256)
rgb[0] = (((((data // 256) // 256) / 256) - (((data // 256) // 256) // 256)) * 256)

return rgb


def _decode(data, base, interval):
'''
Utility to decode RGB encoded data
'''
data = data.astype(np.float64)
return base + (((data[0] * 256 * 256) + (data[1] * 256) + data[2]) * interval)


def _range_check(datarange):
'''
Utility to check if data range is outside of precision for 3 digit base 256
'''
maxrange = 256 ** 3

return datarange > maxrange


Empty file added rio_rgbify/scripts/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions rio_rgbify/scripts/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import click

import rasterio as rio
import numpy as np
from riomucho import RioMucho
from rasterio.rio.options import creation_options

from rio_rgbify.encoders import data_to_rgb

def _rgb_worker(data, window, ij, g_args):
return data_to_rgb(data[0][g_args['bidx'] - 1],
g_args['base_val'],
g_args['interval'])

@click.command('rgbify')
@click.argument('src_path', type=click.Path(exists=True))
@click.argument('dst_path', type=click.Path(exists=False))
@click.option('--base-val', '-b', type=float, default=0,
help='The base value of which to base the output encoding on [DEFAULT=0]')
@click.option('--interval', '-i', type=float, default=1,
help='Describes the precision of the output,by incrementing interval [DEFAULT=1]')
@click.option('--bidx', type=int, default=1,
help='Band to encode [DEFAULT=1]')
@click.option('--workers', '-j', type=int, default=4,
help='Workers to run [DEFAULT=4]')
@click.option('--verbose', '-v', is_flag=True, default=False)
@click.pass_context
@creation_options
def rgbify(ctx, src_path, dst_path, base_val, interval, bidx, workers, verbose, creation_options):
with rio.open(src_path) as src:
meta = src.profile.copy()

meta.update(
count=3,
dtype=np.uint8
)

for c in creation_options:
meta[c] = creation_options[c]

gargs = {
'interval': interval,
'base_val': base_val,
'bidx': bidx
}

with RioMucho([src_path], dst_path, _rgb_worker,
options=meta,
global_args=gargs) as rm:

rm.run(workers)
41 changes: 41 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import sys
from setuptools import setup, find_packages
from setuptools.extension import Extension

# Parse the version from the fiona module.
with open('rio_rgbify/__init__.py') as f:
for line in f:
if line.find("__version__") >= 0:
version = line.split("=")[1].strip()
version = version.strip('"')
version = version.strip("'")
break

long_description = """"""


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(name='rio-rgbify',
version=version,
description=u"Encode arbitrary bit depth rasters in psuedo base-256 as RGB",
long_description=long_description,
classifiers=[],
keywords='',
author=u"Damon Burgett",
author_email='damon@mapbox.com',
url='https://github.com/mapbox/rio-rgbify',
license='BSD',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
zip_safe=False,
install_requires=["click", "rasterio", "rio-mucho"],
extras_require={
'test': ['pytest', 'pytest-cov', 'codecov']},
entry_points="""
[rasterio.rio_plugins]
toa=rio_rgbify.scripts.cli:rgbify
"""
)
Binary file added test/expected/elev-rgb.tif
Binary file not shown.
Binary file added test/fixtures/elev.tif
Binary file not shown.
66 changes: 66 additions & 0 deletions test/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from click.testing import CliRunner
import rasterio as rio
import numpy as np
from rio_rgbify.scripts.cli import rgbify
import click

from tempfile import mkdtemp
from shutil import rmtree
import os

from raster_tester.compare import affaux, upsample_array

class TestingDir:
def __init__(self):
self.tmpdir = mkdtemp()
def __enter__(self):
return self
def __exit__(self, a, b, c):
rmtree(self.tmpdir)
def mkpath(self, filename):
return os.path.join(self.tmpdir, filename)

def flex_compare(r1, r2, thresh=10):
upsample = 4
r1 = r1[::upsample]
r2 = r2[::upsample]
toAff, frAff = affaux(upsample)
r1 = upsample_array(r1, upsample, frAff, toAff)
r2 = upsample_array(r2, upsample, frAff, toAff)
tdiff = np.abs(r1.astype(np.float64) - r2.astype(np.float64))

click.echo('{0} values exceed the threshold difference with a max variance of {1}'.format(
np.sum(tdiff > thresh), tdiff.max()), err=True)

return not np.any(tdiff > thresh)


def test_cli_good_elev():
in_elev_src = 'test/fixtures/elev.tif'
expected_src = 'test/expected/elev-rgb.tif'
with TestingDir() as tmpdir:
out_rgb_src = tmpdir.mkpath('rgb.tif')

runner = CliRunner()
result = runner.invoke(rgbify, [in_elev_src, out_rgb_src, '--interval', 0.001, '--base-val', -100])

assert result.exit_code == 0

with rio.open(out_rgb_src) as created:
with rio.open(expected_src) as expected:
carr = created.read()
earr = expected.read()
for a, b in zip(carr, earr):
assert flex_compare(a, b)


def test_cli_fail_elev():
in_elev_src = 'test/fixtures/elev.tif'
expected_src = 'test/expected/elev-rgb.tif'
with TestingDir() as tmpdir:
out_rgb_src = tmpdir.mkpath('rgb.tif')

runner = CliRunner()
result = runner.invoke(rgbify, [in_elev_src, out_rgb_src, '--interval', 0.00000001, '--base-val', -100])

assert result.exit_code == -1
Loading

0 comments on commit 1eb8f65

Please sign in to comment.