riatelab / cartography

Thematic Cartography for R
http://riatelab.github.io/cartography/docs/
397 stars 46 forks source link

colored pencil maps with r by using #73

Closed cdjemiel closed 4 years ago

cdjemiel commented 4 years ago

Hi,

First of all, congratulations on this package which will be an essential tool for me in the coming weeks. I am new to geostatistics but I am more and more interested in it as part of my postdoc.

I want to do a map using the getPencilLayer() function from a formal class spatialpolygonsdataframe object. However I have the following error message

Error in UseMethod ("st_crs <-"):
   no applicable method for 'st_crs <-' applied to an object of class "c ('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialPolygonsNULL', 'SpatialVector')"

Can you tell me if it's possible to use an object like this for with this function and can you suggest me some tips ?

Here is my code:

library(cartography)
library(sf)
library(raster) ##Load the Raster Library
france_metro = getData('GADM', country='FRA', level=2)
france_metro_sub = subset(france_metro, NAME_2=="Allier" | NAME_2=="Haute-Loire")
france_metro_sub_pencil = getPencilLayer(france_metro_sub)
dieghernan commented 4 years ago

You have to transform it to an sf object, currently is an sp https://r-spatial.github.io/sf/reference/st_as_sf.html

rCarto commented 4 years ago

Thank you,

In fact you have to use an sf object as input.

library(cartography)
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.4.0, PROJ 5.2.0
library(raster) ##Load the Raster Library
#> Le chargement a nécessité le package : sp
france_metro = getData('GADM', country='FRA', level=2)
france_metro_sub = subset(france_metro, NAME_2=="Allier" | NAME_2=="Haute-Loire")
# from sp object to sf object
reg <- st_as_sf(france_metro_sub)
# project object from WGS84 to Lambert 93
# to ease the fine tuning of getPencilLayer() if needed 
reg_p <- st_transform(reg, 2154) 
reg_pencil = getPencilLayer(reg_p, size = 1000)
plot(st_geometry(reg_p))
plot(st_geometry(reg_pencil), add = T)

Created on 2020-04-28 by the reprex package (v0.3.0)

cdjemiel commented 4 years ago

Thank you very much for your quick reply.

It's perfect !