The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [sp]

R package with classes and methods to create and manipulate spatial data

Filter by
Sorted by
Tagged with
16
votes
3answers
15k views

Merge a list of spatial polygon objects in R

I have a list of spatial buffers (30000 buffers) that I built with the function lapply: buff.pts <- lapply(1:nrow(pts.prj), FUN=function(l){ buff <- gBuffer(pts.prj[l,], width=1000) ## 1km ...
15
votes
1answer
6k views

equivalent of sp package's point in polygon / overing using sf

I am migrating code from sp package to the newer sf package. My previous code I had a polygon SpatialDataFrame (censimentoMap) and a SpatialPointDataFrame (indirizzi.sp) and I got the polygon cell id (...
12
votes
2answers
3k views

How does spatial polygon %over% polygon work to when aggregating values in r?

I'm working on an environmental epidemiology project where I have point exposures (~ 2,000 industrial hog operations - IHOs). These IHOs spray on nearby fields, but the feces water droplets and smell ...
10
votes
3answers
16k views

Counting number of points in polygon using R?

I have two classes sharing the same CRS (Latitute and Longitude): bolognaQuartieriMap: a SpatialPolygonDataFrame containing data of a city boroughts. crashPoints: a SpatialPointsDataFrame containing ...
9
votes
2answers
7k views

buffer and union in R without reducing to a multi-polygon

I would like to perform a spatial overlay to identify the polygon within which a set of points fall. However I first want to buffer and dissolve the polygons, such that points falling anywhere within ...
8
votes
2answers
3k views

Re-projecting raster in R: gives warning that projected point(s) not finite?

1. Question I have encountered a warning using the projectRaster() function in the raster package in R. A full reproducible example is pasted below. Warning message: In rgdal::rawTransform(...
8
votes
1answer
5k views

Distance to nearest point for every point same SpatialPointsDataFrame in R

I have a SpatialPointsDataFrame that I'm working with in R. I want to add a new vector to the dataframe that contains, for every point, the distance to the nearest other point in the ...
8
votes
3answers
3k views

Join spatial point data with multiple polygon data using R

I want to spatial join with SpatialPolygonsDataFrame and SpatialPointsDataFrame. get only one point data within multiple polygons, under the code. sp::over(meuse, srdf) Some points data into ...
8
votes
4answers
16k views

Join CSV file to shapefile

I wanted to join a CSV file to a shapefile. When I identified a field to facilitate the join, e.g., map <- spChFIDs(map, as.character(map$ID)) it returned Error in spChFIDs(SP, x) : lengths ...
8
votes
1answer
5k views

Remove certain polygon from SpatialPolygonDataFrame

I am using R maptools library to parse the shapefile into a list of polygons. The function readShapeSpatial shp <- readShapeSpatial("<path to my shapefile>") gives me a ...
6
votes
1answer
1k views

Kriging example with sf object

I am new to geo spacial analysis. I would like to learn kriging from a simple example. Say, we have four points with some z value. library(sf) library(gstat) library(tidyverse) dt<-tibble::...
5
votes
3answers
4k views

Converting SpatialLinesDataFrame to SpatialPointsDataFrame in R?

In R I have a SpatialLinesDataFrame (for example from a coastline shapefile) and I want to convert it to a SpatialPointsDataFrame. I want to keep all the vertices from the line shapefile to become ...
5
votes
3answers
1k views

Dissolving/Unifying Ill Behaved/Irregular Polygons in R

I'm working on a project involving bringing together multiple subjects circling difficult to find parts of an image. The output from users is pretty awesome. Cool, right? However, what I want is to ...
5
votes
2answers
833 views

How to efficiently convert origin-destination coordinates into lines in R

Reproducible example to show what I mean: # Create origins and destinations odf = data.frame(fx = 1:5, fy = 1:5, tx = 0:4, ty = rep(1, 5)) The challenge is to convert this into straight lines ...
5
votes
3answers
1k views

How can I snap one set of points to another in R

I would like to snap one set of points (SpatialPoints* objects) to another in R using Euclidean distance. I'm hoping for a function like maptools::snapPointsToLines. The attributes of the points don'...
5
votes
1answer
1k views

Slow plotting of spatial data in macOS RStudio's plotting device

In my macOS system RStudio, plotting spatial data (statialpolygons, points and lines) is extremely slow, making it impossible to work with. When I try to plot the shapefile in this link (@https://...
5
votes
1answer
2k views

Creating empty Polygons or SpatialPolygons in R?

I'm working on a project where I'm looking at overlap in users circling an area in an image. Esentially, I make a SpatialPolygonsDataFrame where each user gets one row. Some users don't select ...
5
votes
1answer
434 views

R spTransform polygon that crosses dateline

I have created polygons with CRS = UTM (see here), which I need to transform to latitude/longitude. Some of my polygons cross the dateline, which causes the transformation to create a polygon that ...
5
votes
1answer
275 views

Polygon and raster in the same plot do not align

I can't manage to plot a raster and boundary of country (sp data frame) in the same plot. The data (lon,lat,value) are in mat data frame. The grid is not regular and comes from a climate change ncdf ...
4
votes
2answers
3k views

Create spatial polygon grid from spatial points in R

I am trying to recreate a map in R. I need 2 by 5 degree grid cells across the contiguous United States, but plotted in Albers Equal Area. The result is a grid with curved lines and slightly variable ...
4
votes
4answers
1k views

Lines on reprojected sp objects with Mollweide projection

I'm having an odd problem such that when I reproject maps to a Mollweide projection in R using sp, I get additional lines stretching across the globe. What is going on here, and do you have any ...
4
votes
2answers
4k views

Add a point on a spplot in R

I have to add a point on a existing spplot in R. I need it only for a visual purpose. I have the coordinates of the point, but I don't know how to change the code in order to add the point. This is ...
4
votes
2answers
9k views

Inserting a zoomed-in plot with a full scale plot in the sp package in R

I am using the sp package in R to plot sampled points along a river. However, I would like to zoom in to a sampled area of the entire river, and plot this zoomed area on the full length of the river. ...
4
votes
1answer
3k views

Breaking out single polygon (feature) of SpatialPolygonDataframe

I am trying to break out a single feature of a Spatialpolygon object. That is e.g. plot a single polygon from a shapefile that contains multiple. Any suggestions how to do this? >#Import shapefile,...
4
votes
2answers
2k views

Calculate distance of points - SpatialPoints object - along a path - SpatialLines object - using R

I have a SpatialPoints object and a SpatialLines object. I want to calculate all the distances from the start point (first coordinate of the SpatialLines object) to every point in the SpatialPoints ...
4
votes
2answers
739 views

How to sort points from a SpatialPoints object over a SpatialLines object according to line direction using R?

I have a very big dataset of points (SpatialPoints object) over a line (SpatialLines object). I need to sort these points following the line direction. Note: the number of points and coordinates ...
4
votes
4answers
283 views

Representation of spatial lines with altitude

Is there any R data structure that supports spatial lines that include height information? I'm looking for something like SpatialLinesDataFrame with support for altitude in the lines. Associating a ...
4
votes
1answer
2k views

Set the minimum and maximum values for an elevation raster in R

I have a raster that displays elevational values at a global scale. I would like to set the minimum and maximum values based on the values I would like to choose. > elev class : RasterLayer ...
4
votes
1answer
299 views

inconsistent results with sp::over in R

I have a dataset with lat/longs mostly in the UK and want to add a column indicating parliamentary constituency. The whole dataset is ~50k rows by 30 columns. A short sample is: lat long Age ...
4
votes
1answer
531 views

Lapply function on raster iterating through SPDF polygons in R

I have a raster DTM and a SPDF. I'd need to mask a different elevation interval of the DTM within each overlaid polygon. I tried a for loop to iterate through the polygons, but as one might expect ...
4
votes
1answer
1k views

Difference between large Rasterlayer and formal class Raster Layer in R?

I currently possess two rasters with the EXACT SAME resolution and extent, and yet they show different number of rows when loaded into the global environment in R. In addition, before these rasters ...
4
votes
2answers
304 views

“Downsample” a dense grouping of points onto a sparse grid

I have a SpatialPointsDataFrame with thousands of datapoints in a relatively small geographic area. The points are grouped around regions of physical measurements and are hence irregularly spaced: ...
4
votes
1answer
1k views

buffer in spsample for distance between randomly selected points

I wanted to know if there is a way to add a buffer in the function spsample in the sp package. I am randomly selecting points within 28 polygons and would like to specific a minimum distance the ...
3
votes
1answer
632 views

Define cellsize from spsample in meters

I use a shapefile to create a regular grid using spsample, from sp package. Two problems here: (1) I would like to define the size of my grid cells in meters, but I've checked the documentation of sp ...
3
votes
2answers
291 views

Is their an equivalent of SpChFIDS for a spatial POINTS data frame?

This code: seattle <- spChFIDs(seattle.shp, paste("seattle.shp", row.names(seattle.shp), sep=".")) Results in this error: Error in (function (...
3
votes
2answers
4k views

How to plot gridded map from lat-lon and fill values in R?

I want to plot a spatial map in which each pixel is of 0.25 degree spatial resolution in R. For this I have fill values at 0.25 degree grid spacing. Below is the data ----------------------- ...
3
votes
1answer
2k views

Extracting data, not colors, from geotiffs in R

I'm an experienced R user new to GIS, but finding my way. I've recently run into an issue that I haven't been able to find any satisfactory documentation on (but could just be searching poorly). I'm ...
3
votes
1answer
173 views

sf appears to truncate a projection string

Curious how folks deal with this type of thing where sf appears to trim the spatial projection: library(sf) library(sp) nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) ...
3
votes
1answer
1k views

Subset and sort a SpatialPolygonsDataFrame object by one variable

I took a sample of a spatial polygon dataframe with 2256 elements using: valdata <- ref_10[sample(ref_10@data$ID, length(ref_10)/3),] now I would like to sort my new spatial dataframe by my ID-...
3
votes
1answer
3k views

Plotting bubbles on top of OpenStreetMap in R

I can plot points on top of OpenStreetMap like this: library(OpenStreetMap) library(rgdal) subscr<-data.frame(lat=c(10.1237,10.2161,10.2993), lon=c(59.7567,59.7527,59.6863), pop=c(58,12,150)) ...
3
votes
1answer
591 views

Understanding resolution when rasterizing in R?

I'm creeping into building surfaces - useful for a number of public health projects - and can't seem to make sense of what's happening with the resolution and raster building process to get what I ...
3
votes
1answer
127 views

Two rows of data.frame for one polygon

I am new in R and I am facing a problem. I have a list with many SpatialPolygonsDataFrames. Some of them are like this example This is a SpatialPolygonsDataFrame. My "problem" is that the data.frame ...
3
votes
1answer
2k views

Find intersections between spatial lines and points using R

This question assumes that I'm using R with the sp and rgdal packages installed. Now that I have a set of spatial segments of class SpatialLinesDataFrame (called 'segmentData') and a set of spatial ...
3
votes
1answer
48 views

How might one optimise geospatial operations on complex polygons, especially plots in R?

Question: I'm attempting to run some simple plots on some shapefiles with a high number of complex polygons. These aren't massive files - for the example below, the shapefile is 70mb (15872 obs), but ...
3
votes
1answer
38 views

determine whether location of fish is wrong based on boat speed and time of last capture

I have a series of coordinates for fish caught from a boat at different datetimes and different trips. How do I determine whether the coordinates of a fish are likely to be incorrect (e.g. due to ...
3
votes
1answer
520 views

Counting how many times a point is inside a set of intersecting polygons in R

I have a set of points (latitude and longitude) and a set of polygons with the same CRS. Since some of the polygons are interescting, it may be that some of the points are in more than one polygon. ...
3
votes
1answer
1k views

constrain random points with a polygon in R using sp library

I have a map of London's waterways, which I've grided into a raster dataset to enable me to measure proximity to water from randomly generated points. What I'd like to do please is to constrain the ...
3
votes
1answer
85 views

gIntersection error: linearRing not created

I am trying to do something relatively simple but with large shapefiles. I have a shapefile of protected areas in the USA: and watershed boundaries: I want to calculate the percentage and ...
3
votes
0answers
717 views

Group and aggregate SpatialPolygonsDataFrame by column in R

I'm working with rgdal, sp, maptools, etc. I have a SpatialPolygonsDataFrame that has each row/Polygons list with only one Polygon. It also has an identifier column which I want to use to group by, ...
3
votes
0answers
130 views

Assigning a new coordinate system and projection to a shapefile in R [duplicate]

I have a shapefile with no spatial reference nor projection, and a GEOTIFF that has the spatial reference and projection data. The shapefile was created from a segmentation analysis of the GEOTIFF, ...