Overlaying SpatialLines by SpatialPolygons in R - r

I have a SpatialLines object representing roads and a SpatialPolygons object
containing cities.
I'd like to know how to overlay a SpatialLines object by a SpatialPolygons
object in R.
I'd like to know the Lines that passes over the two cities and which cities in?
Is it possible?

You need the rgeos package which wraps the GEOS library of geometry operations.
Then probably gIntersection or gIntersects will do what you want.

In general, overlays of spatial objects are handled by the over function from the sp package. In the Methods section of the documentation of over (?over) there is a list of methods for over, which does not include the combination of Lines and Polygons. Luckily, the documentation of over says that by installing the rgeos package these kind of methods are made available.

Related

How can I create a SpatialPolygons object from SpatialPolygonsDataFrame object in R?

I am working on creating a species distribution model using R-INLA, and I want to use a shapefile(.shp) that I have created in ArcPro as a barrier in the spatial mesh. In order to do so, I think the shapefile must be in the 'SpatialPolygons' format.
When I read in the shapefile (which is made up of 6 polygons), using st_read(), it is imported as a SpatialPolygonsDataFrame (see attached pictures).
Additionally, i tried to get shapefiles using worldhire, thinned them and tried to remove all the extra wee islands, but the mesh wouldn't run on INLA.
Long story short, is there:
a) A way I can import the .shp file as a SpatialPolygons object (would it need to be a separate shapefile for each polygon, or a different function)?
b) A way I can convert the SpatialPolygonsDataFrame object to a SpatialPolygons object?
c) Use a SpatialPolygonsDataFrame object when constructing a spatial autocorrelation mesh in R-INLA?
I appreciate any advice you can provide, please let me know if I can provide any more information
Link to image of shapefile plotted using plot() function
Link to image of the SpatialPolygonsDataFrame
I'm not sure this will help regarding your underlying problem, but to create a Spatial* object from a Spatial*DataFrame, you use the geometry() function:
library(sp)
library(rgdal)
seas = readOGR("il_seas.shp")
class(seas)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
> seas_geom = geometry(seas)
> class(seas_geom)
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
That simply strips off all attribute columns from the SPDF.

Easy way to convert sfc_POINT into RasterLayer?

I am using the library sf for spatial data in R. I want to use the SpaDES::splitRaster() method to split up a grid made with sf::st_make_grid() into many tiles. As far as I know, neither the sf or the raster library support such an operation.
However, st_make_grid() returns an object of sfc_POINT, but splitRaster() requires an object of type RasterLayer (for example).
Is there a method in sf (or raster, but I'd prefer to stay within sf) that I can use to quickly convert sfc_POINT into a RasterLayer, ideally without loading rgdal and rgeos?

r gis: find borders between polygons

Having a polygon shapefile, I need to produce a polyline shapefile containing only the common borders between polygons (see the picture).
My question is similar to 1 and 2, only I need to do this in R. The latter similar question refers to a solution with the use of Shapely package for python. The analogue of Shapely for R is rgeos. Though, I couldn't find the solution with rgeos on my own.
Note: the shapefile with borders used for illustration was produced in ArcGIS using the solution from similar question 1. Now I need to do the same in R.
What you want is the lines that are the difference between the set of lines from the dissolved regions and the lines of the regions themselves. IN the rgeos package, gUnaryUnion will dissolve the polygons, and gDifference will subtract.
For my small EU subset eusub, I can do this with:
library(rgeos); library(sp)
borders = gDifference(
as(eusub,"SpatialLines"),
as(gUnaryUnion(eusub),"SpatialLines"),
byid=TRUE)
Note the need to convert polygons to lines because the output will be lines.
Then see this:
plot(eusub)
plot(borders, col="red",lwd=2,add=TRUE)

How to convert from shape into polygon in R? There was shape2poly(shapefiles) but this function have been removed

How to convert from shape into polygon in R? There was shape2poly(shapefiles) but this function have been removed, are shapefiles, maptools, spdep still packages for handling maps in R?
I tend to use the OGR stuff, as it lets me work with data from a range of sources (geodatabases, kml, etc).
library(rgdal)
mylayer <- readOGR(dsn="/path/to/folder/containing/shapefile",
layer="shapefilename-minus-dot-shp")

Autokriging spatial data

I'm trying to use a kriging function to create vertical maps of chemical parameters in an ocean transect, and I'm having a hard time getting started.
My data look like this:
horiz=rep(1:5, 5)
depth=runif(25)
value = horiz+runif(25)/5
df <- data.frame(horiz, depth, value)
The autoKrige function in the automap package looks like it should do the job for me but it takes an object of class SpatialPointsDataFrame. As far as I can tell, the function spTransform in package rgdal creates SpatialPointsDataFrame objects, but there are two problems:
OSX binaries of this aren't available from CRAN, and my copy of RStudio running on OXS 10.7 doesn't seem to be able to install it, and
This function seems to work on lat/long data and correct distance values for the curvature of the Earth. Since I'm dealing with a vertical plane (and short distances, scale of hundreds of meters) I don't want to correct my distances.
There's an excellent discussion of kriging in R here, but due to the issues listed above I don't quite understand how to apply it to my specific problem.
I want a matrix or dataframe describing a grid of points with interpolated values for my chemical parameters, which I can then plot (ideally using ggplot2). I suspect that the solution to my problem is considerably easier than I'm making it out to be.
So there a a few question you want answered:
The spTransform function does not create SPDF's, but transforms between projections. To create a SPDF you can use a simple data.frame as a start. To transform df to a SPDF:
coordinates(df) = c("horiz", "depth")
OS X binaries of rgdal can be found at http://www.kyngchaos.com. But I doubt if you need rgdal.
spTransform can operate on latlong data, but also on projected data. But I do not think you need rgdal, or spTransform, see also point 1.
After you create the SPDF using point 1, you can use the info at the post you mentioned to go on.

Resources