Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a unit test to stack function and more docs #15

Merged
merged 2 commits into from Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion docs/get-started.rst
@@ -1,4 +1,4 @@
Introduction to Earthpy
Get Started With Earthpy
========================

Earthpy is a python package devoted to working with spatial and remote sensing data.
Expand All @@ -11,3 +11,25 @@ Earthpy Modules
Spatial
IO
Utils

Installation
------------

Dependencies
~~~~~~~~~~~~

Earthpy has several Python package dependencies including : ``rasterio, geopandas, numpy``.

To install earthpy, use pip. ``--upgrade`` is optional but it ensures that the package
overwrites when you install and you have the current version. If you don't have
the package yet you can still use the ``--upgrade argument``.

``pip install --upgrade git+https://github.com/earthlab/earthpy.git``

Then import it into python.

``import earthpy as et``

To import the spatial module use:

``import earthpy.spatial as es``
1 change: 0 additions & 1 deletion docs/index.rst
Expand Up @@ -11,7 +11,6 @@ Welcome to Earthpy's documentation!
:caption: Contents:

get-started
installation
spatial-module


Expand Down
20 changes: 0 additions & 20 deletions docs/installation.rst
@@ -1,21 +1 @@
Install Earthpy
========================

Dependencies
------------

Earthpy has several Python package dependencies including : ``rasterio, geopandas, numpy``.

To install earthpy, use pip. ``--upgrade`` is optional but it ensures that the package
overwrites when you install and you have the current version. If you don't have
the package yet you can still use the ``--upgrade argument``.

``pip install --upgrade git+https://github.com/earthlab/earthpy.git``

Then import it into python.

``import earthpy as et``

To import the spatial module use:

``import earthpy.spatial as es``
19 changes: 15 additions & 4 deletions docs/spatial-module.rst
Expand Up @@ -7,11 +7,17 @@ and ``geopandas`` to work with raster and vector data in Python.
Raster Data
-----------

stack_raster_tifs
~~~~~~~~~~~~~~~~~
Stack Raster Files
~~~~~~~~~~~~~~~~~~

The ``stack_raster_tifs`` function takes a list of raster paths and turns that list
into an 1) output raster stack in numpy format and 2) a stacked geotiff on your hard drive.
into an

1. output raster stack in numpy format and
2. a stacked geotiff on your hard drive.

All files in the list must be in the same crs and must have the same spatial extent
for this to work properly.

stack_raster_tifs takes 2 input parameters:

Expand All @@ -33,7 +39,12 @@ Example:

.. code-block:: python

from glob import glob

# Create file name list
all_file_paths = glob("path/here/*band*.tif")

file_path = "data/location/filename.tif"

# Stack landsat tif files using es.stack_raster_tifs - earthpy
# Stack landsat tif files
arr, arr_meta = es.stack_raster_tifs(all__paths, file_path)
28 changes: 18 additions & 10 deletions earthpy/spatial.py
Expand Up @@ -68,10 +68,13 @@ def stack_raster_tifs(band_paths, out_path):
if not os.path.exists(os.path.dirname(out_path)):
raise ValueError("The output directory path that you provided does not exist")

if len(band_paths) < 2:
raise ValueError("The list of file paths is empty. You need atleast 2 files to create a stack.")
# the with statement ensures that all files are closed at the end of the with statement
with contextlib.ExitStack() as context:
sources = [context.enter_context(rio.open(path, **kwds)) for path in band_paths]

# This should check that the CRS and TRANSFORM are the same. if not, fail gracefully
dest_kwargs = sources[0].meta
dest_count = sum(src.count for src in sources)
dest_kwargs['count'] = dest_count
Expand Down Expand Up @@ -350,7 +353,7 @@ def plot_rgb(arr, rgb = (0,1,2),
Parameters
----------
arr: a n dimension numpy array in rasterio band order (bands, x, y)
rgb: tuple, indices of the three bands to be plotted (default = 0,1,2)
rgb: list, indices of the three bands to be plotted (default = 0,1,2)
extent: the extent object that matplotlib expects (left, right, bottom, top)
title: optional string representing the title of the plot
ax: the ax object where the ax element should be plotted. Default = none
Expand Down Expand Up @@ -400,7 +403,7 @@ def plot_rgb(arr, rgb = (0,1,2),


def hist(arr,
titles = None,
title = None,
colors = ["purple"],
figsize=(12,12), cols = 2,
bins = 20):
Expand All @@ -410,7 +413,7 @@ def hist(arr,
Parameters
----------
arr: a n dimension numpy array
titles: a list of title values that should either equal the number of bands or be empty, default = none
title: a list of title values that should either equal the number of bands or be empty, default = none
colors: a list of color values that should either equal the number of bands or be a single color, (purple = default)
cols: int the number of columsn you want to plot in
bins: the number of bins to calculate for the histogram
Expand All @@ -423,32 +426,37 @@ def hist(arr,
# if the array is 3 dimensional setup grid plotting
if arr.ndim > 2:
# test if there are enough titles to create plots
if titles:
if not (len(titles) == arr.shape[0]):
if title:
if not (len(title) == arr.shape[0]):
raise ValueError("The number of plot titles should be the same as the number of raster layers in your array.")
# calculate the total rows that will be required to plot each band
plot_rows = int(np.ceil(arr.shape[0] / cols))
total_layers = arr.shape[0]

fig, axs = plt.subplots(plot_rows, cols, figsize=figsize, sharex=True, sharey=True)
axs_ravel = axs.ravel()
# what happens if there is only one color?
for band, ax, i in zip(arr, axs.ravel(),range(total_layers)):
for band, ax, i in zip(arr, axs.ravel(), range(total_layers)):
if len(colors) == 1:
the_color = colors[0]
else:
the_color = colors[i]
ax.hist(band.ravel(), bins=bins, color=the_color, alpha=.8)
if titles:
ax.set_title(titles[i])
if title:
ax.set_title(title[i])
# Clear additional axis elements
for ax in axs_ravel[total_layers:]:
ax.set_axis_off()
#ax.set(xticks=[], yticks=[])
elif arr.ndim == 2:
# plot all bands
fig, ax = plt.subplots(figsize=figsize)
ax.hist(arr.ravel(),
range=[np.nanmin(arr), np.nanmax(arr)],
bins=bins,
color=colors[0])
if titles:
ax.set(title=titles[0])
if title:
ax.set(title=title[0])


def hillshade(arr, azimuth=30, angle_altitude=30):
Expand Down