true intersection testing with NTS and Ogr

Topics: Data Access, SharpMap v0.9 / v1.x
Jul 8, 2011 at 6:55 AM

Is there a way to specify the filterdelegate (or similar behaviour) when I am querying via the ogr provider and using nts for intersection testing.

I have tried the following but I am getting data that seems to be wrong as it includes rows that don't intersect (although they do belong to neighbours)


                var propertyRow = propertyResults.Tables.First().Rows[0] as FeatureDataRow;
                var propertyBoundingBox = propertyRow.Geometry;

                var provider = GetLayerProvider(layerName);

                var layerBoundingBox = provider.GetExtents();

                var layerEnvelope = GeometryConverter.ToNTSEnvelope(layerBoundingBox);

                var results = new FeatureDataSet();
                provider.ExecuteIntersectionQuery(propertyBoundingBox, results);

                interestingRows = results.Tables.First().Clone();

               foreach(var row in results.Tables.First().Rows)
                   var featureRow = row as FeatureDataRow;

                   var compareGeometry = GeometryConverter.ToNTSEnvelope(featureRow.Geometry.GetBoundingBox());

                   bool isInteresting = (QueryType)queryType == QueryType.Within ? compareGeometry.Contains(layerEnvelope) : layerEnvelope.Intersects(compareGeometry);


Jul 8, 2011 at 7:59 AM

You're comparing envelopes, try to use

and check for true intersections
Jul 8, 2011 at 9:25 AM

the OgrProvider does true intersection testing against specified geometry. Just use the ExecuteIntersectionQuery function that takes Geometry as argument.

Hth FObermaier

Jul 8, 2011 at 9:27 AM
Is it possible to do a within test using the Ogr provider?
Jul 8, 2011 at 9:53 AM

Try this

var propertyRow = propertyResults.Tables.First().Rows[0] as FeatureDataRow;

var provider = GetLayerProvider(layerName) as OgrProvider;

var results = new FeatureDataSet();
provider.ExecuteIntersectionQuery(propertyRow.Geometry, results);

var interestingRows = results.Tables[1];

Hth FObermaier

Jul 10, 2011 at 4:48 AM

Sorry forgive my ignorance (reasonably new to the GIS stuff)

How does that perform a within query?


What I am trying to achieve is I have a geometry that I have l retrieved from a main "Cadastre" layer

 //Query cadastre to get property bounding box
            var query = String.Format("SELECT * FROM {0} WHERE {1} = {2}", cadastreProvider.LayerName, Properties.Settings.Default.PropertyKeyField, propertyID);

            var propertyResults = cadastreProvider.ExecuteQuery(query);

            if (propertyResults.Tables.First().Rows.Count > 1)
                throw new Exception(string.Format("PropertyID was not unique multiple results returned from property in {0}", Properties.Settings.Default.PropertyLayerName));

            var resultsTable = propertyResults.Tables.First();

Once I have that starting point I need to know if any geometries in another particular layer are within (entirely within not partially) this geometry and if they are I need to return the feature information for the geometry that is within the "main" layer geometry.

The ExecuteIntersectionQuery seems like it would be able to do a pure intersection based query for me but not the within part of am I missing something here? Is there another way to attack this?

Jul 11, 2011 at 7:57 AM

Is it a feeling that because of the function name you are missing sth, or have you actually tested it?

For spatial relations see

Hth FObermaier