How to append a shp file to modify map boundaries? - r

Thanks to suggestions from #AriBFriedman and #PaulHiemstra and subsequently figuring out how to merge .shp files, I have managed to produce the following map using the following code and data:
Data:
Morocco and Western Sahara
Code:
# Loading administrative coordinates for Morocco maps
library(sp)
library(maptools)
library(mapdata)
# Loading shape files
Mor <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
Sah <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
# Ploting the maps individually
png("Morocco.png")
Morocco <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
plot(Morocco)
dev.off()
png("WesternSahara.png")
WesternSahara <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
plot(WesternSahara)
dev.off()
# Merged map of Morocco and Western Sahara
MoroccoData <- rbind(Mor#data,Sah#data) # First, 'stack' the attribute list rows using rbind()
MoroccoPolys <- c(Mor#polygons,Sah#polygons) # Next, combine the two polygon lists into a single list using c()
# summary(MoroccoData)
# summary(MoroccoPolys)
offset <- length(MoroccoPolys) # Next, generate a new polygon ID for the new SpatialPolygonDataFrame object
browser()
for (i in 1: offset)
{
sNew = as.character(i)
MoroccoPolys[[i]]#ID = sNew
}
ID <- c(as.character(1:length(MoroccoPolys))) # Create an identical ID field and append it to the merged Data component
MoroccoDataWithID <- cbind(ID,MoroccoData)
MoroccoPolysSP <- SpatialPolygons(MoroccoPolys,proj4string=CRS(proj4string(Sah))) # Promote the merged list to a SpatialPolygons data object
Morocco <- SpatialPolygonsDataFrame(MoroccoPolysSP,data = MoroccoDataWithID,match.ID = FALSE) # Combine the merged Data and Polygon components into a new SpatialPolygonsDataFrame.
Morocco#data$id <- rownames(Morocco#data)
Morocco.fort <- fortify(Morocco, region='id')
Morocco.fort <- Morocco.fort[order(Morocco.fort$order), ]
MoroccoMap <- ggplot(data=Morocco.fort, aes(long, lat, group=group)) +
geom_polygon(colour='black',fill='white') +
theme_bw()
Results:
Individual maps
Merged Map:
Question:
I want to eliminate the boundary that cuts through the middle of the map? Anyone with any suggestions and/or help?
Thx all

I did achieve the elimination of the extra polygon layer that cuts the map in two after merging by using QGIS software, which is free to download at this link QGIS

Related

iterate SpatialPoints and SpatialPolygonsDataFrame merge in R

I am trying to iterate thru the process of creating centroids of a list of SpatialPolygonsDataFrames (each with several polygon features within), with the resulting SpatialPoints preserving the attributes (data) of the parent polygons. I have tried the sp::over function, but it seems to be problematic because centroids do not necessarily overlap with parent polygons. Furthermore, I am new to coding/R and do not know how to achieve this in a for loop and/or using an apply function.
So specifically, I need to (1) find a different function that will link the SpatialPolygonsDataFrames with the associated SpatialPoints (centroids); and (2) iterate thru the entire process and merge the SpatialPolygonsDataFrames data with the appropriate SpatialPoints - I do not know how to match the index value of one list to the index value in another while running a loop.
Here is a reproducible example for a single SpatialPolygonsDataFrames object showing that using sp::over doesn't work because some centroids do not end up overlapping parent polygons:
library(maptools) ## For wrld_simpl
library(sp)
## Example SpatialPolygonsDataFrames (SPDF)
data(wrld_simpl) #polygon of world countries
spdf1 <- wrld_simpl[1:25,] #country subset 1
spdf2 <- wrld_simpl[26:36,] #subset 2
spdf3 <- wrld_simpl[36:50,] #subset 3
spdfl[[1]]#data
#plot, so you can see it
plot(spdf1)
plot(spdf2, add=TRUE, col=4)
plot(spdf3, add=TRUE, col=3)
#make list of SPDF objects
spdfl<-list()
spdfl[[1]]<-spdf1
spdfl[[2]]<-spdf2
spdfl[[2]]<-spdf3
#create polygon centroids for each polygon feature (country) within spdfl[[1]]
spdf1c <- gCentroid(spdfl[[1]], byid=TRUE)
plot(spdfl[[1]])
plot(spdf1c, add=TRUE)
#add attributes 'NAME' and 'AREA' to SpatialPoints (centroid) object from SPDF data
spdf.NAME <- over(spdf1c, spdfl[[1]][,"NAME"])
spdf.AREA <- over(spdf1c, spdfl[[1]][,"AREA"])
spdf1c$NAME <- spdf.NAME
spdf1c$AREA <- spdf.AREA
spdf1c#data
#`sp::over` output error = name and area for ATG, ASM, BHS, and SLB are missing
I find SF is a great package for working with spatial data in R. I have fixed a few typos and added the right for loop below.
library(maptools) ## For wrld_simpl
library(sp)
library(sf)
## Example SpatialPolygonsDataFrames (SPDF)
data(wrld_simpl) #polygon of world countries
spdf1 <- wrld_simpl[1:25,] #country subset 1
spdf2 <- wrld_simpl[26:36,] #subset 2
spdf3 <- wrld_simpl[36:50,] #subset 3
spdfl[[1]]#data
#plot, so you can see it
plot(spdf1)
plot(spdf2, add=TRUE, col=4)
plot(spdf3, add=TRUE, col=3)
#make list of SPDF objects
spdfl<-list()
spdfl[[1]]<-spdf1
spdfl[[2]]<-spdf2
spdfl[[3]]<-spdf3
# Empty List for Centroids
centres <-list()
# For Loop
for (i in 1:length(spdfl)) {
centres[[i]] <- spdfl[[i]] %>%
sf::st_as_sf() %>% # translate to SF object
sf::st_centroid() %>% # get centroids
as(.,'Spatial') # If you want to keep as SF objects remove this line
}
#Plot your Spatial Objects
plot(spdfl[[1]])
plot(centres[[1]], add=TRUE)
Here is a solution which uses SF and sp. SF is great as it keeps things as dataframes which can easy to deal with. More information here: https://r-spatial.github.io/sf/

trouble plotting data in the ggmap package for R

I'm a New R user so not quite comfortable with the language.
Attempting to plot the locations of bird records on a map of Manchester, England.
have managed to create a map with following code
mymap<-get_map(c(lon=53.46388,lat=-2.294037),zoom=3,col="bw")
Have read spreadsheet as an xlsx file from excel via gdata, columns containing both long and lat assigned to Lon & Lat.
Seem to be able to qplot lon&lat but not as a layer on the map, when I attempt this I get the following error
Error: ggplot2 doesn't know how to deal with data of class list
I've now tried so many combinations of code it would be impossible for me to offer a demonstrative line on how I'm attempting to attach the data onto my map, have followed tutorials online to no avail - is it a problem in my xlsx file?
Edited: Sample code :
#Here is what Jamie Dunning tried:
require(ggmap)
origin<-c("Worsley,Salford","Elton reservoir","Etherow country park","Blackleach country park","Low Hall,LNR, Wigan","Cheadle royal","Rhodes lodges,Middleton","Persons flash,Wigan","Sale water park","Plattfields","Higher Boarshaw","Clifton country park","Horrocks flash")
ringing.origins<-geocode(origin)
map<-c(get_map("Greater Manchester")
swans.coor<-cbind(ringing.origins$lon,ringing.origins$lat)
I'm yet to have an example where they are plotted successfully.
Another Alternative using plotGoogleMaps
1- Get coordinates
require(ggmap)
#List places to find GPS coordinates for:
origin<-c("Worsley,Salford","Elton reservoir","Etherow country park","Elton reservoir","Blackleach country park","Low Hall,LNR, Wigan","Cheadle royal","Rhodes lodges,Middleton","Persons flash,Wigan","Sale water park","Plattfields","Higher Boarshaw","Clifton country park","Horrocks flash")
#Get coordinates via geocode function
ringing.origins<-geocode(origin)
#Put these coordinates in a data frame for creating an SP object later
df <- as.data.frame(origin)
row.names(df) <- 1:nrow(df)
2- Create Spatial Object
require(sp)
#Coordinates must be numeric and binded together as one element and rows numbered:
ringing.origins$lon <- as.numeric(ringing.origins$lon)
ringing.origins$lat <- as.numeric(ringing.origins$lat)
coord <- cbind(ringing.origins$lon,ringing.origins$lat)
row.names(coord) <- 1:nrow(coord)
#Define a mapping projection
AACRS <- CRS("+proj=longlat +ellps=WGS84")
#Creating a spatial object of "df" using the binded coordinates "coord":
Map2 <- SpatialPointsDataFrame(coord, df, proj4string = AACRS, match.ID = TRUE)
3-Create an interactive html googlemap:
require(plotGoogleMaps)
#Simple Map
plotGoogleMaps(Map2)
#Map with some options, filename creates a file in the working directory.
plotGoogleMaps(Map2, mapTypeId="ROADMAP", colPalette="red", legend=FALSE, filename="Swan_Map.htm")
Plotting using ggmap
require(ggmap)
#Get your coordinates
origin<-c("Worsley,Salford","Elton reservoir","Etherow country park","Elton reservoir","Blackleach country park","Low Hall,LNR, Wigan","Cheadle royal","Rhodes lodges,Middleton","Persons flash,Wigan","Sale water park","Plattfields","Higher Boarshaw","Clifton country park","Horrocks flash")
ringing.origins<-geocode(origin)
#Map of Greater Manchester
map<-get_map("Greater Manchester")
ggmap(map, extent = 'normal') +
geom_point(aes(x = lon, y = lat), data = ringing.origins)
#Box is too small...
#Bounding box with All points
mymap<-get_map(c(lon=-2.294037,lat=53.46388),zoom=10)
ggmap(mymap, extent = 'device') +
geom_point(aes(x = lon, y = lat), data = ringing.origins, alpha = 1)

Label a point depending on which polygon contains it (NYC civic geospatial data)

I have the longitude and latitude of 5449 trees in NYC, as well as a shapefile for 55 different Neighborhood Tabulation Areas (NTAs). Each NTA has a unique NTACode in the shapefile, and I need to append a third column to the long/lat table telling me which NTA (if any) each tree falls under.
I've made some progress already using other point-in-polygon threads on stackoverflow, especially this one that looks at multiple polygons, but I'm still getting errors when trying to use gContains and don't know how I could check/label each tree for different polygons (I'm guessing some sort of sapply or for loop?).
Below is my code. Data/shapefiles can be found here: http://bit.ly/1BMJubM
library(rgdal)
library(rgeos)
library(ggplot2)
#import data
setwd("< path here >")
xy <- read.csv("lonlat.csv")
#import shapefile
map <- readOGR(dsn="CPI_Zones-NTA", layer="CPI_Zones-NTA", p4s="+init=epsg:25832")
map <- spTransform(map, CRS("+proj=longlat +datum=WGS84"))
#generate the polygons, though this doesn't seem to be generating all of the NTAs
nPolys <- sapply(map#polygons, function(x)length(x#Polygons))
region <- map[which(nPolys==max(nPolys)),]
plot(region, col="lightgreen")
#setting the region and points
region.df <- fortify(region)
points <- data.frame(long=xy$INTPTLON10,
lat =xy$INTPTLAT10,
id =c(1:5449),
stringsAsFactors=F)
#drawing the points / polygon overlay; currently only the points are appearing
ggplot(region.df, aes(x=long,y=lat,group=group))+
geom_polygon(fill="lightgreen")+
geom_path(colour="grey50")+
geom_point(data=points,aes(x=long,y=lat,group=NULL, color=id), size=1)+
xlim(-74.25, -73.7)+
ylim(40.5, 40.92)+
coord_fixed()
#this should check whether each tree falls into **any** of the NTAs, but I need it to specifically return **which** NTA
sapply(1:5449,function(i)
list(id=points[i,]$id, gContains(region,SpatialPoints(points[i,1:2],proj4string=CRS(proj4string(region))))))
#this is something I tried earlier to see if writing a new column using the over() function could work, but I ended up with a column of NAs
pts = SpatialPoints(xy)
nyc <- readShapeSpatial("< path to shapefile here >")
xy$nrow=over(pts,SpatialPolygons(nyc#polygons), returnlist=TRUE)
The NTAs we're checking for are these ones (visualized in GIS): http://bit.ly/1A3jEcE
Try simply:
ShapeFile <- readShapeSpatial("Shapefile.shp")
points <- data.frame(long=xy$INTPTLON10,
lat =xy$INTPTLAT10,
stringsAsFactors=F)
dimnames(points)[[1]] <- seq(1, length(xy$INTPTLON10), 1)
points <- SpatialPoints(points)
df <- over(points, ShapeFile)
I omitted transformation of shapefile because this is not the main subject here.

Plot spatial area defined by multiple polygons

I have a SpatialPolygonsDataFrame with 11589 spatial objects of class "polygons". 10699 of those objects consists of exactly 1 polygon. However, the rest of those spatial objects consist of multiple polygons (2 to 22).
If an object of consists of multiple polygons, three scenarios are possible:
1) The additional polygons could describe a "hole" in the spatial area described by the first polygon .
2) The additional polygons could also describe additional geographic areas, i.e. the shape of the region is quite complex and described by putting together multiple parts.
3) Often it is a mix of both, 1) and 2).
My question is: How to plot such a spatial object which is based on multiple polygons?
I have been able to extract and plot the information of the first polygon, but I have not figured out how plot all polygons of such a complex spatial object at once.
Below you find my code. The problem is the 15th last line.
# Load packages
# ---------------------------------------------------------------------------
library(maptools)
library(rgdal)
library(ggmap)
library(rgeos)
# Get data
# ---------------------------------------------------------------------------
# Download shape information from the internet
URL <- "http://www.geodatenzentrum.de/auftrag1/archiv/vektor/vg250_ebenen/2012/vg250_2012-01-01.utm32s.shape.ebenen.zip"
td <- tempdir()
setwd(td)
temp <- tempfile(fileext = ".zip")
download.file(URL, temp)
unzip(temp)
# Get shape file
shp <- file.path(tempdir(),"vg250_0101.utm32s.shape.ebenen/vg250_ebenen/vg250_gem.shp")
# Read in shape file
x <- readShapeSpatial(shp, proj4string = CRS("+init=epsg:25832"))
# Transform the geocoding from UTM to Longitude/Latitude
x <- spTransform(x, CRS("+proj=longlat +datum=WGS84"))
# Extract relevant information
att <- attributes(x)
Poly <- att$polygons
# Pick an geographic area which consists of multiple polygons
# ---------------------------------------------------------------------------
# Output a frequency table of areas with N polygons
order.of.polygons.in.shp <- sapply(x#polygons, function(x) x#plotOrder)
length.vector <- unlist(lapply(order.of.polygons.in.shp, length))
table(length.vector)
# Get geographic area with the most polygons
polygon.with.max.polygons <- which(length.vector==max(length.vector))
# Check polygon
# x#polygons[polygon.with.max.polygons]
# Get shape for the geographic area with the most polygons
### HERE IS THE PROBLEM ###
### ONLY information for the first polygon is extracted ###
Poly.coords <- data.frame(slot(Poly[[polygon.with.max.polygons ]]#Polygons[[1]], "coords"))
# Plot
# ---------------------------------------------------------------------------
## Calculate centroid for the first polygon of the specified area
coordinates(Poly.coords) <- ~X1+X2
proj4string(Poly.coords) <- CRS("+proj=longlat +datum=WGS84")
center <- gCentroid(Poly.coords)
# Download a map which is centered around this centroid
al1 = get_map(location = c(lon=center#coords[1], lat=center#coords[2]), zoom = 10, maptype = 'roadmap')
# Plot map
ggmap(al1) +
geom_path(data=as.data.frame(Poly.coords), aes(x=X1, y=X2))
I may be misinterpreting your question, but it's possible that you are making this much harder than necessary.
(Note: I had trouble dealing with the .zip file in R, so I just downloaded and unzipped it in the OS).
library(rgdal)
library(ggplot2)
setwd("< directory with shapefiles >")
map <- readOGR(dsn=".", layer="vg250_gem", p4s="+init=epsg:25832")
map <- spTransform(map, CRS("+proj=longlat +datum=WGS84"))
nPolys <- sapply(map#polygons, function(x)length(x#Polygons))
region <- map[which(nPolys==max(nPolys)),]
plot(region, col="lightgreen")
# using ggplot...
region.df <- fortify(region)
ggplot(region.df, aes(x=long,y=lat,group=group))+
geom_polygon(fill="lightgreen")+
geom_path(colour="grey50")+
coord_fixed()
Note that ggplot does not deal with the holes properly: geom_path(...) works fine, but geom_polygon(...) fills the holes. I've had this problem before (see this question), and based on the lack of response it may be a bug in ggplot. Since you are not using geom_polygon(...), this does not affect you...
In your code above, you would replace the line:
ggmap(al1) + geom_path(data=as.data.frame(Poly.coords), aes(x=X1, y=X2))
with:
ggmap(al1) + geom_path(data=region.df, aes(x=long,y=lat,group=group))

Can't change the coordinates of a SpatialPolygonsDataFrame in R

This is my program:
library(sp)
library(RColorBrewer)
#get spatial data for Spain on region level
con <- url("http://gadm.org/data/rda/ESP_adm1.RData")
print(load(con))
close(con)
# plot Spain with colors
col = c("blue","blue","blue","blue","blue","blue","blue","blue","blue",
"blue","blue","blue","blue","blue","blue","blue","red","red")
spplot(gadm, "NAME_1", col.regions=col, main="Regiones de EspaƱa",
colorkey=F, lwd=.4, col="black")
What I get when I run is what I am looking for, but I want to change the islands in the south west (Islas Canarias) to be closer to Spain country. I don't know what to do to change the coordinates of the islands. I just know they are here:
gadm[14,]
I try to make changes for example here:
gadm[14,]#polygons[[1]]#Polygons[[1]]#coords
but I get the message: no method for coercing this S4 class to a vector
One could also try the sf package - CRAN link here
The GADM data URL seems deprecated now, but one can manually download the RDS file containing the SpatialPolygonsDataFrame for Spain from http://gadm.org/.
library(sf)
spain <- readRDS(file = "ESP_adm1.rds") # downloaded from http://gadm.org/
# convert from sp to sf object
spain_sf <- st_as_sf(spain)
# Change the coordinates of Canary Islands - the 14th geometry of spain_sf,
# itself with 20 polygons, check str(st_geometry(spain_sf)[[14]])
# Will shift them closer to mainland Spain.
# The added values are degrees because the CRS is unprojected
# (e.g. add 10 to longitudes and 6 to latitudes)
for (i in 1:length(st_geometry(spain_sf)[[14]])) {
st_geometry(spain_sf)[[14]][[i]][[1]][, 1] <- st_geometry(spain_sf)[[14]][[i]][[1]][, 1] + 10
st_geometry(spain_sf)[[14]][[i]][[1]][, 2] <- st_geometry(spain_sf)[[14]][[i]][[1]][, 2] + 6
}
# plot to check
par(omi=c(0,0,0,2)) # make some space to the right for the legend
plot(spain_sf["NAME_1"],
axes = TRUE,
graticule = st_crs(spain_sf))
# dev.off() # resets par()

Resources