[R-sig-Geo] Adding SQL "where" to readOGR

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Jul 3 11:00:21 CEST 2013


This pops up on stackoverflow and r-sig-geo occasionally - how can I
add SQL to my readOGR call?

There was a discussion here about 2.5 years ago, which ended in some
comment from Roger about some refactoring of rgdal before trying to
implement setAttributeFilter. Since then, nothing, and no sign of it
in the current codebase.

Is it as simple as it seems it could be?

1. Add an optional 'where' argument to readOGR.
2. Pass that string to C++ and call setAttributeFilter on the OGRLayer object.
3. Check for errors...
4. Get back a filtered data set.

 spdf = readOGR("PG:dbname=maps","countries",where="population > 1000")

I don't think it needs the full hammer of using OGR's ExecuteSQL
functionality whch looks a bit different. The attribute filter
mechanism is smart enough to pass the SQL to the database for database
drivers, or do the hard work itself for reading from Shapefiles. I
think the implementation of this in gdal is widespread enough now for
it to be accepted (1.8?).

Now, if only I wasn't teaching 100 ecologists next week I might attack
this. Actually I would probably be going to UseR!... But anyway....

Has anyone looked at this in more detail?

Barry



More information about the R-sig-Geo mailing list