Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting sfc_POLYGON in leaflet

Tags:

r

leaflet

sf

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)?

like image 451
Dhiraj Avatar asked Aug 16 '17 09:08

Dhiraj


1 Answers

"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")
like image 138
Sébastien Rochette Avatar answered Oct 12 '22 20:10

Sébastien Rochette