Smathermather's Weblog

Remote Sensing, GIS, Ecology, and Oddball Techniques

Posts Tagged ‘r-stat’

Quantitative analysis of gorilla and monkey movement and R-Stat (part 3)

Posted by smathermather on February 16, 2017

This blog post is from a series of posts on gorilla and biodiversity research in Rwanda. I have introduced the people, the place, and a little on the beasties there. Now we’ll talk some R-code for doing home range estimation.

Home range estimation is a pretty deep and also abstract concept. Heuristically, it is the process looking at where an animal or group of animals move in the world. If one were to create a home range map for me, it’d be a pretty simple bimodal map of home and work.

Where it gets funky, is what does one do with the unusual places that an animal travels. So, for my home range, I am mostly at work, home, church on Sundays, yoga, and the grocery store. But I did spend 2 weeks in Rwanda, one week in Tanzania, one week in Belgium and the Netherlands, one week in Seattle, one in Raleigh, etc. etc.. Should these places be part of my home range?

All this to say usually home ranges are calculated with some means to not include the less common places. I would be happy if East Africa were part of my home range, but I think it’s arguable that it is not yet so.

Also, depending on the approach we use, travel between places may or may not be considered part of the home range. Back to my home range: ideally even if we concluded that 2 weeks living in Rwanda expanded my home range to include Musanze, the flight there and back probably shouldn’t be included in my home range. For our work today, we are not going to be excluding travel from our home range calculations, but understand that it can be relevant to some home range calculations.


For our home range calculation today, the following assumptions will be made:

  • We won’t be explicitly excluding travel from our home range calculations.
  • We’ll use simple techniques to exclude ephemeral portions of the home range

For basic home range analysis, we’ll use R’s adehabitat home range (adehabitatHR) package.

# Load the adehabitatHR library
# Load appropriate libraries for loading and manipulating data
library(sp) # Spatial data objects in R
library(rgdal) # Geospatial Data Abstraction Library
library(adehabitatHR) # Adehabitat HomeRange
library(readr) # File read capacity
library(rgeos) # Geometric calculation to be used later
library(maptools) # more spatial stuff

Now that we have every library we need loaded (and maybe then some) let’s load the data.

# We need to add a data filter here... .
# For now, we assign just the columns we need for HR calculation
loc_int_totf <- loc_int_tot[,c('X','Y', 'id')]

We’ll want to explicitly turn these data into geospatial data.

# Use sp library to assign coordinates and projection
coordinates(loc_int_totf) <- c("X", "Y")
# Our projection is UTM Zone 35S
# proj4string acquired at spatialreference.org
proj4string(loc_int_totf) <- CRS("+proj=utm +zone=35 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs") 

Now we are ready to do some home range calculations. We’ll use the kernelUD function to convert our data into a surface representing our home range estimate. The total of all the pixels in this surface (as represented by a raster) will total to 1, or 100 of the home range.

# Estimating the utilization distribution using "reference" bandwidth
kud <- kernelUD(loc_int_totf)

# Display the utilization distribution
image(kud)

Kernel density map

Recall that the total of all the pixel values here is 1. This means that this image represents 100 percent of the calculated range of the Golden Monkeys. If we want to calculate the 70% homerange (what we estimate the golden monkeys spend 70% of their time in) we would do so as follows:

# Estimate the homerange from the utilization distribution
homerange <- getverticeshr(kud, 70)
plot(homerange)

Outline of 70% home range

Now it would be useful to convert this to a data frame so that we can further manipulate and understand the data. For example, what is the home range size for any given percentile?

# Calculate home range sizes
as.data.frame(homerange)

# Calculate home range sizes for every 5% from 50-95%
ii <- kernel.area(kud, percent=seq(1, 99, by=1))
plot(ii)

Plot comparing home range % vs hectares

Finally, it would be nice to be able to get these data out of R and display alongside other GIS data. We’ll use writeOGR as part of RGDAL to do so.

# Write out data
writeOGR(homerange, getwd(),
         "homerange", driver="ESRI Shapefile")

That’s it for today. This bit of R will serve as the core code for a range of different analyses. Stay tuned!

Posted in Gorillas, Karisoke, R | Tagged: , , , , | Leave a Comment »

Gorilla research in Musanze, Rwanda: Hillshades continued

Posted by smathermather on January 30, 2017

I’ve been working on base cartography for the research area in Rwanda. Unlike here in Cleveland, we have some great topography to work with, so we can leverage that for basemaps. But, it’s such a beautiful landscape, I didn’t want to sell these hillshades short by doing a halfway job, so I’ve been diving deep.

Background

First, some legacy. I read three great blog posts on hillshades. One was from ESRI revealing their “Next Generation Hillshade”. Drawing on Swiss Cartographic Traditions, these are some nice looking hillshades using lighting sources from multiple directions (more on this later):

ESRIs Hillshades

Next, we look to Peter Richardsons’s recent post on Mapzen’s blog regarding terrain simplification.

Terrain Generalization example from Mapzen

Terrain Generalization example from Mapzen

I tried (not nearly as hard as I should have) to understand their code, when I saw a link to Daniel Huffman’s blog post from 2011 on terrain generalization: On Generalization Blending for Shaded Relief.

That’s when I saw the equation:

((Generalized DEM * Weight) + (Detailed DEM * (WeightMax – Weight))) / WeightMax

I’ll let you read these posts, rather than rehashing, but here’s what I did toward adding to them. The gist of Daniel and Peter’s approach is to blend together a high resolution and lower resolution version of the DEM based on a weighting factor. Both use a standard deviation filter to determine where to use the high resolution DEM vs resampled version — if the location is much higher or lower than it’s neighbors, it is considered an important feature, and given detail, otherwise the low resolution version is used (actually, I suspect Mapzen’s approach is only highlighting top features based on their diagrams, but I haven’t dived into the code to verify).

The Need

Excuse the colors, we’ll fix those at the end, but this allows us to simplify something that looks like this:

Multicolor hillshade with no simplification

Multicolor hillshade with no simplification

Into something that looks like this:

Multicolor hillshade with simplification

Multicolor hillshade with simplification

See how the hilltops and valleys remain in place and at full detail, but some of the minor facets of the hillsides are simplified? This is our aim.

I developed a pure GDAL approach for the simplification. It is purely command line, has hardcoded file names, etc, but could be done with a python or other API and turned into a proper function. TL:DR: this is not yet refined but quite effective.

Landscape Position

If you’ve been following my blog for a while, you may recall a series of blog posts on determining landscape position using gdal.

Landscape position as calculated on a DEM

Landcape position

This, with small modification, is a perfect tool for determining where to retain DEM features and where to generalize. The one modification is to calculate standard deviation from our simple difference data.

The Tools

Generalization

Back to those ugly colors on my hillshade version of the map. They go deeper than just color choice — it’s hard not to get a metallic look to digital hillshades. We see it in ESRI’s venerable map and in Mapbox’s Outdoor style. Mapzen may have avoided it by muting the multiple-light approach that ESRI lauds and Mapbox uses — I’m not sure.

HDRI (Shading)

To avoid this with our process (HT Brandon Garmin) I am using HDRI environment mapping for my lighting scheme. This allows for more complicated and realistic lighting that is pleasing to the eye and easy to interpret. Anyone who has followed me for long enough knows where this is going: straight to Pov-Ray… :

Results

The results? Stunning (am I allowed to say that?):

Example of simplified and HDRI rendered hillshade.

Example of simplified and HDRI rendered hillshade.

Hillshade over the hills of Rwanda

The color is very simple here, as we’ll be overlaying data. Please stay tuned.

Posted in GDAL, Gorillas, Karisoke, Optics, POV-Ray, QGIS, R | Tagged: , , , , , , , | Leave a Comment »

Gorilla research in Musanze, Rwanda: Hillshades!

Posted by smathermather on January 22, 2017

In previous posts here1, here2, and here3 discussed a then and future trip to Rwanda to help with GIS and gorilla research.

No more in depth write up yet, but I’ve been working on some of the cartographic products to show in the background of maps. Since Rwanda is so beautifully hilly (read: mountainous) and the research is focused on the Virunga Mountains (volcanoes) themselves, this is a huge opportunity for some hillshades to figure in the background of some maps.

So… the following image probably won’t make its way into production maps, but it is very painterly and beautiful, so I thought I’d share it:

Hillshade of the Virungas and surrounding areas.

Hillshade of the Virungas and surrounding areas.

Posted in Gorillas, Karisoke, QGIS, R | Tagged: , , , , , , | Leave a Comment »

Gorilla research in Musanze, Rwanda

Posted by smathermather on January 18, 2017

In previous posts here1 and here2, I discussed a (then future) trip to Rwanda to help with GIS and gorilla research.

I cannot say enough good about the experience. The people of Rwanda are warm and welcoming, the research team at Karisoke (Dian Fossey Gorilla Fund International) hard working, brilliant, and fun. For now we’ll do some pictures to give flavor. Then I’ll build out the narrative and code in the next few blog posts:

Mount Mgahinga over Musanze Town

Mount Mgahinga over Musanze Town

Mount Sabinyo over Musanze Town

Mount Sabinyo over Musanze Town

 

Class on QGIS, First Day

A little Karisoke Soccer/Football. No GIS Managers were injured in the filming of this video:

 

Samedi Mucyo working on some QGIS2ThreeJS for visualizing Golden Monkey ranging data

Potato fields below Mount Bisoke

Stay tuned for more!

Posted in Gorillas, Karisoke, QGIS, R | Tagged: , , , , | 1 Comment »

Quantitative analysis of gorilla movement and R-Stat (part 2)

Posted by smathermather on November 23, 2016

In my previous post, I did a bit of setup on the who and what for analyzing gorilla data. Now let’s move into R-Stat a little bit, specifically installation and configuration.

For R, we’ll install whatever package is the correct one for your machine, and then also install R-Studio. This gives us a nice development environment for R.

Screen shot of R-Studio

We’re going to leverage a few packages in our work. The first is the “sp” package. This gives us “CLasses and Methods for Spatial Data” in R:

install.packages("sp")

We’ll also “rgdal”. This package gives us “Bindings for the Geospatial Data Abstraction Library”. For our use case here, this allows us to read shapefiles, geotiffs, and other spatial data.

install.packages("rgdal")

We’ll use “rmarkdown” also. This allows us to build a markdown file (think HTML but much simpler) that includes our code in code blocks, and can be compiled to a document whether a web page, pdf, etc.. This encourages us to write code and document it in one place, and ultimately will allow us to build some really great reporting functionality while getting our analysis work done.

install.packages("rmarkdown")

Finally, we’ll need the adehabitatLT package for aiding in our analysis of animal movement and habitat use.

install.packages("adehabitatLT")

We can, of course install all this in one swell foop as follows:

install.packages("sp")
install.packages("rgdal")
install.packages("rmarkdown")
install.packages("adehabitatLT")

Now we have all the building blocks we need to start analyzing our data. Stick around for part 3.

Posted in Gorillas, Karisoke, R | Tagged: , , , , | 2 Comments »

Quantitative analysis of gorilla movement and R-Stat

Posted by smathermather on November 21, 2016

At this point in my career, it is a rare request that intimidates me. Most requests are either solved, largely solved (and I know how to find the answer), or broadly improbably / excessively expensive.

XKCD cartoon demonstrating the difficulty of distinguishing difficult problems in computer science

This is a nice place to be. I have a bit of calmness with every request, a vision for how to execute, or the escape valve of saying, “That’s harder / more expensive than you think. Perhaps we should look at these alternatives.”

Recently, I got a request that was tougher — a theoretically possible project at the edge of my knowledge domain, a desire to deliver at any reasonable cost, and no clear sense of who to ask for help.

Specifically, how does one take volumes of gorilla tracking data, and make sense of the behaviors and intentions implicit in their movement? Many know the story of Dian Fossey, the primatologist who lived with the mountain gorillas in Rwanda. What many may not know, is her research continues in Rwanda at Karisoke Research Center (and has continued for 50 years), following the troops of gorillas in Rwanda, and the project has expanded quite a bit under auspices of the Dian Fossey Gorilla fund:

Map of Africa

Map of East Africa centered on Rwanda

Map of Rwanda

Zoomed in map of Rwanda centered near Karisoke

Image of Karisoke then and now

I have the privilege of being sponsored by the Cleveland Zoological Society to got to Karisoke, observe the research, and help where needed with GIS infrastructure development and geospatial analyses. I am excited beyond belief.

So, what’s the test here? I know how to do geospatial infrastructure. Ask me to improve infrastructure and I have a decade of experience chasing down these problems, trying out different technologies, developing new ones. I also know how to perform advanced analyses. I pride myself on my analytic skills, even as I’ve moved from analyst to programmer to manager, etc.. But, I’ve never done the more advanced quantitative analyses of movement necessary to deepen understanding beyond work already done at Karisoke. Lots of great minds have already been looking at the data for years and decades. The basic question in my mind — how can I meaningfully help?

Thanks to my colleague Dr. Patrick Lorch, I have some sources. Pat did his thesis work studying the movements and behavior of Mormon crickets in the western US, and his experience with quantitative analysis of movement is directly applicable.

First we looked to Peter Turchin’s Quantitative Analysis of Movement. Then we found the adehabitat R-Stat module.

In short, the next few blog posts will be about the apparently incomparably beautiful nation of Rwanda, mountain gorillas, R-Stat, and adehabitat. There will be PostGIS mixed in for good measure. Stay tuned.

Posted in Gorillas, Karisoke, R | Tagged: , , , , | 3 Comments »