Is it possible to create a SpatialPolygonsDataFrame with three dimensions? - r

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.

Related

How can I edit the values of coordinates within a geojson file?

I am trying to map a geojson file (A map of Alaska precincts, downloaded from the division of elections and then converted online to geojson) onto a choropleth map using folium, the problem is the coordinates are in 7-digit numbers like this:
[ -16624764.227, 8465801.1497 ]
I read on a similar post that this was most likely a US coordinate system like UTM or State Plane, and recommended using an API to reproject it. Is it also possible to access the coordinates directly such as with geopandas and divide them by 100000?
The data is most likely in a specific cartographic projection. You don't just want to divide by 100k - the data will likely have nonlinear transformations which have a different effect on the position depending on the location. See the GeoPandas docs on working with projections.
If the CRS of the data is correctly encoded, you can re-project the dataframe into lat/lons (e.g. WGS84, which has the EPSG Code 4326) using geopandas.GeoDataFrame.to_crs, e.g.:
df_latlon = df.to_crs("epsg:4326")

[R]How to extract multiple points based on polygons

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

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

Create buffer around spatial data in R

I have a spatial dataset of shopping centers that I would like to create buffers around in R.
I think these packages will be useful:
require(maptools)
require(geosphere)
I was able to do so for a set of coordinates, but not for spatial data. The code looks like this:
coordinates(locs) <- c("Longitude", "Latitude") # set spatial coordinates
fivekm <- cbind(coordinates(locs), X=rowSums(distm (coordinates(locs)[,1:2], fun = distHaversine) / 1000 <= 5)) # number of points within 5 km
But I don't know what function/package to use for a set of polygons. Can someone please advise on the function (or code) and I will go from there?
Thanks!
In library rgeos, there is the gBuffer function that works with SpatialPoints or SpatialPolygons.
The width parameter allows to set the distance to which you want to buffer. However, be careful, this distance is in the scale of the coordinates system used. Thus, in degrees and not in meters with non-projected data. As suggested by #Ege Rubak, you will have to project your data with spTransform first (be sure to use the appropriate CRS according to your location).
As for now, rgeos library works with library sp, but not (yet?) with the recent sf.
I think the only option at the moment is to project your longitude and latitude points to a flat map and then do everything there. As far as I know there are no packages for doing polygonal geometry on the sphere yet (I'm working on one, but there is no ETA).
Projection used to be done with spTransform from the sp package, but now it may be more convenient to use the more modern simple features package sf which has the function st_transform. The vignette https://cran.r-project.org/web/packages/sf/vignettes/sf1.html has a section called "Coordinate reference systems and transformations" to help you with this part. The buffering is described in the section "Geometrical operations".
The two previous post have covered the details but I thought it might be helpful to provide a workflow. This is assuming you have you are using points of lat and long. What is your original spatial data format?
Convert your coordinates into a Spatial Points Dataframe SpatialPointsDataFrame and assign it a geographic CRS (proj4) that matches your coordinate data (probably WGS84)
Change the projection to a local projected CRS with preferred units
Apply buffer to spatial point data frame, the width will now be in more usable units

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