[gdal-dev] Problems doing spatial query with OGR

Frank Warmerdam warmerdam at pobox.com
Wed May 28 11:17:13 EDT 2008


Clay, Bruce wrote:
> Environment: Windows XP Visual Studio 2005, GDAL 1.5.0 with the C#  SWIG 
> bindings.
> 
>  
> 
> I am trying to determine the number of features within a geographic 
> bounding box for a specific time period.  The code I am using is shown below
> 
>  
> 
>     OSGeo.OGR.Feature feature;
> 
>     int numFeatures;
> 
>  
> 
>     string sqlString = "select count(*) from " + tableName + " where " + 
> queryDefString;

Bruce,

I don't think you mention the driver you are operating against.  I'm guessing
it is a real RDBMS?

There is a problem that when we do ExecuteSQL() against a real database
the spatial filter has to be applied *after* the query since we have no way
of inserting it into the actual SQL sent to the database.  That means we
can't alter the answer to the "SELECT COUNT(*)" part.  All we can do is filter
features coming back from that select which in this case will be a single
non-spatial attribute.

In fact, I'm not sure that the spatial filter is even honoured in this fashion
by all drivers, though it ought to be.

If you really want to do this, my suggestion is to set an attribute filter
with your WHERE statement, and a spatial filter on the OGRLayer, and then
do GetFeatureCount().  That is more likely to work.

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org



More information about the gdal-dev mailing list