In [1]:
    
import logging
root = logging.getLogger()
root.addHandler(logging.StreamHandler())
%matplotlib inline
    
In [2]:
    
# download http://bit.ly/1R8pt20 (zipped Turtles shapefiles), and unzip them
from iSDM.species import IUCNSpecies
turtles = IUCNSpecies(name_species='Acanthochelys pallidipectoris')
turtles.load_shapefile('../data/FW_TURTLES/FW_TURTLES.shp')
    
    
In [3]:
    
turtles.get_data().head(10)
    
    Out[3]:
In [4]:
    
turtles.get_data().columns # all the columns available per species geometry
    
    Out[4]:
In [5]:
    
turtles.source.name
    
    Out[5]:
In [10]:
    
# WARNING! many shapefiles to plot for 181 species! You may want to skip this line if your PC is not strong! :P
turtles.plot_species_occurrence()
    
    
In [6]:
    
turtles.find_species_occurrences()
    
    
    Out[6]:
In [7]:
    
turtles.get_data() # datatype: geopandas.geodataframe.GeoDataFrame
    
    Out[7]:
In [8]:
    
# Now plot the filtered selection
turtles.plot_species_occurrence()
    
    
In [9]:
    
turtles.save_data() # serialize all the current data to a pickle file, so it can be loaded later on
    
    
In [10]:
    
turtles.load_data()
    
    
    Out[10]:
In [11]:
    
turtles.ID # derived from "id_no" column. It's a sort of unique ID per species
    
    Out[11]:
In [12]:
    
ax = turtles.get_data().plot(figsize=(10,10))
turtles.data_full.geometry.convex_hull.plot(ax=ax)
    
    Out[12]:
    
In [13]:
    
with_buffer = turtles.get_data().geometry.buffer(0.5)
with_buffer.plot() # hmm, now the buffer behaves differently
    
    Out[13]:
    
In [14]:
    
turtles.save_shapefile(full_name="./TURTLES_FILTERED.shp")
    
    
In [15]:
    
pseudo_absence = turtles.random_pseudo_absence_points(buffer_distance=1.5, count=200)
    
    
In [16]:
    
#import matplotlib.pyplot as plt
#plt.figure(figsize=(15,15))
ax = pseudo_absence.plot(figsize=(10,10))
turtles.get_data().plot(ax=ax)
    
    Out[16]:
    
In [17]:
    
# 1. Make simplified buffer around the geometry
simplified_buffer = turtles.get_data().geometry.buffer(0).simplify(1,True).buffer(1.5)
# 2. Make a difference of the above area, with the original one
buffer_difference = simplified_buffer.geometry.difference(turtles.get_data().geometry.buffer(0))
# 3. Randomly generate points in simplified_buffer's envelope until you reach <count> number of points belonging to this area
buffer_difference.plot(figsize=(10,10))
    
    Out[17]:
    
There are more complicated geometries (possibly with multiple polygons and "invalid"). Operations like intersection or union are problematic for such geometries, as they can throw errors. For example:
In [18]:
    
turtles.load_shapefile("../data/filtered_turtles_again.shp")
turtles.plot_species_occurrence()
    
    
    
For them the process of creating a buffer region is a bit more involved, as simplifications must be made. Note that this is done automatically behind the scenes. We show the inner-process just to get an idea of the area from which random points are drawn. In this case, the area to draw points from, would look like this:
In [19]:
    
simplified_buffer = turtles.get_data().geometry.buffer(0).simplify(1,True).buffer(1.5)
# this would throw an exception, so we must simplify it further
# buffer_difference = simplified_buffer.geometry.difference(turtles.get_data().geometry.buffer(0))
simplified_buffer = simplified_buffer.simplify(4, True) 
buffer_difference = simplified_buffer.geometry.difference(turtles.get_data().geometry.buffer(0))
ax = buffer_difference.plot(figsize=(10,10))
turtles.get_data().plot(ax=ax)
    
    Out[19]:
    
In practise, all you need to do is, again, only the following:
In [20]:
    
pseudo_absence = turtles.random_pseudo_absence_points(buffer_distance=1.5, count=200)
    
    
Let's plot them together, the original area and the random points
In [21]:
    
ax = pseudo_absence.plot(figsize=(10,10))
turtles.get_data().plot(ax=ax)
    
    Out[21]:
    
In [22]:
    
turtles.rasterize(raster_file='./turtles.tif', pixel_size=0.5, all_touched=True)
    
    
    Out[22]:
In [23]:
    
turtles_raster_data = turtles.load_raster_data(raster_file='./turtles.tif')
    
    
In [24]:
    
turtles_raster_data
    
    Out[24]:
In [25]:
    
turtles_raster_data.shape # 1 layer(band), resolution
    
    Out[25]:
In [26]:
    
import matplotlib.pyplot as plt
plt.figure(figsize=turtles_raster_data.shape[1:]) # careful with big images!
plt.imshow(turtles_raster_data[0], cmap="hot", interpolation="none") # the first band has index 0
    
    Out[26]:
    
In [27]:
    
result = turtles.rasterize(raster_file='./turtles.tif', pixel_size=0.5, all_touched=False, no_data_value=254, default_value=5) # all_touched=False now
    
    
In [28]:
    
result
    
    Out[28]:
In [29]:
    
plt.figure(figsize=result.shape) # careful with big images!
plt.imshow(result, cmap="hot", interpolation="none")
    
    Out[29]:
    
In [ ]: