reduce number of points of a geometry of sf object in R - r

I am facing the following problem:
when using the shape file of intercommunal limits in France (which you can download here: https://www.data.gouv.fr/fr/datasets/r/971027a8-3ceb-48c6-97e3-59deaf7e2704), plotting the map is really slow:
library(sf)
library(ggplot2)
epci_france <- read_sf("./epci_shape/EPCI_SHAPEFILE.shp")
ggplot()+
geom_sf(data = test,
aes(geometry = geometry),
color = "black")+
guides(fill = "none")+
theme_void()
I think this is due to the high number of points in the shapefile.
Following comments in here: https://github.com/tidyverse/ggplot2/issues/2655, I tried
sum(rapply(st_geometry(epci_france), nrow))
[1] 1913182
which looks like a lot of points. I am looking for a way to lower this number, i.e. lower the precision of the limits of the geometry. I tried smooth from library(smoothr), but it has the opposite effect: it increases the number of points. I tried to cast it to other formats with st_cast, but it did not work either.
How should I proceed? My objective is to have a simple delimitation of the intercommunal limits to make choropleth maps. Thanks!

Related

Map FAO fishing areas in R

I would like to make a map in R that colours in the FAO Fishing Areas according to a data set (in my case, length data of shark species).
I would prefer to do a choropleth map in ggplot but other types of maps are also fine. Worst case scenario a base map of FAO areas that I can add bubbles to. Even just an existing base map of FAO areas would be great. Any suggestions welcome!
I went to this page and clicked through to find this link to retrieve a GeoJSON file:
download.file("http://www.fao.org/fishery/geoserver/fifao/ows?service=WFS&request=GetFeature&version=1.0.0&typeName=fifao:FAO_AREAS_CWP&outputFormat=json", dest="FAO.json")
From here on, I was following this example from the R graph gallery, with a little help from this SO question and these notes:
library(geojsonio)
library(sp)
library(broom)
library(ggplot2)
library(dplyr) ## for joining values to map
spdf <- geojson_read("FAO.json", what = "sp")
At this point, plot(spdf) will bring up a plain (base-R) plot of the regions.
spdf_fortified <- tidy(spdf)
## make up some data to go with ...
fake_fish <- data.frame(id = as.character(1:324), value = rnorm(324))
spdf2 <- spdf_fortified %>% left_join(fake_fish, by = "id")
ggplot() +
geom_polygon(data = spdf2, aes( x = long, y = lat, group = group,
fill = value), color="grey") +
scale_fill_viridis_c() +
theme_void() +
theme(plot.background = element_rect(fill = 'lightgray', colour = NA)) +
coord_map() +
coord_sf(crs = "+proj=cea +lon_0=0 +lat_ts=45") ## Gall projection
ggsave("FAO.png")
notes
some of the steps are slow, it might be worth looking up how to coarsen/lower resolution of a spatial polygons object (if you just want to show the picture, the level of resolution might be overkill)
to be honest the default sequential colour scheme might be better but all the cool kids seem to like "viridis" these days so ...
There are probably better ways to do a lot of these pieces (e.g. set map projection, fill in background colour for land masses, ... ?)

How to combine sf elements (layers) in R

despite having some experience with R, I am much less experienced using R for GIS-like tasks.
I have a shapefile of all communities within Germany and created a new object that only shows the borders of the 16 states of Germany.
gem <- readOGR(path/to/shapefile.shp) # reading shapefile
gemsf <- st_read(path/to/shapefile.shp) # reading shapefile as sf object
f00 <- gUnaryUnion(gem, id = gem#data$SN_L) # SN_L is the column of the various states - this line creates a new sp object with only the states instead of all communities
f002 <- sf::st_as_sf(f00, coords = c("x","y")) # turning the object into an sf object, so graphing with ggplot is easier
To check my work so far I plotted the base data (communities) using
gemsf %>%
ggplot(data = .,) + geom_sf( aes(fill = SN_L)) # fill by state
as well as plot(f002) which creates a plot of the 16 states, while the ggplot-code provides a nice map of Germany by community, with each state filled in a different color.
Now I'd like to overlay this with a second layer that indicates the borders of the states (so if you e.g. plot population density you can still distinguish states easily).
My attempt to do so, I used "standard procedure" and added another layer
ggplot() +
geom_sf(data = gemsf, aes(fill = SN_L)) + # fill by state
geom_sf(data = f002) # since the f002 data frame/sf object ONLY has a geometry column, there is no aes()
results in the following output: https://i.ibb.co/qk9zWRY/ggplot-map-layer.png
So how do I get to add a second layer that only provides the borders and does not cover the actual layer of interest below? In QGIS or ArcGIS, this is common procedure and not a problem, and I'd like to be able to recreate this in R, too.
Thank you very much for your help!
I found a solution which I want to share with everyone.
ggplot() +
geom_sf(data = gemsf_data, aes(fill = log(je_km2))) + # fill by state
geom_sf(data = f002, alpha = 0, color = "black") + # since the f002 data frame/sf object ONLY has a geometry column, there is no aes()
theme_minimal()
The trick was adding "alpha" not in the aes() part, but rather just as shown above.

r convert raster stack or brick to an animation

I have downloaded some NetCDF files of land use scenario results from http://luh.umd.edu/data.shtml. Each file at this location has values for 11 land use types with annual values from 2015 to 2100. I'd like to make an animated gif or movie that shows the changes over time. This seems like it should be straightforward but I've tried a variety of routes, none of which work, so I'm hoping for some that actually work.
1. One approach involves creating a raster stack or brick of one of the land use variables using the stack or brick functions from the raster package. then using the raster animate function. Unfortunately, I get the following error message "animation of RasterLayer [stack, brick] objects not supported".
Another option is to convert each year of the land use data into a SpatialPixelDataFrame and then into a data.frame, use ggplot to create a gif and then combine the gifs into an animated gif. But this process seems extremely convoluted.
An R script that contains my current efforts to do this is here.
Having a look through your code, I can make the code below work.
Basically, I'm making a big dataframe with data for all years.
mydf <- purrr::map_dfr(
as.list(ncin.brick),
~setNames(as.data.frame(as(., "SpatialPixelsDataFrame")), c('value', 'x', 'y')),
.id = 'year'
)
gg <- ggplot(
mydf,
aes(x = x, y = y, fill = value)
) +
geom_sf(data = borders, fill = "transparent", color = "black", inherit.aes = FALSE) +
geom_tile() +
scale_fill_viridis_c() +
ggthemes::theme_map()
gganim <- gg + transition_time(as.numeric(year)) #+ labs(title = "Year: {frame_time}")
gganim
The picture below is the result (animation is subtle).
Try raster::animate(), there are several incompatible animate functions across packages and this seems like a clash.
I usually animate using a loop to plot raster slices, and capture that using the animate package, e.g. with saveHTML().
For ggplot2 see the gganimate package, but it doesn't scale well given the need to expand the data out.

Identifying grids into a map/shapefile

I have some questions about maps/shapefiles. I'm not a expert in R, so, to make it easier do understand what I'm going for, I'll enumerate:
1- Identify each grid on the map, and maybe omit some of these grids.
2- Color each grid by values from a data frame
I just made what i'm trying to make using Photoshop to help to illustrate my goal here
I made this map using the 'intersect' function with a shapefile I got from internet and a grid i made using 'rastertoPolygons' function, but I'm not sure if using a .shp is the best way to get what I want, altough it has been the only way I found to do this, since I got lost trying with ggplot2 options (and I'm very familiar with the package)
Any help or suggestion will be awesome.
Sorry if I made a stupid question and sorry for my bad english.
If you are asking how you can do this in ggplot, you can pretty easily. If not, can you clarify what you are asking?
You can draw the map of Brazil easily, and use your shapefile either directly, or with some adjustments. Since I don't have your shapefile, I'll use one of my own and you can adjust for yourself. I just made two arbitrary boxes, and labelled them with a field called id. Your grouping name may be different.
library(ggplot2)
library(maps)
library(rgdal)
brasilia <- borders("world", regions = "Brazil")
brazil <- ggplot() + brasilia + theme_bw() + xlab("Longitude (decimals)") + ylab("Latitude (decimals)") +
theme(panel.border = element_blank(), panel.grid.major = element_line(colour = "grey80"), panel.grid.minor = element_blank()) +
coord_fixed(1.0)
brazil # You can see just the map of Brazil
Next, import your shapefile using rgdal, which should read all the metadata so you don't have to tell it what projection, etc. Just tell it where it is, and what the shape file name is. See ?readOGR for help.
shapes <- readOGR(dsn = "C:/foo/GIS/Brazil", layer = "brazil_grid")
brazil_shapes <- brazil + geom_path(data = shapes, aes(x = long, y = lat, group = id), color = "red")
brazil_shapes
Filling them with the colors you want may take the most work, creating a table to map your fill levels to the grids. It looks like this answer can point you in the right direction though. R ggplot2 merge with shapefile and csv data to fill polygons
Here's a good overview of mapping in R. http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html

Plotting OpenStreetMap with ggmap

I'm trying to get districts of Warsaw and draw them on google map. Using this code, where 2536107 is relation code for OpenStreetMap single Warsaw district, gives me almost what I want but with a few bugs. There is general outline but also lines between points which shouldn't be connected. What am I doing wrong?
map <- get_googlemap('warsaw', zoom =10)
warszawa <- get_osm(relation(2536107), full = T)
warszawa.sp <- as_sp(warszawa, what='lines')
warsawfort <- fortify(warszawa.sp)
mapa_polski <- ggmap(map, extent='device', legend="bottomleft")
warsawfort2 <- geom_polygon(aes(x = long, y = lat),
data = warsawfort, fill="blue", colour="black",
alpha=0.0, size = 0.3)
base <- mapa_polski + warsawfort2
base
Edit: I figured it must be somehow connected with order of plotting every point/line but I have no idea how to fix this.
There is a way to generate your map without using external packages: don't use osmar...
This link, to the excellent Mapzen website, provides a set of shapefiles of administrative areas in Poland. If you download and unzip it, you will see a shapfile set called warsaw.osm-admin.*. This is a polygon shapefile of all the districts in Poland, conveniantly indexed by osm_id(!!). The code below assumes you have downloaded the file and unzipped it into the "directory with your shapefiles".
library(ggmap)
library(ggplot2)
library(rgdal)
setwd(" <directory with your shapefiles> ")
pol <- readOGR(dsn=".",layer="warsaw.osm-admin")
spp <- pol[pol$osm_id==-2536107,]
wgs.84 <- "+proj=longlat +datum=WGS84"
spp <- spTransform(spp,CRS(wgs.84))
map <- get_googlemap('warsaw', zoom =10)
spp.df <- fortify(spp)
ggmap(map, extent='device', legend="bottomleft") +
geom_polygon(data = spp.df, aes(x = long, y=lat, group=group),
fill="blue", alpha=0.2) +
geom_path(data=spp.df, aes(x=long, y=lat, group=group),
color="gray50", size=0.3)
Two nuances: (1) The osm IDs are stored as negative numbers, so you have to use, e.g.,
spp <- pol[pol$osm_id==-2536107,]
to extract the relevant district, and (2) the shapefile is not projected in WGS84 (long/lat). So we have to reproject it using:
spp <- spTransform(spp,CRS(wgs.84))
The reason osmar doesn't work is that the paths are in the wrong order. Your warszawa.sp is a SpatialLinesDataframe, made up of a set of paths (12 in your case), each of which is made up of a set of line segments. When you use fortify(...) on this, ggplot tries to combine them into a single sequence of points. But since the paths are not in convex order, ggplot tries, for example, to connect a path that ends in the northeast, to a path the begins in the southwest. This is why you're getting all the extra lines. You can see this by coloring the segments:
xx=coordinates(warszawa.sp)
colors=rainbow(11)
plot(t(bbox(warszawa.sp)))
lapply(1:11,function(i)lines(xx[[i]][[1]],col=colors[i],lwd=2))
The colors are in "rainbow" order (red, orange, yellow, green, etc.). Clearly, the lines are not in that order.
EDIT Response to #ako's comment.
There is a way to "fix" the SpatialLines object, but it's not trivial. The function gPolygonize(...) in the rgeos package will take a list of SpatialLines and convert to a SpatialPolygons object, which can be used in ggplot with fortify(...). One huge problem (which I don't understand, frankly), is that OP's warszaw.sp object has 12 lines, two of which seem to be duplicates - this causes gPolygonize(...) to fail. So if you create a SpatialLines list with just the first 11 paths, you can convert warszawa.sp to a polygon. This is not general however, as I can't predict how or if it would work with other SpatialLines objects converted from osm. Here's the code, which leads to the same map as above.
library(rgeos)
coords <- coordinates(warszawa.sp)
sll <- lapply(coords[1:11],function(x) SpatialLines(list(Lines(list(Line(x[[1]])),ID=1))))
spp <- gPolygonize(sll)
spp.df <- fortify(spp)
ggmap(map, extent='device', legend="bottomleft") +
geom_polygon(data = spp.df, aes(x = long, y=lat, group=group),
fill="blue", alpha=0.2) +
geom_path(data=spp.df, aes(x=long, y=lat, group=group),
color="gray50", size=0.3)
I am not sure this is a general hangup--I can reproduce your example and see the issue. My first thought was that you didn't supply group=id which are typically used for polygons with many lines, but you have lines, so that should not be needed.
The only way I could get it to display properly was by changing your lines into a polygon off script. Qgis' line to polygon didn't get this "right", getting a large donut hole, so I used ArcMap, which produced a full polygon. If this is a one off that may work for your workflow. Odds are it is not. In that case, perhaps RGDAL can transform lines to polygons, assuming that is indeed a general problem.
Upon reading the polygon shapefile and fortifying that, your code ran without problems.

Resources