<commit_before>---
published: true
status: publish
author: <NAME>
layout: post
title: Monk parakeets and land use in Uruguay 
---
 
We don't often think of parrots as successful invasive species. The monk parakeet, _Myiopsitta monachus_, is a widespread invader in the Northern hemisphere. Monk parakeets are native to temperate South America, and have been dispersed to other countries by means of the global pet trade. Monks survive winters in Chicago, Brooklyn and Washington State. Recently, SEO/BirdLife volunteers estimated around 20,000 monk parakeets in Spanish cities . 
 
Lately, I've been working on making maps of monk parakeet sightings using Global Biodiversity Information Facility (GBIF) data. Introduced populations are nearly always found in cities or suburbs. I've included some code below to explore how monk parakeets are distributed across different land class categories in Uruguay, a country thought to be the origin for introduced populations. Keep in mind that GBIF sightings will be biased by human reporting.
 
The map I will make here is heavily based off of Oscar Perpiñán Lamigueiro's code to create lattice maps using raster layers: see [link](https://procomun.wordpress.com/2012/02/20/maps_with_r_2/) for Oscar's original code.
 
 

    ## 
    ##  # ATTENTION: maps v3.0 has an updated 'world' map.        #
    ##  # Many country borders and names have changed since 1990. #
    ##  # Type '?world' or 'news(package="maps")'. See README_v3. #

    ## Loading required package: sp

    ## Checking rgeos availability: TRUE

    ## rgeos version: 0.3-15, (SVN revision 515)
    ##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
    ##  Linking to sp version: 1.2-1 
    ##  Polygon checking: TRUE

    ## Loading required package: methods

    ## rgdal: version: 1.1-3, (SVN revision 594)
    ##  Geospatial Data Abstraction Library extensions to R successfully loaded
    ##  Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
    ##  Path to GDAL shared files: /usr/local/Cellar/gdal/1.11.2_2/share/gdal
    ##  Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
    ##  Path to PROJ.4 shared files: (autodetected)
    ##  Linking to sp version: 1.2-1

    ## Loading required package: lattice

    ## Loading required package: latticeExtra

    ## 
    ## Attaching package: 'colorspace'

    ## The following object is masked from 'package:raster':
    ## 
    ##     RGB
 

    # make sure to install packages
    library(maps)
    library(mapdata)
    library(maptools)  #for shapefiles
    library(scales)  #for transparency
    library(RColorBrewer)
    library(rgeos)
    library(rgdal)
    library(sp)
    library(rgbif)
    library(raster)
    library(rasterVis)
    library(colorspace)
    library(lattice)
    library(latticeExtra)
    library(grid)
    library(gridExtra)
    library(grDevices)
 
Read in GBIF monk parakeet (Myiopsitta monachus) sightings and Uruguay administrative bouandaries

    mymon <- read.csv("mymon.csv", header = TRUE)
    # str(mymon)
     
    # Uruguay administrative boundaries from http://www.gadm.org/country
    URY_adm0 <- readShapePoly("URY_adm0.shp", proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")) # country border
    # str(URY_adm0)
    # class(URY_adm0)
    # str(URY_adm0@data)
     
    # convert country level data to data frame for mapping
    URY_adm0_df <- fortify(URY_adm0)

    ## Error in eval(expr, envir, enclos): could not find function "fortify"

    # str(URY_adm0_df)
 
Retain only GBIF observations contained within the country level polygon, using sp::over().
I did this because GBIF observations do not always contain accurate stateProvince information. Also, reading in Uruguay roads as shapefiles. 

    # create SpatialPoints object of monk parakeet observations
    lon <- mymon$decimalLongitude
    lat <- mymon$decimalLatitude
    mat <- as.matrix(cbind(lon, lat))
    p <- SpatialPoints(mat, proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
     
    # determine if each sighting is inside or outside of Uruguay country polygon
    URY <- !is.na(over(p, as(URY_adm0, "SpatialPolygons")))
    length(URY)

    ## [1] 32644

    lon.URY.in <- lon[which(URY == TRUE)]
    lat.URY.in <- lat[which(URY == TRUE)]
     
    # create new SpatialPoints object to convert all Uruguay observations to the 
    # same coordinate system as gadm data
    mat2 <- as.matrix(cbind(lon.URY.in, lat.URY.in))
    p2 <- SpatialPoints(mat2, proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
    # str(p2@coords)
    conv.lon <- p2@coords[, 1]
    conv.lat <- p2@coords[, 2]
    points <- data.frame(lon = conv.lon, lat = conv.lat)
     
    # read in Uruguay roads data
    # from Natural Earth http://www.naturalearthdata.com/downloads/10m-cultural-vectors/
    URYroadsOGR <- readOGR("URY_roads.shp", "URY_roads")

    ## Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open file

    str(URYroadsOGR)

    ## Error in str(URYroadsOGR): object 'URYroadsOGR' not found

    URYroads <- spTransform(URYroadsOGR, CRS("+proj=longlat +datum=WGS84"))

    ## Error in spTransform(URYroadsOGR, CRS("+proj=longlat +datum=WGS84")): error in evaluating the argument 'x' in selecting a method for function 'spTransform': Error: object 'URYroadsOGR' not found

    URYroads_df <- fortify(URYroads)

    ## Error in eval(expr, envir, enclos): could not find function "fortify"

    str(URYroads_df)

    ## Error in str(URYroads_df): object 'URYroads_df' not found
 
Read in population and land cover raster layers

    # download global population and land cover MODIS data as GEOTiff
    # http://neo.sci.gsfc.nasa.gov
     
    # bounding box for Uruguay
    b <- extent(-59, -53, -35.5, -29.5)
     
    # read in population data and crop to fit Uruguay
    pop <- raster("SEDAC_POP_2000-01-01_rgb_3600x1800.FLOAT-2.TIFF")
    pop <- crop(pop, b)
    pop[pop == 99999] <- NA
     
    # disaggregating raster to create higher resolution (smaller cells), 
    # for a less pixelated map, this doesn't work well for the land cover raster
    pop2 <- disaggregate(pop, fact = 10, method = "bilinear")<commit_msg>Troubleshooting "Monk parakeets and land use in Uruguay"<commit_after>---
published: true
status: publish
author: <NAME>
layout: post
title: Monk parakeets and land use in Uruguay 
---
 
We don't often think of parrots as successful invasive species. The monk parakeet, _Myiopsitta monachus_, is a widespread invader in the Northern hemisphere. Monk parakeets are native to temperate South America, and have been dispersed to other countries by means of the global pet trade. Monks survive winters in Chicago, Brooklyn and Washington State. Recently, SEO/BirdLife volunteers estimated around 20,000 monk parakeets in Spanish cities . 
 
Lately, I've been working on making maps of monk parakeet sightings using Global Biodiversity Information Facility (GBIF) data. Introduced populations are nearly always found in cities or suburbs. I've included some code below to explore how monk parakeets are distributed across different land class categories in Uruguay, a country thought to be the origin for introduced populations. Keep in mind that GBIF sightings will be biased by human reporting.
 
The map I will make here is heavily based off of <NAME>'s code to create lattice maps using raster layers: see [link](https://procomun.wordpress.com/2012/02/20/maps_with_r_2/) for Oscar's original code.
 
 

 

    # make sure to install packages
    library(maps)
    library(mapdata)
    library(ggmap)
    library(ggplot2)
    library(maptools)  #for shapefiles
    library(scales)  #for transparency
    library(RColorBrewer)
    library(rgeos)
    library(rgdal)
    library(sp)
    library(rgbif)
    library(raster)
    library(rasterVis)
    library(colorspace)
    library(lattice)
    library(latticeExtra)
    library(grid)
    library(gridExtra)
    library(grDevices)
 
Read in GBIF monk parakeet (Myiopsitta monachus) sightings and Uruguay administrative bouandaries

    mymon <- read.csv("mymon.csv", header = TRUE)
    # str(mymon)
     
    # Uruguay administrative boundaries from http://www.gadm.org/country
    URY_adm0 <- readShapePoly("URY_adm0.shp", proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")) # country border
    # str(URY_adm0)
    # class(URY_adm0)
    # str(URY_adm0@data)
     
    # convert country level data to data frame for mapping
    URY_adm0_df <- fortify(URY_adm0)

    ## Regions defined for each Polygons

    # str(URY_adm0_df)
 
Retain only GBIF observations contained within the country level polygon, using sp::over().
I did this because GBIF observations do not always contain accurate stateProvince information. Also, reading in Uruguay roads as shapefiles. 

    # create SpatialPoints object of monk parakeet observations
    lon <- mymon$decimalLongitude
    lat <- mymon$decimalLatitude
    mat <- as.matrix(cbind(lon, lat))
    p <- SpatialPoints(mat, proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
     
    # determine if each sighting is inside or outside of Uruguay country polygon
    URY <- !is.na(over(p, as(URY_adm0, "SpatialPolygons")))
    length(URY)

    ## [1] 32644

    lon.URY.in <- lon[which(URY == TRUE)]
    lat.URY.in <- lat[which(URY == TRUE)]
     
    # create new SpatialPoints object to convert all Uruguay observations to the 
    # same coordinate system as gadm data
    mat2 <- as.matrix(cbind(lon.URY.in, lat.URY.in))
    p2 <- SpatialPoints(mat2, proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
    # str(p2@coords)
    conv.lon <- p2@coords[, 1]
    conv.lat <- p2@coords[, 2]
    points <- data.frame(lon = conv.lon, lat = conv.lat)
     
    # read in Uruguay roads data
    # from Natural Earth http://www.naturalearthdata.com/downloads/10m-cultural-vectors/
    URYroadsOGR <- readOGR("URY_roads.shp", "URY_roads")

    ## Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open file

    str(URYroadsOGR)

    ## Error in str(URYroadsOGR): object 'URYroadsOGR' not found

    URYroads <- spTransform(URYroadsOGR, CRS("+proj=longlat +datum=WGS84"))

    ## Error in spTransform(URYroadsOGR, CRS("+proj=longlat +datum=WGS84")): error in evaluating the argument 'x' in selecting a method for function 'spTransform': Error: object 'URYroadsOGR' not found

    URYroads_df <- fortify(URYroads)

    ## Error in fortify(URYroads): object 'URYroads' not found

    str(URYroads_df)

    ## Error in str(URYroads_df): object 'URYroads_df' not found
 
Read in population and land cover raster layers

    # download global population and land cover MODIS data as GEOTiff
    # http://neo.sci.gsfc.nasa.gov
     
    # bounding box for Uruguay
    b <- extent(-59, -53, -35.5, -29.5)
     
    # read in population data and crop to fit Uruguay
    pop <- raster("SEDAC_POP_2000-01-01_rgb_3600x1800.FLOAT-2.TIFF")
    pop <- crop(pop, b)
    pop[pop == 99999] <- NA
     
    # disaggregating raster to create higher resolution (smaller cells), 
    # for a less pixelated map, this doesn't work well for the land cover raster
    pop2 <- disaggregate(pop, fact = 10, method = "bilinear")
{'fraction_non_alphanumeric': 0.09673455056179775, 'fraction_numerical': 0.024841994382022472, 'mean_word_length': 3.569995988768552, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7359974', 'n_tokens_mistral': 4093, 'n_tokens_neox': 3649, 'n_words': 1188}