Plotting sfc_POLYGON in leaflet - r

I have a sfc_POLYGON, IHS obtained as a union of a few shapes using the sf package.
Geometry set for 1 feature
geometry type: POLYGON
dimension: XY
bbox: xmin: 270194.7 ymin: 2255850 xmax: 279225 ymax: 2265469
epsg (SRID): 32643
proj4string: +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs
POLYGON((279224.998943279 2261084.1514869, 2792...
While I am able to plot this in mapview, I am facing some problems while plotting the same in leaflet. When I plot this is leaflet, I get a warning sf layer is not long-lat data which it isn't, and I get the base world map. This is the code I used for plotting in leaflet.
IHS%>%leaflet()%>%addTiles()%>%
addProviderTiles(providers$Esri.WorldImagery, group ="ESRI")%>%
addPolygons()
I suspect this has to do something with the CRS. I have tried st_set_crs(IHS, 4326)which changes my projection of IHS from utm to longlat:
Geometry set for 1 feature
geometry type: POLYGON
dimension: XY
bbox: xmin: 270194.7 ymin: 2255850 xmax: 279225 ymax: 2265469
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
However there is no change in the leaflet warning and output. I am not sure here, but has it got something to do with changing the dimension and bbox (which appears to be in meters)?

"Converting" into Lon/Lat is to change its spatial projection. Once you know that, you can play with spatial data and find in library help the correct way of projecting spatial features.
In your case, using st_set_crs(IHS, 4326) only says that the projection is Lon/lat, without projecting it.
You need to use st_transform:
IHS_wgs84 <- st_transform(IHS, "+init=epsg:4326")

Related

Spatial join longitude and latitude points to multipolygon shapefile

I have a spatial data frame of the longitude and latitude of wildfire origins that I am trying to perform a spatial join on to a US Census TIGER/Line shapefile (places) to see if/where there is spatial intersection of fire origins and places.
I am converting the longitude and latitude to coordinate geometry using st_as_sf then attempting to st_join this to the places file, but am encountering an error as the CRS are different. The shapefile is in NAD83 projection, so I am attempting to match that.
library(tidyverse)
library(sf)
> head(fires)
# Longitude Latitude FireName
#1 -106.46667 34.66000 TRIGO
#2 -81.92972 35.87111 SUNRISE
#3 -103.76944 37.52694 BRIDGER
#4 -122.97556 39.37500 BACK
#5 -121.15611 39.62778 FREY
#6 -106.38306 34.77056 BIG SPRING
#convert df to sf
fires_sf <- st_as_sf(fires, coords = c("Longitude", "Latitude"), crs = 4269, agr = "constant")
head(fires_sf$geometry)
#Geometry set for 6 features
#Geometry type: POINT
#Dimension: XY
#Bounding box: xmin: -122.9756 ymin: 34.66 xmax: -81.92972 ymax: 39.62778
#Geodetic CRS: NAD83
#POINT (-106.4667 34.66)
#POINT (-81.92972 35.87111)
#POINT (-103.7694 37.52694)
#POINT (-122.9756 39.375)
#POINT (-121.1561 39.62778)
head(places$geometry)
#Geometry set for 6 features
#Geometry type: MULTIPOLYGON
#Dimension: XY
#Bounding box: xmin: -1746916 ymin: -395761.6 xmax: -1655669 ymax: -212934.8
#Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
#First 5 geometries:
#MULTIPOLYGON (((-1657066 -233757.7, -1657192 -2...
#MULTIPOLYGON (((-1668181 -273428.5, -1669420 -2...
#MULTIPOLYGON (((-1735046 -389578.2, -1735146 -3...
#MULTIPOLYGON (((-1732841 -376703.9, -1732642 -3...
#MULTIPOLYGON (((-1693749 -377716, -1693286 -377..
joined <- st_join(places, fires_sf)
Error in st_geos_binop("intersects", x, y, sparse = sparse, prepared = prepared, :
st_crs(x) == st_crs(y) is not TRUE
To work around this, I have tried st_transform to change the projection to longitude and latitude coordinates, as the places shapefile may be using UTM coordinates, and the datum to NAD83 in both spatial frames. I am getting an error for this as well.
#transform CRS projections
places_transform <- st_transform(places, "+proj=longlat +datum=NAD83")
fires_sf_transform <- st_transform(fires_sf, "+proj=longlat +datum=NAD83")
joined_new <- st_join(places_transform, fires_sf_transform)
Error in s2_geography_from_wkb(x, oriented = oriented, check = check) :
Evaluation error: Found 1045 features with invalid spherical geometry.
[3] Loop 0 is not valid: Edge 280 has duplicate vertex with edge 306
I have attempted to convert the geometry from longitude and latitude coordinates in the fires dataset to UTM coordinates to match the places shapefile, but this was also unsuccessful.
Any advice on how I can properly perform the spatial join of these points and multipolygons would be greatly appreciated.

R Spatial: Mismatch of latitude / longitude between 2 shapefiles with same CRS

my goal is to count the frequency of extreme weather events within subnational African regions. To do so, I have set up a shapefile containing African provinces, using mostly GADM data and the new geocoded EMDAT GDIS dataset for point-data on weather events.
This is how the region shapefile looks like:
library(sf)
st_geometry(africa_map)
Geometry set for 796 features
Geometry type: GEOMETRY
Dimension: XY
Bounding box: xmin: -25.36042 ymin: -46.96575 xmax: 63.49391 ymax: 37.3452
Geodetic CRS: WGS 84
First 5 geometries:
MULTIPOLYGON (((-4.821226 24.99475, -4.821355 2...
MULTIPOLYGON (((1.853562 35.8605, 1.8424 35.865...
MULTIPOLYGON (((-1.361976 35.3199, -1.358957 35...
MULTIPOLYGON (((2.984874 36.81497, 3.014171 36....
MULTIPOLYGON (((7.262677 37.076, 7.266449 37.07..
And the GDIS dataset after converting longitude and latitude to WGS 84:
gdis_africa_sf <- st_as_sf(x = gdis_africa,
coords = c("longitude", "latitude"),
crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
st_geometry(gdis_africa_sf)
Geometry set for 5171 features
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -34.04233 ymin: -25.19619 xmax: 37.08849 ymax: 63.4228
CRS: +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
First 5 geometries:
POINT (-17.09348 15.66576)
POINT (-16.53153 15.77399)
POINT (-16.20006 15.84419)
POINT (-17.09348 15.66576)
POINT (-16.53153 15.77399)
By now, you can already tell that something's off because the bounding boxes do not correspond at all, even though the projections seem to fit.
st_crs(africa_map)==st_crs(gdis_africa_sf)
[1] TRUE
When plotting the two next to each other, the issue becomes clearer, no matter if I use the new shapefile or just apply longitude and latitude of the data frame.
ggplot() +
geom_sf(data = africa_map) +
geom_sf(data = gdis_africa_sf)
Plot 1
ggplot(data = africa_map) +
geom_sf() +
geom_point(data = gdis_africa, aes(x = longitude, y = latitude),
color = "red",
alpha = 0.3,
size = 2,
shape = 1)
Plot 2
It seems like the weather event coordinates are shifted some thousand kilometers to the North West - but what's the source? And how can I fix the issue and make my two geographical data compatible? Any hints would be much appreciated.
It seems like you've mis-diagnosed the problem, it's not a translation, but a reflection in a diagonal line - you've swapped latitude and longitude in your station points. Try:
gdis_africa_sf <- st_as_sf(x = gdis_africa,
coords = c("latitude", "longitude"),
crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

R tmap: polygons plotted in tmap_mode("view") but with ("plot") only the first observation

I am trying to plot a polygon with tmap r package.
My object comes from a shapefile, and looks like:
geometry type: POLYGON
dimension: XY
bbox: xmin: 453850.6 ymin: 7168979 xmax: 513813.6 ymax: 7234603
CRS: +proj=utm +zone=36 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs
First 10 features:
NeighCode NameNeigh Locality AdminPost Populacao Shape_Leng Shape_Area geometry
1 3914 Bassane Chichongue Calanga 114 41926.454 111037471 POLYGON ((504325.3 7212444,...
2 3913 Chinguwine Chichongue Calanga 181 16966.423 14526253 POLYGON ((499974.9 7212379,...
3 3912 Sihanine Chichongue Calanga 238 13508.601 9502638 POLYGON ((500542.6 7208832,...
4 3911 Mpalene Chichongue Calanga 352 6961.326 2646152 POLYGON ((499063.9 7208412,...
5 3910 Chichongue Chichongue Calanga 342 12096.768 6473003 POLYGON ((498551.8 7206144,...
When I am trying to plot it with plot(), I get this:
But, when I am doing it with tmap with the following code:
dss_map <- tm_shape(dss_utm) +
tm_polygons("AdminPost") +
tm_layout(frame = F)
dss_map
I get the following map with tmap_mode("plot").
Only the first observation is plotted. With tmap_mode("view") all of them are plotted.
Anyone could tell me what is happening? I could plot this object in tmap_mode("plot") before without any problems. Here is a link (tmap only plotting first polygon in sf geometry column) of a similar question that wasn't answered.
Thanks for your help!

How to manipulate the geometry of a shapefile polygon using sf in R

I have two shapefile polygons, one with a defined CRS (shapefile A) and another with an undefined CRS (shapefile B). I have lat/long coordinates with geometries similar to that of shapefile A. However, I want to count these points in the polygons of shapefile B. I realize that simply setting the CRS in shapefile B to that of shapefile A does not solve the problem as the geometries are different.
How do I go about modifying the geometry of shapefile B so that it matches with shapefile A? The closest help I found was to apply an affine transformation on the polygons, but I'm not sure how to go about doing so, if that is the correct method.
Here are some descriptions of the two shapefiles (note the bbox and CRS fields in particular):
Shapefile A
Simple feature collection with 61 features and 29 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -61.93033 ymin: 10.04404 xmax: -60.90916 ymax: 10.84451
geographic CRS: WGS 84
Shapefile B
Simple feature collection with 534 features and 13 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: 617063.2 ymin: 1110279 xmax: 728431.5 ymax: 1199347
CRS: NA
I imagine that I would need to multiply the geometries of shapefile B by -10^-4 for the x coordinates and 10^-5 for the y coordinates but I'm not sure if the negative will reflect the shapefile as well. If so, is it still possible to apply another viable transformation?

Obtain CRS from prj4string

I have a shape file, and i want to merge it with another data frame. But for this, i need to know the crs. But the shape file doesnt have the crs, just this description:
Simple feature collection with 96 features and 9 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: 313434.8 ymin: 7343789 xmax: 360663.2 ymax: 7416202
epsg (SRID): NA
proj4string: +proj=utm +zone=23 +south +ellps=aust_SA +units=m +no_defs
I tried to find the crs from the proj4string, but didnt find. Any one know how can i obtain this?
Typically, if you install the rgdal package, you can look up the name of the coordinate reference system with the proj4string in the shapefile metadata you provided:
library(rgdal)
showEPSG('+proj=utm +zone=23 +south +ellps=aust_SA +units=m +no_defs')
But this returns nothing useful in this case:
"OGRERR_UNSUPPORTED_SRS"
From the proj4 string, you have a UTM projection in southern zone 23. Googling the proj4string led me here:https://wiki.osgeo.org/wiki/Brazilian_Coordinate_Reference_Systems
Also, here is more help explaining the use of coordinate reference systems in R: https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/OverviewCoordinateReferenceSystems.pdf

Resources