[R-sig-Geo] how to filter a shapefile ?

Roger Bivand Roger.Bivand at nhh.no
Thu Sep 16 14:10:55 CEST 2010


On Thu, 16 Sep 2010, Mayeul KAUFFMANN wrote:

> You can read your shapefile with:
> library(rgdal)
> countries_spdf <- readOGR(dsn=root_path, "countries_file_without_extension")
> #You'll have a spatial point data frame with marks. Look at str(countries_spdf).
> Try:
> plot(countries_spdf[countries_spdf at data$AREA>1000])

Correct, subset with the "[" method as with data.frame objects. But avoid 
using the data slot directly, use the "$" method as with data.frame 
objects:

plot(countries_spdf[countries_spdf$AREA>1000,])

and insert a comma in the "[" method to choose the rows matching the 
condition:

library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
scot_BNG <- readOGR(dsn=dsn, layer="scot_BNG")
plot(scot_BNG, border="grey")
plot(scot_BNG[scot_BNG$SMR > 100,], border="black", lwd=2, add=TRUE)

Hope this helps,

Roger


> Mayeul
> _____________________________________________________
> Dr. Mayeul KAUFFMANN, Conflict Specialist
> European Commission, Joint Research Centre (JRC)
> Institute for the Protection and Security of the Citizen (IPSC)
> Global Security and Crisis Management - ISFEREA
> Via E. Fermi 2749 - I-21027 Ispra (VA), ITALY
> Phone: (+39) 033278 5071
> http://isferea.jrc.ec.europa.eu/Staff/Pages/Kauffmann-Mayeul.aspx
>
> (Office: building 48c, 1st floor, room 123. TP: 483)
>
> _____________________________________________________
> Dr. Mayeul KAUFFMANN, Conflict Specialist
> European Commission, Joint Research Centre (JRC)
> Institute for the Protection and Security of the Citizen (IPSC)
> Global Security and Crisis Management - ISFEREA
> Via E. Fermi 2749 - I-21027 Ispra (VA), ITALY
> Phone: (+39) 033278 5071
> http://isferea.jrc.ec.europa.eu/Staff/Pages/Kauffmann-Mayeul.aspx
>
> (Office: building 48c, 1st floor, room 123. TP: 483)
>
>
> -----Original Message-----
> From: r-sig-geo-bounces at stat.math.ethz.ch
> [mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of Arnald Marcer
> Sent: Thursday, September 16, 2010 12:58 PM
> To: r-sig-geo
> Subject: [R-sig-Geo] how to filter a shapefile ?
>
> Hi,
>
> Can anyone give me a hint on how to extract/filter/subset
> polygons from a shapefile based on an attribute of its dbf
> table ?
>
> Example:
>
> shapefile: world.shp
> attributes of its dbf table: ID, AREA, COUNTRY, ...
>
> How can I plot a shapefile with only countries above a certain AREA value ?
>
> Thank you very much,
>
> Arnald Marcer
> CREAF
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list