Archive

Posts Tagged ‘Google’

Retrieve spatial coordinates from location’s name

It is a common task to retrieve the spatial coordinates (longitude and latitude) from the sole name of a place. This occurs for example when one wants to map spatial occurrences of a species that are reported in the literature in the form of a list of places.

This work is strongly facilitated by the R package titled gooJSON developped by Christopher Steven Marcum.

Let’s say that we want to retrieve the coordinates of a place reported as Fort Myers in Florida where J. C. Denmark reported the presence of Homalodisca vitripennis Germar, 1821 in 1957.


library(gooJSON)
a<-gooadd(address = list("Fort Myers","Florida"))
b<-goomap(a)
b$Placemark[[1]]$Point$coordinates

[1] -81.87231 26.64063 0.00000

Here we are, Google API tells us (through R) that Fort Myers in Florida is located at -81.87231 longitude and 26.64063 latitude.

Now imagine that we want to repeat that operation 500 times because we have a bunch of places where our study species has been recorded. The goomap function can be put into a loop and the Google API questioned 500 times, but here comes the twist: Google will not answer 500 times in a row and will send no data for some records. This problem can simply be solved by slowing your loop so that Google API won’t receive your calling to quickly and will answer to all queries. This is done by adding a call to the function Sys.sleep in the loop to suspend execution of R expressions for a given number of seconds. I used Sys.sleep(0.2) and it worked perfectly!


Reference
Christopher Steven Marcum (2012). gooJSON: Google JSON Data Interpreter for R. R package version 1.0.01.http://CRAN.R-project.org/package=gooJSON



Advertisements
Categories: R Tags: , ,

Display pie charts with varying alpha transparencies upon a Google static map

It is sometimes interesting to display pie charts on a map showing geographic details such as Google static maps. This post will illustrate :

  • how to download and manipulate Google static maps using the R package {RgoogleMaps} by M. Loecher
  • how to overlay floating pie charts upon a map using the R package {plotrix} by J. Lemon
  • how to change the alpha value associated to colors {seqinr} by D. Charif, and J.R. Lobry

As an example we will focus on the town of Niamey where my colleagues Madougou Garba and Gauthier Dobigny (Centre Régional AGRHYMET, Département Formation Recherche, Niamey) are currently studying urban rodents communities. Lets say we want to display pie charts corresponding to sampling locations where the rodent community has been sampled using traps.

First we download the static map from Google server using the function GetMap from the package RgoogleMaps.


path<-"/home/rossi/data/blog/data/"
map1.n<-paste(path,"map1",".png",sep="")
zoom<-12 ; lonc<-2.114668 ; latc<-13.520508 ; center<-c(latc,lonc) ; maptype<-"satellite"
niamey <- GetMap(center=center, zoom=zoom, maptype=maptype,destfile=map1.n)

Note that lonc and latc are the coordinates of a points roughtly located at the center of the town. It indicates where the center of the static map should be located.

Once the data are available through the R object niamey we can display it on sreeen using PlotOnStaticMap

PlotOnStaticMap(niamey,destfile=map1.n)

Here is is what you get :

Google static map of Niamey

Now we come to the pie charts. Lets say we have a sample of the urban rodent community collected in a location which coordinates are latitude=13.51204 and longitude=2.09884. Imagine that 20 individuals of R. rattus and 5 individuals of M. natalensis were recorded at that location.

We can plot a point that shows the spatial location of the locality

PlotOnStaticMap(niamey,lat=13.51204, lon=2.09884, pch=3,cex = 3,col="red",verbose=0,destfile=map1.n)

Niamey

If we want a pie chart showing the proportion of each rodent species we use the function floating.pie(xpos,ypos,x,...) where xpos and ypos are the coordintates of the centre of the pie and x is a vector with rodent species counts.
The trick is that xpos and ypos should correspond to the map tile coordinates system and cannot be given in raw degrees.
The package RgoogleMaps provides the function LatLon2XY.centered allowing the conversion:

newcoord<-LatLon2XY.centered(niamey,lat=13.51204,lon=2.09884,zoom=zoom)

We project the pie chart using these new coordinates:

PlotOnStaticMap(niamey,destfile=map1.n)
floating.pie(xpos=newcoord$newX,ypos=newcoord$newY,x=c(20,5),
radius=20,col=c("red","blue"))

radius controls the size of the pie chart, and it can be adjusted to plot a set of pie chart with size proportional to the sample size for example.

When a large number of pie charts are plotted on the same map they may be superimposed. In that case having more or less transparent colors (i.e. changing the alpha) may be interesting.
This is achieved using the function col2alpha from the package seqinr.

col<-c("red","blue")
for (i in 1:(2)) {col[i]<-col2alpha(color=col[i], alpha = 0.50)}
PlotOnStaticMap(niamey,destfile=map1.n)
floating.pie(xpos=newcoord$newX,ypos=newcoord$newY,x=c(20,5),
radius=20,col=col)

Here is the final result, note that the river Niger can now be seen through the pie chart:

Pretty cool he?

Categories: R Tags: ,