[gdal-dev] SetAttributeFilter and OSM

agerrius arno at agerrius.nl
Sun Jan 5 00:29:13 PST 2014


Even,

Thanks for the quick answer.

I did some more testing. When using ogr2ogr it works fine indeed. When I use
the ExecuteSQL it also works fine. And it seems to only fail the second time
I call SetAttributeFilter.

Here is some test code I use:

using System;
using System.Collections.Generic;
using System.Text;
using OSGeo.OGR;
using OSGeo.GDAL;
using ASToFra.Utils;

namespace OGR_test
{
    class Program
    {
        static void Main(string[] args)
        {
            Ogr.RegisterAll();

            DataSource ds =
Ogr.Open(@"c:\flightsim\work\agn\schier\schier.osm", 0);

            {
                Console.WriteLine("all point");
                Layer lay = ds.GetLayerByName("points");
                lay.ResetReading();
                Feature feat = lay.GetNextFeature();
                int count = 0;
                while (feat != null)
                {
                    count++;
                    feat = lay.GetNextFeature();
                }
                Console.WriteLine(count);
            }

            {
                Console.WriteLine("all line");
                Layer lay = ds.GetLayerByName("lines");
                lay.ResetReading();
                Feature feat = lay.GetNextFeature();
                int count = 0;
                while (feat != null)
                {
                    count++;
                    feat = lay.GetNextFeature();
                }
                Console.WriteLine(count);
            }

            {
                Console.WriteLine("sql point");
                Layer lay = ds.ExecuteSQL("SELECT * FROM points WHERE
highway IS NOT NULL", null, null);
                lay.ResetReading();
                Feature feat = lay.GetNextFeature();
                int count = 0;
                while (feat != null)
                {
                    count++;
                    feat = lay.GetNextFeature();
                }
                Console.WriteLine(count);
                ds.ReleaseResultSet(lay);
            }

            {
                Console.WriteLine("sql line");
                Layer lay = ds.ExecuteSQL("SELECT * FROM lines WHERE highway
IS NOT NULL", null, null);
                lay.ResetReading();
                Feature feat = lay.GetNextFeature();
                int count = 0;
                while (feat != null)
                {
                    count++;
                    feat = lay.GetNextFeature();
                }
                Console.WriteLine(count);
                ds.ReleaseResultSet(lay);
            }

            Console.WriteLine("loop");
            for (int n = 0; n < 4; n++)
            {
                for (int i = 0; i < 2; i++)
                {
                    Layer lay = ds.GetLayerByIndex(i);
                    lay.SetAttributeFilter("highway IS NOT NULL");
                    lay.ResetReading();
                    Feature feat = lay.GetNextFeature();
                    int count = 0;
                    while (feat != null)
                    {
                        count++;
                        feat = lay.GetNextFeature();
                    }
                    Console.WriteLine(count);
                }
            }
        }
    }
}

When I run the code this is the output I get:

all point
137
all line
341
sql point
41
sql line
321
loop
41
341
41
321
41
321
41
321

As you can see the second time I set an attribute filter in the loop it
returns all 341 features on the layer, not only the 321 that match the
filter. In all other cases it seems to work fine.

Did I go something wrong with in my code? Do I need to call some other
commands maybe before I apply the SetAttributeFilter?

Arno



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/SetAttributeFilter-and-OSM-tp5096352p5096413.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list