Tag Info

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))")) %>...
• 1,733

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, ...
• 1,733
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 ...
• 281
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,...
• 13.8k
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....
• 10.9k
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,226
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....
• 2,079
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/...
• 86
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(...
• 3,772

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 ...
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 ...
• 65.6k

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 ...
• 3,494

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") ...
• 149
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 ...
• 3,176

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*. # ...
• 1,143

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.
• 410

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 ...
• 143
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 ...
• 8,226
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(...
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('...
• 3,176
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 ...
• 65.6k

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). ...
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,203
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 ...
• 166
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(...
• 251
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 ...
• 3,794
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.
• 2,317

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 ...
• 3,176