Merge remote-tracking branch 'remotes/origin/master' into notebook-debug

intro16
epifanio 2016-03-21 17:10:25 -03:00
commit de81a1004f
1 changed files with 442 additions and 0 deletions

View File

@ -0,0 +1,442 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# An introduction to Rasterio\n",
"\n",
"The smallest interesting problems [1] addressed by Rasterio are reading raster data from files as [Numpy](http://www.numpy.org/) arrays and writing such arrays back to files. In between, you can use the world of scientific python software to analyze and process the data. Rasterio also provides a few operations that are described in the next notebooks in this series.\n",
"\n",
"This notebook demonstrates the basics of reading and writing raster data with Rasterio.\n",
"\n",
"## Overview of a dataset\n",
"\n",
"A raster dataset consists of one or more dense (as opposed to sparse) 2-D arrays of scalar values. An RGB TIFF image file is a good example of a raster dataset. It has 3 bands (or channels we'll call them bands here) and each has a number of rows (its `height`) and columns (its `width`) and a uniform data type (unsigned 8-bit integers, 64-bit floats, etc). Geospatially referenced datasets will also possess a mapping from image to world coordinates (a `transform`) in a specific coordinate reference system (`crs`). This metadata about a dataset is readily accessible using Rasterio.\n",
"\n",
"The Scientific Python community often imports numpy as `np`. Do this and also import rasterio.\n",
"\n",
"\n",
"------------------------------\n",
" Rasterio's tests require several raster data files. Grab them from\n",
"\n",
" https://github.com/mapbox/rasterio/tree/master/tests/data\n",
"\n",
" and copy them to this directory.\n",
"\n",
" The RGB.byte.tif file is derived from USGS Landsat 7 ETM imagery. The shade.tif\n",
" file is derived from USGS SRTM 90 data. The float.tif and float_nan.tif files\n",
" are original works of the Rasterio authors. All test images are licensed under\n",
" the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication:\n",
" http://creativecommons.org/publicdomain/zero/1.0/.\n",
"\n",
"\n",
"`` Copyright (c) 2013, MapBox. All rights reserved. ``"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import rasterio"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rasterio uses for many of its tests a small 3-band GeoTIFF file named \"RGB.byte.tif\". Open it using the function `rasterio.open()`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"src = rasterio.open('data/RGB.byte.tif')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function returns a dataset object. It has many of the same properties as a Python file object."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'../tests/data/RGB.byte.tif'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.name"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'r'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.mode"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.closed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Raster datasets have additional structure and a description can be had from its `meta` property or individually."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'affine': Affine(300.0379266750948, 0.0, 101985.0,\n",
" 0.0, -300.041782729805, 2826915.0),\n",
" 'count': 3,\n",
" 'crs': {'init': u'epsg:32618'},\n",
" 'driver': u'GTiff',\n",
" 'dtype': 'uint8',\n",
" 'height': 718,\n",
" 'nodata': 0.0,\n",
" 'transform': (101985.0,\n",
" 300.0379266750948,\n",
" 0.0,\n",
" 2826915.0,\n",
" 0.0,\n",
" -300.041782729805),\n",
" 'width': 791}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.meta"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'init': u'epsg:32618'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.crs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To close an opened dataset, use its `close()` method."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.close()\n",
"src.closed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can't read from or write to a closed dataset, but you can continue access its properties."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"u'GTiff'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.driver"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dataset layout\n",
"\n",
"Three properties of a Rasterio dataset tell you a lot about it in Numpy terms. The `shape` of a dataset is a `height, width` tuple and is exactly the shape of Numpy arrays that would be read from it. The testing dataset has 718 rows and 791 columns."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(718, 791)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `count` of bands in the dataset is 3."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.count"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All three of its bands contain 8-bit unsigned integers."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['uint8', 'uint8', 'uint8']"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy concepts are the model here. If you wanted to create a 3-D Numpy array into which the testing data file's bands would fit without any resampling, you would use the following Python code."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ..., \n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ..., \n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ..., \n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]]], dtype=uint8)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dest = np.empty((src.count,) + src.shape, dtype='uint8')\n",
"dest"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[1]: Mike Bostock's words from his FOSS4G keynote, 2014-09-10"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}