[R]How to extract multiple points based on polygons - r

I have a polygon file and a point file (gpkg file)
Within the area of the polygon, there are several point files.
How can I extract only the points that exist in the area of the polygon?
The language used is assumed to be the R sf package.
The sample data is as follows.
https://drive.google.com/file/d/1vzyStvbSWYiqj7Yu-Te4sPHlZlxscAlE/view?usp=sharing
https://drive.google.com/file/d/1StzjcogZAS3gxy9owxKNZWOZFf1LoI1o/view?usp=sharing

Related

Overlap (or convert) a raster in shapefile with R software

I have an .TIFF file (raster) for Latin America and .shp file (shapefile) for brazilian municipalities and I want to overlap both but I want sum all the informations that a raster have.
In my case, I have a TIFF file is about light pollution. Every point in raster represent one light information that ranging from 0-63. The shapefile have only the geometry.
In the end I want that every information in municipality return the sum (or mean) of all the raster points.
Someone knows that's possible in R software?
Many thanks
I Cannot give an example because TIFF file is too big but I sending an image
I want to overlay a map like this one, but with smaller dimensions
try this:
require(raster)
rs=raster("pathToTiff.tif")
shp=shapefile("pathToShapefile.shp")
##to get the sum of all raster values within in each administrative area:
##assuming that they are in the same CRS, if not in same CRS run this first - shp=spTransform(shp,crs(rs))
extract(rs,shp,fun=sum)

Is it possible to create a SpatialPolygonsDataFrame with three dimensions?

I'm using the plotKML package to write a number of shapefiles to a KML file. The shapefiles only contain coordinates in two dimensions (longitude & latitude). The plotKML package has support for reading a third dimension (elevation or altitude) from the spatial object and writing this to the KML file.
My plan is to iterate through the shapefiles and create new SpatialPolygonsDataFrames with the missing third dimension (the altitude data is contained in the 'data' slot) but the Polygon() function specifically states that the coords argument must be a "2-column numeric matrix with coordinates".
How is it possible to create a Polygon with a three column coords matrix?
This is not supported by sp, by design. Package sf has support for three-dimensional coordinates in polygon nodes (POLYGON Z) but this is, afaict, not (yet) supported by plotKML.

Loading ArcGIS vector and raster layers in R (for ipdw package; Inverse Path Distance Weighting)

I am trying to interpolate landscape influences in the coastal/marine environment by inverse distance weighting while accounting for land barrier, and am excited to find the ipdw package (https://cran.r-project.org/web/packages/ipdw/ipdw.pdf). Within ArcGIS, I currently have 1) a cost raster object (.adf file) that sets the study extent and 2) a point object (.csv file with latitude, longitude, and intended metric for interpolation) - and am in the process of trying to make them compatible with R.
Can someone direct me to resources for converting a .csv file to a shapefile that would work within the ipdw package, to be loaded as the spdf (SpatialPointsDataFrame) object?
Does the ArcGIS raster have to be in a certain format to be loaded as the costras (cost raster) object?
I would really appreciate any leads and insights!
This tutorial covers creating an sp SpatialPointsDataFrame object from a csv:
https://www.neonscience.org/dc-csv-to-shapefile-r
This tutorial covers loading raster files in R and combining with vector objects:
https://www.neonscience.org/dc-crop-extract-raster-data-r

Calculating the percentage of the area that a polygon covers a cell in a raster in QGIS

I'm having trouble with using QGIS.
I have a GRIB file containing meteorlogical data, this is loaded in QGIS as rasterdata if I'm not mistaken.
I also have a shape-file containing a polygon, describing regions within the area covered in the GRIB file.
What I need to know, is how many % of a raster-cell is covered by a region within the polygon. I'm using QGIS.
What I have done so far, is I rasterized the polygon, and used zonal statistics to calculate the SUM and COUNT on the shape-file, and then used the field calculator to calculate the percentage covered. This does not seem to provide the result I hoped for.
Can anyone push me in the right direction?

How to use the function r.cost to get the least-cost path between two polygons?

I am a beginner in GRASS but I would like to get the least-cost path between two polygons. More exactely, I would like to get the smallest cost from any point situated at the edge of one polygon (polygon A) to any point situated at the edge of another polygon (polygon B).
Until now, I used the function CostDistance and CostPath of ArcGIS by using a cost raster where each cell had a cost value, a shapefile for the first polygon, and a shapefile for the second polygon. I would like to do the same thing with GRASS. I think that the function r.cost allows to do this. But I don't know how to specify in parameters the two polygons in GRASS ?
Have you got an example of how to use r.cost with two polygons in R with package spgrass6?
Thanks very much for your help.
If the use of GRASS is not mandatory and sticking with R is sufficient, you should check the marmap package. Section 2.4 of the vignette (vignette("marmap")) is entitled:
2.4 Using bathymetric data for least-cost path analysis
The marmap package allows for computation of least-cost path constrained within a range of depth/altitude between any number of points. The two key functions here are trans.mat() to create a transition matrix similar to the cost-raster you mention. Then, lc.dist() computes the least-cost distance and allows to plot the path between points.
Detailed examples are provided in the marmap vignette.

Resources