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

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)

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

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?

Create stage height raster using least cost path and r

I have a point shapefile of Station IDs and stageheights. I would like to create a raster where each cell has the stage height value (in meters) of the closest in situ station to that cell.
I want this raster to match up with another raster. So I would like it if I could input both a raster I have created (dataset 3 described below) and my point shapefile (1).
Datasets:
1) Point Shapefile with stage heights of a river delta
2) Shapefile of the river delta extent
3) Raster of the delta where NA's represent land (could also have them be zero's if need be) and 1's are water. Two datasets 10 meter resolution and 30 meter resolution.
One conceptual issue I am having is with the amount of small streams I have.
For example (pictured in image below), station 1 (circled in blue) is technically closer to the black x region than station 2 (circled in red), but the stage height value in red is more representative of point x. There are NA's in between the two streams, does that mean that the value will not jump across streams?
How can I reassign the values in my Raster (all the 1's) to the stage height of the nearest station and make sure that these values are not jumping from stream to stream? Do I need to use least cost path? What is the best way to do this?
I would like to use R, but can use ArcMap if I must.
So I'm not sure what tools you have available to you but I think this answer may be useful:
Calculating attribute for network distance between multiple points in ArcGIS Desktop?
Here the questioner was looking to calculate distances on roads to some points, but your problem seems similar. I think the main point I would make here is that you should do your network distance classification prior to worrying about the raster layer. You may have to convert from polygon to lines or some workaround to get your data into a format that works, but this is the kind of job the tool is designed to do.
After you have reclassified your river shapefile based on their network distance to a given point, then convert the polygons to raster and use this to classify your original raster. You could do this in R or Arcmap. Arcmap will probably be faster.

Resources