Skip to content

Instantly share code, notes, and snippets.

@dshorthouse
Last active March 30, 2023 17:58
Show Gist options
  • Save dshorthouse/f6ed3029a696b1285f81d2688bdc7b5f to your computer and use it in GitHub Desktop.
Save dshorthouse/f6ed3029a696b1285f81d2688bdc7b5f to your computer and use it in GitHub Desktop.
Basic R Script to use SimpleMappr API with csv file
species latitude longitude
Pardosa moesta 45.755 -110.12
Pardosa fuscula 47.9 -112
Pardosa moesta 55.6 -101
Pardosa xerampelina 48.9 -103.55
Pardosa xerampelina 43.02 -105.9
Trochosa terricola 45.5 -103.8
Trochosa terricola 46 -100
Trochosa terricola 47.7 -110.9
Pardosa moesta 48 -109
# This R script downloads maps from the SimpleMappr API, https://www.simplemappr.net
# It takes a 3+ column, UTF-8 csv as input with "species, latitude, longitude" as the header row
# Latitudes and longitudes are expressed in decimal degrees or DDMMSS, eg 100° 21' 44" W
# 1+ maps are saved as png file(s), named according to the unique values in the species column
# After adjusting variables below, execute the script by pressing control+shift+enter if using RStudio
# The dplyr, httr, and png packages are downloaded and installed if not already present
# Adjust data.file, output.directory as required
data.file <- "C:\\Users\\dshorthouse\\Desktop\\data.csv" # Mac/Linux as /Users/dshorthouse/Desktop/data.csv
output.directory <- "C:\\Users\\dshorthouse\\Desktop\\maps" # Mac/Linux as /Users/dshorthouse/Desktop/maps
# SimpleMappr API parameters, see API tab on http://www.simplemappr.net for details
bbox <- "-140,40,-50,75" # ~ NA bounding box of map expressed as minx,miny,maxx,maxy in decimal degrees
shape <- "circle" # shape of dots
color <- "255,0,0" # RGB color, red dots
outlinecolor <- "20,20,20"
size <- 10 # size of dots
projection <- "esri:102009" # projection
layers <- "stateprovinces,lakes" # enabled layers
zoom <- 0 # zoom level
scalebar <- TRUE # embed a scalebar on the map
width <- 600 # width in pixels
height <- 300 # height in pixels
####################################################
list.of.packages <- c("dplyr", "httr", "png")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
library(dplyr)
library(httr)
library(png)
data <- read.csv(data.file)
dir.create(output.directory, showWarnings = FALSE)
setwd(output.directory)
df.list <- split(data, as.factor(data$species))
MakeMap <- function(df)
{
coords <- paste(df$latitude, df$longitude, sep = ",", collapse = '\n')
title <- paste(df$species[1], collapse='')
file.name <- paste(title, ".png", sep = '', collapse = '')
POST("https://www.simplemappr.net/api", body = list(
points = coords,
bbox = bbox,
"shape[0]" = shape,
"size[0]" = size,
"color[0]" = color,
"legend[0]" = title,
projection = projection,
layers = layers,
outlinecolor = outlinecolor,
zoom = zoom,
scalebar = scalebar,
width = width,
height = height
), write_disk(file.name, overwrite = TRUE))
pp <- readPNG(file.name)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE,asp=dim(pp)[1]/dim(pp)[2])
title(main=title)
rasterImage(pp,0,0,1,1)
}
lapply(df.list, MakeMap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment