Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter parts of a SpatialPolygonDataFrame?

Tags:

r

rgdal

r-sp

I would like if there is a simple solution to filter a SpatialPolygonDataFrame in R. Imagine I have many polygons but I only want to select some of them to plot them using leaflet

My data comes from data.gouv.fr

Here is my gist for loading the data into R.

If I want to draw only one polygon in leaflet, I filter the @data part of my SpatialPolygonDataFrame, get the id

> ign_shape_iris@data %>% 
+   filter(DCOMIRIS == "606120301")
 DEPCOM NOM_COM IRIS  DCOMIRIS               NOM_IRIS TYP_IRIS  id 
1  60612  Senlis 0301 606120301 Vald'Aunette-Gateliere        H 790

Then I plot only the polygon I want :

leaflet(ign_shape_iris@polygons[[790]]) %>% 
 addTiles() %>%
 addPolygons()

I'm sure there is a better solution.

like image 722
PAC Avatar asked Oct 31 '25 19:10

PAC


1 Answers

Try treating it as a usual data.frame, and think subset instead of filter:

ign_shape_iris[ign_shape_iris$DCOMIRIS == "606120301",]

or

subset(ign_shape_iris, DCOMIRIS == "606120301")
like image 150
Edzer Pebesma Avatar answered Nov 03 '25 10:11

Edzer Pebesma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!