R cran: sf Sew two MULTILINESTRING/LINESTRING - r

I try to sew/merge/bind two LINESTRINGs together (spanish coast + french coast) to have the full map of the whole coast. I found all the shpaefiles here:
https://www.marineregions.org/gazetteer.php?p=details&id=3417
https://www.marineregions.org/gazetteer.php?p=details&id=19888
Import and plot the data
frenchCoast_CoteBanyuls <- st_read("coasts_subnational/coasts_subnational.shp") %>%
st_geometry() #
plot(frenchCoast_CoteBanyuls)
spainCoast_CoteBanyuls <- st_read("coasts_subnational SPAIN/coasts_subnational.shp")%>%
st_geometry() #
spainCoast_CoteBanyuls <- st_cast(x = spainCoast_CoteBanyuls, to = "LINESTRING")
plot(spainCoast_CoteBanyuls, add = T)
This map and features seem ok.
According to all the posts I found, I tried to merge the two shapefiles together:
CoteBanyuls_c <- c(frenchCoast_CoteBanyuls, spainCoast_CoteBanyuls)
# CoteBanyuls_rbind <- rbind(frenchCoast_CoteBanyuls, spainCoast_CoteBanyuls)
# CoteBanyuls <- st_union(CoteBanyuls)
# CoteBanyuls <- union(CoteBanyuls)
# CoteBanyuls <- bind(CoteBanyuls)
plot(CoteBanyuls)
the c() function gives a shapefile that does not have the good dimensions (see below) and none of these solutions does plot the good shape of the two coast merged (seems to plot only the spanish coast).
What did I do wrong ? I do not understand why these solutions are not working...
Do you have some ideas ?
Thanks in advance !
Charlotte

I think the issue comes from extracting only the geometry attributes before you combine them. Try this:
frenchCoast_CoteBanyuls <- st_read("coasts_subnational/coasts_subnational.shp")
spainCoast_CoteBanyuls <- st_read("coasts_subnational SPAIN/coasts_subnational.shp")
combined_coast <- rbind(spainCoast_CoteBanyuls, frenchCoast_CoteBanyuls)
mapview::mapview(combined_coast)

Related

Extract CMIP6 information with R

I'm doing a research about climate change and I want to extract the CMIP6 information using R but I don't get the results I want. This is the code.
library(raster)
library(ncdf4)
long_lat <- read.csv("C:/Users/USUARIO/Desktop/Tesis/Coordenadas - copia.csv", header = T)
raster_pp <- raster::brick("pr_day_ACCESS-CM2_historical_r1i1p1f1_gn_2000.nc")
sp::coordinates(long_lat) <- ~XX+YY
raster::projection(long_lat) <- raster::projection(raster_pp)
points_long_lat <- raster::extract(raster_pp[[1]], long_lat, cellnumbers = T)[,1]
data_long_lat <- t(raster_pp[points_long_lat])
The cordinates I've used are the following: -77.7754,-9.5352 (latitude, longitude), but I've tried these as well and still doesn't work 102.2246,-9.5352. Thanks for your response.

R (Geosphere Library) - "Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed"

I have an issue that I have tried for seven hours to fix without any success. Basically, I am trying to visualize the airport and flight data from openflights.org. This was supposed to be a simple, straightforward visualization, but it has turned in to a real hair puller.
Everything works up until the for loop. When I try and run the for loop in order to depict the flight paths on my map, it runs for a little and some lines appear on the map, but then it quits with the error:
Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed
What I tried to do to fix it: As you can see, I have gone through the data set and removed any bad entries. For example, there were some IDs that were "\\N" and therefore I removed those entries entirely. Then I tried to change the IDs to be numbers rather than strings, just to see what would happen.
It always errors out around the same time after I run the for loop, is there a way I can view the line it got to when it returned the error? I am new too R.
I know a similar question has been asked before but none of the solutions they had there worked for me. Thank you for any help you can provide.
Code:
library('geosphere')
library('maps')
# Reading in the files from openflights.org
airports <- read.csv("airports.dat.txt", header=FALSE, col.names=c("Airport ID","Name","City","Country","IATA","ICAO","Latitude","Longitude",
"Altitude","Timezone","DST", "TZ Database","Type","Source"), as.is=TRUE)
flights <- read.csv("routes.dat.txt", header=FALSE, col.names=c("Airline","Airline ID","Source Airport","Source Airport ID","Destination Airport",
"Destination Airport ID","Codeshare","Stops","Equipment"), as.is=TRUE)
# Cleaning the data set, there are some instances of the value "\\N" in the flights data set.
flights <- flights[!grepl("\\N", flights$Source.Airport.ID),]
flights <- flights[!grepl("\\N", flights$Destination.Airport.ID),]
# Converting all of the IDs to numbers (I thought this might work but it did not)
flights$Source.Airport.ID <- as.numeric(flights$Source.Airport.ID)
flights$Destination.Airport.ID <- as.numeric(flights$Destination.Airport.ID)
airports$Airport.ID <- as.numeric(airports$Airport.ID)
# Creating a world background
map("world", col="white", border="gray10", fill=TRUE, bg="gray30")
# Adding all of the airports as points
points(x=airports$Longitude, y=airports$Latitude, pch=19,
cex=0.05, col="blue")
# Generating a color set to be used for mapping the flight paths
col.1 <- adjustcolor("limegreen", alpha=0.05)
col.2 <- adjustcolor("darkgreen", alpha=0.05)
edge.pal <- colorRampPalette(c(col.1, col.2), alpha = TRUE)
edge.col <- edge.pal(100)
# Now, generating the visualization of the flight paths.
# Here is where the error occurs, when I run this.
# It gets through some of the data but then errors out.
for(i in 1:nrow(flights)) {
node1 <- airports[airports$Airport.ID == flights[i,]$Source.Airport.ID,]
node2 <- airports[airports$Airport.ID == flights[i,]$Destination.Airport.ID,]
arc <- gcIntermediate( c(as.numeric(node1[1,]$Longitude), as.numeric(node1[1,]$Latitude)),
c(as.numeric(node2[1,]$Longitude), as.numeric(node2[1,]$Latitude)),
n=1000, addStartEnd=TRUE)
#edge.ind <- round(100*table(flights[i,]$Source.Airport.ID) / table(max(airports$Airport.ID)))
#lines(arc, col=edge.col[edge.ind], lwd=edge.ind/30)
lines(arc, col = "limegreen", lwd = 0.02)
}

Spatial data overlay selection in R

I'm trying to overlay some spatial data from a bigger SpatialPolygonsDataFrame (world size) to a smaller (country size), by doing these:
x <- c("rgdal", "dplyr",'ggplot2')
apply(x, library, character.only = TRUE)
est<-readOGR(dsn='/estados_2010',layer='estados_2010')
est_f<-fortify(est)
est$id<-row.names(est)
est_f<-left_join(est_f,est#data)
zon<-readOGR(dsn='/Zonas Homogeneas/gyga_ed_poly.shp',layer='gyga_ed_poly')
zon_f<-fortify(zon)
zon$id<-row.names(zon)
zon_f<-left_join(zon_f,zon#data)
t<-ggplot()+geom_polygon(data=zon_f,aes(x=long,y=lat,group=group,fill=GRID_CODE))
t+geom_polygon(data=est_f,aes(x=long,y=lat,group=group),fill=NA,color='red')+coord_fixed(xlim=est_f$long,ylim=est_f$lat,1)
Which is resulting in this:
I'm want to select only what is being plotted inside the polygon with the red lines.
If someone could help me with this issue, I'll appreciate
PS.: For those who want to reproduce the example completely by yourselves, the files are available in the links above to my google drive:
https://drive.google.com/open?id=0B6XKeXRlyyTDakx2cmJORlZqNUE
Thanks in advance.
Since you are using polygons to display the raster values, you can use a spatial selection via [ like in this reproducible example:
library(raster)
library(rgdal)
bra <- getData("GADM", country = "BRA", level = 1)
r <- getData("worldclim", res = 10, var = "bio")
r <- r[[1]]
r <- crop(r, bra)
r <- rasterToPolygons(r)
# bra and raster (now as polygons) have to have the same projection, thusly reproject!
bra <- spTransform(bra, CRSobj = proj4string(r))
here comes the magic!!
r <- r[bra, ]
let's look at the results:
library(ggplot2)
t <- ggplot()+
geom_polygon(data=r,aes(x=long,y=lat,group=group, fill = rep(r$bio1, each = 5)))
t +
geom_polygon(data=bra,aes(x=long,y=lat,group=group),fill=NA,color='red') + coord_map()

Advice on troubleshooting dotsInPolys error (maptools)

I am pretty new to R and am still learning some of the ways to troubleshoot problems I encounter. I'm running into one that I'm stuck on and wondered if anyone has suggestions.
I am trying to build a dot density map, but I'm running into an error with the dotsInPolys function. The line:
scc.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random")
Which gives me the error:
> sccdots.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random")
Error in dotsInPolys(sccpolys, as.integer(plotvar), f = "random") :
different lengths
The documentation indicates that sccpolys and plotvar need to be the same length, but I'm unsure on how to double-check that, or, more importantly, correct the problem. Does anyone have recommendations on how I can check what's wrong? Thanks ahead of time.
Here's the entire set of code I'm working on:
library(maptools)
# Population data
sccpop <- read.csv("nhgis0010_ds98_1970_tract.csv", stringsAsFactors = FALSE)
sccpop.sub <- sccpop[sccpop$COUNTY=="Santa Clara",c(1,3,21,22,23)]
# Shapefile for Census tracts
scctract.shp <- readShapePoly("1970-ca-tracts.shp")
sccpolys <- SpatialPolygonsDataFrame(scctract.shp, data=as(scctract.shp, "data.frame"))
# Merge datasets
sccdata <- merge(sccpolys#data, sccpop.sub, sort=FALSE)
plotvar <- sccdata$C0X001 / 1000 # one dot per 1,000 people
head(sccpolys#data)
head(sccpop.sub)
# Generate random dots in polygons
sccdots.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random")
# County boundaries
baycounties.shp <- readShapePoly("ca-counties-1970.shp")
baycounties <- SpatialPolygonsDataFrame(baycounties.shp, data=as(baycounties.shp, "data.frame"))
par(mar=c(0,0,0,0))
plot(baycounties, lwd=0.1)
# Add dots
plot(sccdots.rand, add=TRUE, pch=19, cex=0.1, col="#00880030")
#LincolnMullen is right. After your merge you have:
> length(sccpolys)
[1] 787
and
> length(plotvar)
[1] 210
To account for this you could replace
sccdots.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random")
with
sccdots.rand <- dotsInPolys(sccpolys[sccpolys$GISJOIN %in% sccdata$GISJOIN,], as.integer(plotvar), f="random")
The problem is that you have more tracts (i.e., polygons in your shapefile) than you wish to plot. There are 787 tracts and only 210 tracts in Santa Clara. In addition, there is some manipulation of the #data slot in the SpatialPolygonsDataFrame that is unnecessary. Here is a solution that cleans up the merge code.
library(maptools)
shp <- readShapePoly("1970-ca-tracts.shp")
sccpop <- read.csv("nhgis0010_ds98_1970_tract.csv", stringsAsFactors = FALSE)
sccpop.sub <- sccpop[sccpop$COUNTY=="Santa Clara",c(1,3,21,22,23)]
shp <- merge(shp, sccpop.sub)
Now we have a SpatialPolygonsDataFrame with the data, but there are missing values for all non Santa Clara counties. And we want to transform the population as you did above. The first line below does the transformation, adding a column to the data frame. It's easiest just to keep that inside the data frame instead of as an external variable. The second line filters out all the polygons that don't have population associated with them, i.e., the non-Santa Clara counties.
shp#data$plotvar <- as.integer(shp#data$C0X001 / 1000)
shp <- shp[!is.na(shp#data$plotvar), ]
Now we can proceed as you did before.
sccdots.rand <- dotsInPolys(shp, shp#data$plotvar, f="random")
baycounties.shp <- readShapePoly("ca-counties-1970.shp")
par(mar=c(0,0,0,0))
plot(baycounties.shp, lwd=0.1)
plot(sccdots.rand, add=TRUE, pch=19, cex=0.1, col="#00880030")
FWIW, I have had better results using rgdal::readOGR() for loading shapefiles, but maptools::readShapePoly() works fine here.

How can I add directional arrows to lines drawn on a map in R?

I've got a map that I've built using the maps and geosphere packages that looks like an airline map. However, I'd like to add arrows to the lines to show the directions of the "routes" in my map. You can see my current working code below (based off of the fabulous tutorial from FlowingData). I've tried before to use the arrows function in lieu of the lines function, yet I'm not sure how to make the arrows go with the geosphere curve, or ensure that the arrows are spaced along the line so that they look like this:
-->-->-->
I'm incredibly new to R, so any and all assistance would be greatly appreciated. Thanks in advance.
library(maps)
library(geosphere)
read.csv("http://marsiccr.github.io/Data/airports.csv", header=TRUE, as.is=TRUE) -> airports
read.csv("http://marsiccr.github.io/Data/leaders.csv", header=TRUE, as.is=TRUE) -> flights
pal <- colorRampPalette(c("#f2f2f2", "blue"))
colors <- pal(100)
colleges<-NULL
colleges$name <- airports$insname
colleges$long <- airports$long
colleges$lat <- airports$lat
colleges
map("state")
map("state", col="#f2f2f2", fill=TRUE, bg="white", lwd=0.25)
fsub <- flights[flights$type == "aau",]
fsub <- fsub[order(fsub$cnt),]
maxcnt <- max(fsub$cnt)
for (j in 1:length(fsub$type)) {
air1 <- airports[airports$unitid == fsub[j,]$school1,]
air2 <- airports[airports$unitid == fsub[j,]$school2,]
inter <- gcIntermediate(c(air1[1,]$long, air1[1,]$lat), c(air2[1,]$long, air2[1,]$lat), n=100, addStartEnd=TRUE)
colindex <- round( (fsub[j,]$cnt / maxcnt) * length(colors) )
lines(inter, col=colors[colindex], lwd=0.8)
}
Slipping this code into the for-loop just after inter<- got me arrowheads (and a few warnings)
tinter <- tail(inter,2)
arrows(tinter[1,1], tinter[1,2], tinter[2,1], tinter[2,2])
Obviously there's some tweaking to be done. See ?arrowsfor the full range of options. You could also use the second to last (or the fifth to last?) points in the inter matrix. You might also want to onlyy put in arrowheads for selected routes.

Resources