24
votes
Accepted
equivalent of sp package's point in polygon / overing using sf
You can get the same result by using st_join:
First create a demo polygon and some points with sf.
library(sf)
library(magrittr)
poly <- st_as_sfc(c("POLYGON((0 0 , 0 1 , 1 1 , 1 0, 0 0))")) %>...
17
votes
Create spatial polygon grid from spatial points in R
here is a solution using sf and sf::st_make_grid:
library(raster)
library(sf)
e <- as(raster::extent(-125, -65, 25, 49), "SpatialPolygons") %>%
st_as_sf()
grd_lrg <- st_make_grid(e, ...
12
votes
Accepted
How should I handle CRS properly after the major change in PROJ library?
Please see: https://cran.r-project.org/web/packages/rgdal/vignettes/CRS_projections_transformations.html rather than the exploratory PROJ6_GDAL3 vignette, which described processes of seeing what ...
10
votes
Accepted
Breaking out single polygon (feature) of SpatialPolygonDataframe
You need to select which feature will be plotted. For this purpose, select the observation as the same way as you select a row or more from a data.frame object:
plot(shp)
plot(shp[1,])
plot(shp[2:3,...
10
votes
Accepted
Create spatial polygon grid from spatial points in R
You can make a long/lat raster and transform
library(raster)
r <- raster(ext = extent(-125, -65, 25, 49), res=c(5,2))
values(r) <- 1:ncell(r)
rA <- projectRaster(r, crs="+proj=aea +lat_1=29....
9
votes
Accepted
Re-projecting raster in R: gives warning that projected point(s) not finite?
Short answer: it's fine, and you can thank raster for proceeding to completion instead of failure, and for letting you know that some data were lost.
Long answer:
it will depend on the projection, ...
8
votes
Accepted
Why does updating `rgdal` package in R change output of `sp::CRS` (+towgs84=0,0,0)?
The underlying library, PROJ, has changed dramatically over the past few years, and this is a consequence. The time has come to stop using proj.4 strings. See also this blog post https://www.r-spatial....
7
votes
Accepted
Converting Spatial Points to Neighbours List using R?
SpatialPoints has different way to create neighbourhood list than SpatialPolygons. I faced the same problem and this link might help you out. https://stat.ethz.ch/pipermail/r-sig-geo/2009-December/...
7
votes
Accepted
sf equivalent of R maptools package's SpatialLinesMidPoints
Looks like maptools:::getMidpoint, which underpins maptools::SpatialLinesMidPoints, can be applied to a LINESTRING geometry set without too much modification, e.g.
st_line_midpoints <- function(...
7
votes
Kriging example with sf object
After the study of https://rpubs.com/nabilabd/118172
and https://rpubs.com/nabilabd/134781 I assembled the following solution.
library(sf)
library(sp)
library(gstat)
library(tidyverse)
Here are ...
7
votes
Accepted
How to I limit the x and y axes with tmap in r
Have you read the help for tm_shape? It gives the following parameter:
bbox: bounding box. One of the following:
• A bounding box (an ‘sf’ bbox object, see ‘st_bbox’, a 2
by 2 ...
7
votes
R spatial: Erase one polygon from another: correct use of `st_difference`?
Your code worked just fine for me, so I think that make it throws that error because you have some topology error in your shape. I've just adapted your code adding a check and repair part in order to ...
7
votes
r sp::CRS() returns NA
I was able to get around the problem by using the sf::st_crs() function to transform in wkt, instead of directly using sp::CRS("+init=epsg:4326").
sp::CRS("+init=epsg:4326")
...
6
votes
Accepted
Lapply function on raster iterating through SPDF polygons in R
You can try crop and mask instead of only masking over the Raster Layers. Try the reproducible and commented code below. In this example, using crop reduced ~ 92% the size of the objects in the R ...
6
votes
Join spatial point data with multiple polygon data using R
The st_join function in the sf package (development version) provides a simple and intuitive solution. It returns the spatial join as a data.frame, and you can easily convert back to Spatial*.
# ...
6
votes
equivalent of sp package's point in polygon / overing using sf
Instead of using
st_join(pts, poly, join = st_intersects)
you can just use
st_intersection(pts, poly)
It is faster, and gives only points that are inside the poly.
5
votes
Re-projecting raster in R: gives warning that projected point(s) not finite?
This is not a full answer to my original question, in terms of all the gritty details, but it does provide the interested reader somewhere to go.
In general data is lost and distorted during ...
5
votes
Accepted
Enlarging polygon slightly using R?
Use the gBuffer function from the rgeos package. It has a width argument to expand the polygons by a margin.
Use byid = TRUE to maintain the identity of the input polygons, else they get all merged ...
5
votes
Accepted
Calculating Proportion of Land Cover Classes with moving window around point in R?
For calculating proportions around points you can define a function in the raster::extract function.
First, create an example raster with values of [1,2] and generate a random point sample.
library(...
5
votes
Accepted
Set the minimum and maximum values for an elevation raster in R
Try the commented and reproducible example below. You can assign NA values to your elevation RasterLayer as elev[elev < 0] <- NA and elev[elev > 1200] <- NA.
# Load libraries
library('...
5
votes
Accepted
Is there an equivalent of dplyr::bind_rows (or the old plyr::rbind.fill) for spatial objects?
Since (I think) NA in geometry is not supported, you can bind_rows the data frames and then add on the geometry and convert.
nc1 and nc2 are two sf objects with differing columns:
> nc1
Simple ...
5
votes
Merge a list of SpatialLines
As I mentioned in my comment, you can use do.call for this. Here is an example.
First, add sp library and create a list with three SpatialLines objects (would work with Lines object as well).
...
5
votes
Accepted
Adding labels to spatial grid in leaflet using R?
As you correctly stated, you're almost there. Popup windows can be easily realized through mapview, which is built upon leaflet. I usually tend to transform input polygons to 'SpatialPolygonsDataFrame'...
5
votes
Accepted
Creating lines between pairs of points
Here is a tidyverse method of doing it, starting with your table from before converting to sf. The approach is to create a long-form table where each row is a start or end point, but include a lineid ...
5
votes
Accepted
Plotting spatial points data in R?
Converting the data to an sf object, you can either use ggplot as the answer before or ggmap in the following way:
library(sp)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(...
5
votes
Accepted
sp::proj4string equivalent in the PROJ6 framework?
I was also quite confused about the move away from PROJ Strings and what to use instead until I watched this video of a talk held last year at FOSS4G.
Here's my main takeaways:
Why PROJ and GDAL are ...
5
votes
Accepted
Checking whether CRS is geographic coordinate system or projected coordinate system using R
The raster package has a function isLonLat for this purpose.
Analogous for vector data, there is the st_is_longlat function from sf.
4
votes
Counting number of points in polygon using R?
You can achieve the same using the sf package. Check the reproducible and commented code below. The package sf is used to handle spatial objects as simple features objects. In this answer the package ...
4
votes
I want to do SpatialLines programatically
I am sure that one could write a function that would do this using lapply but I am not sure that it is worth the trouble. In this case, I am not sure that an apply like approach would buy you much.
...
4
votes
Join spatial point data with multiple polygon data using R
Use over(meuse, srdf, returnList=TRUE)
if ‘returnList’ is TRUE, a list of length
‘length(x)’, with list element ‘i’ the vector of all indices of
the geometries in ‘y’ that correspond to the $i$-th ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sp × 308r × 299
sf × 65
raster × 61
rgdal × 35
polygon × 32
coordinate-system × 27
rgeos × 25
shapefile × 16
ggplot2 × 14
ggmap × 13
line × 12
spatial-statistics × 9
spatstat × 9
point × 8
intersection × 8
proj × 7
overlay × 7
gdal × 6
coordinates × 6
point-in-polygon × 6
gstat × 6
terra × 6
spatial-analyst × 5
distance × 5