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

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.

Related

Plot coastline and calculate distances (marmap and ggmap)

I am working on a research project in marine ecology, using R, and I would like to create a map of a small and precise part of the French Mediterranean coast. From this map I would like to add the different fish collection sites in order to calculate the distances between these sites, taking into account the topology of the coast (the sites being very close to the coast). I have used the marmap package to do this, however due to the size of the map I wish to create, the resolution is very poor and the map is unworkable.
data <- getNOAA.bathy(lon1 =2.97,lon2 =3.53,lat1 =41.9,lat2 =42.3,resolution = 1)
I would like to know if there is an alternative, such as using the ggmap package to get a map with a good resolution, then import the GPS points of the sites and calculate the distances between them using marmap ? Are the two packages compatible?
Do you have any other ideas?
I'd recommend using leaflet for mapping and geosphere to find the Haversine (as the crow flies` distance betwen points.

Randomly sampling an irregular raster extent in R

Is there a function in the R raster package that is analogous to sampleRandom but which extracts n random pixel values from within an irregularly shaped polygon feature rather than a rectangular extent object?
I know there are alternative approaches such as generating random points within a polygon and then use the extract() function to get pixel values, but am wondering if there is a more direct path I have missed.
Thanks
No, there is not a single function for this.

Cluster adjacent Vornoi polygons of same category

We have a set of points, each with (x,y) coordinates and a category C. We have built the Voronoi diagram based on these points and would now like to "cluster" adjacent polygons when they are of a particular category. Is there a ready-made algorithm / R package for doing this ?
If not, our current thinking is to go back to the Delaunay triangulation and brute-force our way to the solution : consider each vertix V, find the vertix v of each edge going into V and see if they are the same category, if so aggregate the polygons.
Is there a better way to do that ? Is there an R package that could do that ? If not, which R package implementing Delaunay would have the best result data-structure to do this ?
Note: I wouldn't call this cluster analysis. If you stick to this keyword, you will not find anything useful to you. What you apparently want to do is merge adjacent Voronoi cells, but that's about it.
Your Voronoi cell / Delaunay triangulation algorithm should give you information of all the edges. What you probably want to do is iterate over all edges, and when both cells have the same category, merge them.
Trivial code; and heavily application dependant (what is "same category"?), so you probably won't find a "libary" for it.
You can use a convex hull on the points and remove all points inside a same category. Then repeat the voronoi diagram. BTW. I know nothing about R.

Using point coordinates and diameter to calculate areal projection of points

I have data on a number of ecological variables associated with spatial points. Each point has x & y coordinates relative to the bounding box, however the points represent circular areas of varying diameter. What I'm trying to achieve is to project the area occupied by each point onto the observation window so that we can subsequently pixellate the area and retrieve the extent of overlap of the area of each point with each pixel (grid cell). In the past I have been able to achieve this with transect data by converting to a psp line object and then using the pixellate function in the spatstat package but am unsure how to proceed with these circular areas. It feels like I should be using polygon classes but again I am unsure how to define them. Any suggestion would be greatly appreciated.
In the spatstat package, the function discs will take locations (x,y) and radii r (or diameters, areas etc) and generate either polygonal or pixel-mask representations of the circles, and return them either as separate objects or as a single combined object.

R: Determining whether a point lies inside a region made up of separate polygons generated from contourLines()

I am using the function contourLines() in R to record the vertices of a contour based on a probability density estimation. Then I test to see whether a point lies inside the contour region. I can do this test easily when there is only one region (polygon) created from contourLines, but sometimes the there are multiple polygons created. I am trying to come up with a way to determine whether a point lies inside the multiple polygon contour.
My idea so far is to calculate the number of polygons generated and treat each one separately. I was thinking I could use graph theory to determine the number of polygons generated because there will not be a path between points on 2 separate polygons.
Probably there is an easier way. Any suggestions?
Thanks in advance,
HS

Resources