Thanks for the tips. Its a good thing to know that long/lat are considered FALSE projection.
However it seem that it wasn't the reason of leaflet not displaying the data.
I use to connect to a PostGis database to get my data, using the custom function dbreadspatial from this website : https://neocarto.hypotheses.org/1186
This function directly connect to the database and import gis data into a spatialdataframe, according to his type (point, line, polygon...) and CRS.
So when I summarize my spdf, I get :
summary(test)
Object of class SpatialPolygonsDataFrame
Coordinates:
min max
x 99404 384674
y 233785 493775
Is projected: TRUE
proj4string :
[+proj=utm +zone=22 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs]
Then I apply the reprojection :
testproj <- spTransform(test, CRS("+proj=longlat +init=epsg:4326 +ellps=WGS84 +datum=WGS84 +no_defs"))
Wich looks good for use
summary(testproj)
Object of class SpatialPolygonsDataFrame
Coordinates:
min max
x -54.600647 -52.038341
y 2.111988 4.463896
Is projected: FALSE
proj4string :
[+proj=longlat +init=epsg:4326 +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0]
But when I try to display it on leaflet, I get an error message saying that I'm not using the long lat projection, even tought I do.
m <- leaflet() %>%
-
addTiles() %>%
-
setView(lng=-52.3333300, lat=4.9333300 , zoom=5) %>%
-
addPolygons(map=m, data=sample_test, weight=2, col="black", opacity=0.5)
Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") :
addPolygons must be called with both lng and lat, or with neither.
So what's wrong with my method? I tried to connect to database with readOGR and applied the same transform and it didn't work either, with same error message.
I tried to reproject with st_transform but It didn't work :
sample_testproj <- st_transform(sample_test, 4326)
Error in UseMethod("st_transform") : no applicable method for 'st_transform' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialPolygonsNULL', 'SpatialVector')"