In [1]:
import logging
import numpy as np
import pandas as pd
root = logging.getLogger()
root.addHandler(logging.StreamHandler())
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
from iSDM.environment import ContinentsLayer
from iSDM.environment import Source

In [3]:
continents = ContinentsLayer(file_path="../data/continents/continent.shp", source=Source.ARCGIS)

In [4]:
continents.load_data()


Loading data from ../data/continents/continent.shp 
The shapefile contains data on 8 environmental regions.

In [5]:
continents.get_data()


Out[5]:
continent geometry
0 Asia (POLYGON ((93.27554321289062 80.26361083984375...
1 North America (POLYGON ((-25.28166961669922 71.3916625976562...
2 Europe (POLYGON ((58.06137847900391 81.68775939941406...
3 Africa (POLYGON ((0.6946510076522827 5.77336502075195...
4 South America (POLYGON ((-81.71305847167969 12.4902763366699...
5 Oceania (POLYGON ((-177.3933410644531 28.1841583251953...
6 Australia (POLYGON ((142.2799682617188 -10.2655563354492...
7 Antarctica (POLYGON ((51.80305480957031 -46.4566726684570...

In [6]:
fig, ax = plt.subplots(1,1, figsize=(30,20))
continents.data_full.plot(column="continent", colormap="hsv")


Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe609fbbc50>

In [7]:
continents_rasters = continents.rasterize(raster_file="../data/continents/continents_raster.tif", pixel_size=0.5, all_touched=True)


Will rasterize continent-by-continent.
Rasterizing continent Asia 
Rasterizing continent North America 
Rasterizing continent Europe 
Rasterizing continent Africa 
Rasterizing continent South America 
Rasterizing continent Oceania 
Rasterizing continent Australia 
Rasterizing continent Antarctica 
RASTERIO: Data rasterized into file ../data/continents/continents_raster.tif 
RASTERIO: Resolution: x_res=720 y_res=360

In [8]:
continents_rasters.shape # stacked raster with 8 bands, one for each continent.


Out[8]:
(8, 360, 720)

In [24]:
plt.figure(figsize=(25,20))
plt.imshow(continents_rasters[4], cmap="hot", interpolation="none")


Out[24]:
<matplotlib.image.AxesImage at 0x7fe60486cda0>

In [10]:
plt.figure(figsize=(25,20))
plt.imshow(continents_rasters[4], cmap="hot", interpolation="none")


Out[10]:
<matplotlib.image.AxesImage at 0x7fe605120d30>

In [11]:
raster_reader = continents.load_raster_data()


Loaded raster data from ../data/continents/continents_raster.tif 
Driver name: GTiff 
Metadata: {'affine': Affine(0.5, 0.0, -180.0,
       0.0, -0.5, 90.0),
 'count': 8,
 'crs': {'init': 'epsg:4326'},
 'driver': 'GTiff',
 'dtype': 'uint8',
 'height': 360,
 'nodata': 0.0,
 'transform': (-180.0, 0.5, 0.0, 90.0, 0.0, -0.5),
 'width': 720} 
Resolution: x_res=720 y_res=360.
Bounds: BoundingBox(left=-180.0, bottom=-90.0, right=180.0, top=90.0) 
Coordinate reference system: {'init': 'epsg:4326'} 
Affine transformation: (-180.0, 0.5, 0.0, 90.0, 0.0, -0.5) 
Number of layers: 8 
Dataset loaded. Use .read() or .read_masks() to access the layers.

In [12]:
raster_reader.read(5) # read a particular band


Out[12]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

In [18]:
set(np.unique(continents_rasters))


Out[18]:
{0, 1}

In [17]:
continents_rasters.shape[1:]


Out[17]:
(360, 720)

In [19]:
isinstance(continents_rasters, np.ndarray)


Out[19]:
True

In [20]:
set(np.unique(continents_rasters)) == set({0, 1})


Out[20]:
True

In [21]:
continents_rasters[1]


Out[21]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

In [25]:
# download from Google Drive: https://drive.google.com/open?id=0B9cazFzBtPuCOFNiUHYwcVFVODQ
# Representative example with multiple polygons in the shapefile, and a lot of point-records (also outside rangemaps)
from iSDM.species import IUCNSpecies
salmo_trutta = IUCNSpecies(name_species='Salmo trutta')
salmo_trutta.load_shapefile("../data/fish/selection/salmo_trutta")


Enabled Shapely speedups for performance.
Loading data from: ../data/fish/selection/salmo_trutta
The shapefile contains data on 3 species areas.

In [26]:
rasterized = salmo_trutta.rasterize(raster_file="./salmo_trutta_full.tif", pixel_size=0.5, all_touched=True)


RASTERIO: Data rasterized into file ./salmo_trutta_full.tif 
RASTERIO: Resolution: x_res=720 y_res=360

In [45]:
plt.figure(figsize=(25,20))
plt.imshow(rasterized, cmap="hot", interpolation="none")


Out[45]:
<matplotlib.image.AxesImage at 0x7fe601480cf8>

In [27]:
rasterized.shape


Out[27]:
(360, 720)

In [54]:
(continents_rasters * rasterized)[2].max()


Out[54]:
1

In [60]:
plt.figure(figsize=(25,20))
plt.imshow(continents_rasters[2] + continents_rasters[0], cmap="hot", interpolation="none")


Out[60]:
<matplotlib.image.AxesImage at 0x7fe6015ff748>

In [58]:



Out[58]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

In [129]:
overlayed_continents_species = continents_rasters * rasterized

In [130]:
selected_continents = np.zeros_like(rasterized)

In [131]:
for idx, band in enumerate(overlayed_continents_species):
    if band.max()==1:
        selected_continents +=continents_rasters[idx]

In [121]:
plt.figure(figsize=(25,20))
plt.imshow(selected_continents, cmap="hot", interpolation="none")


Out[121]:
<matplotlib.image.AxesImage at 0x7fe60539b128>

In [118]:
selected_continents.max() # where the continents touch, we have overlap!


Out[118]:
2

In [122]:
plt.figure(figsize=(25,20))
plt.imshow(selected_continents==2, cmap="hot", interpolation="none")


Out[122]:
<matplotlib.image.AxesImage at 0x7fe60517fd30>

In [127]:
selected_continents[selected_continents > 1]=1

In [128]:
plt.figure(figsize=(25,20))
plt.imshow(selected_continents, cmap="hot", interpolation="none")


Out[128]:
<matplotlib.image.AxesImage at 0x7fe6047405c0>

In [135]:
if continents_rasters is not None:
    print("YES")


YES

In [176]:
continents_rasters = continents_rasters_copy.copy()

In [139]:
continents_rasters_copy = continents_rasters.copy()

In [177]:
continents_rasters[0] = continents_rasters[0] + continents_rasters[2]

In [178]:
continents_rasters = np.delete(continents_rasters, 2, 0) # delete layer 2 (Europe, previously merged with layer 0==Asia)

In [179]:
continents_rasters.shape


Out[179]:
(7, 360, 720)

In [182]:
continents_rasters[0].max() # where the continents touch, we have overlap! that's why max is not 1, but 2.


Out[182]:
2

In [191]:
sum_continents = continents_rasters[0]+continents_rasters[1]+continents_rasters[2]+continents_rasters[3] + continents_rasters[4] + continents_rasters[5]+continents_rasters[6]

In [195]:
plt.figure(figsize=(25,20))
plt.imshow(sum_continents, cmap="hot", interpolation="none")


Out[195]:
<matplotlib.image.AxesImage at 0x7fe6024bd898>

In [184]:
continents.get_data().continent


Out[184]:
0             Asia
1    North America
2           Europe
3           Africa
4    South America
5          Oceania
6        Australia
7       Antarctica
Name: continent, dtype: object

In [187]:
np.sum(continents_rasters)


Out[187]:
94138

In [ ]: