Strange behavior by design of the spatial function Filter in SQL Server 2008

In SQL Server 2008 there is a spatial function called Filter, documented here:
http://msdn.microsoft.com/en-us/library/cc645883.aspx
This function makes a fast index-based scan for geometry intersection. It guarantees to return all intersecting cases, but might return cases not intersecting as well. So this is a first filtering and as I understand it, it is an internal part of STIntersects. This functionality has nothing to do with the bounding box comparison  PostGIS does as a first filtering before the real ST_intersts calculation. In SQL Server Filter returns a more accurate answer to the intersection question. This discussion explains a lot of how it works:
social.msdn.microsoft.com/Forums/en/sqlspatial/thread/6e1d7af4-ecc2-4d82-b069-f2517c3276c2

The problem with this function is that, as the documentation says:

“In cases where an index is not available, or is not used, the method will return the same values as STIntersects() when called with the same parameters.”

Why is this a problem? Well, this means that the function gives different answers with and without an index from the exactly same query and dataset. This I think is a little problematic in itself for a function not being totally internal. But it might become nearly absurd in some cases. Look at this query:

Select a.id, b.id from table1 a ,  table2 b where a.geom.Filter(b.geom)=1;

If the geometries in table1 and table2 are indexed, we will get a fast answer, which might contain more geometry combinations than those actually intersecting with each other.  That is no problem, that is the whole idea. The problem shows if we want to see the result of the Filter function in the select part of the query like this:

Select.geom.Filter(b.geom) asFiltered,  a.id, b.id from table1 a ,  table2 b where a.geom.Filter(b.geom)=1;

Then the interesting thing happens that since the index won’t be used in the select part (using index makes no sense in select part) the Filter function here will give the same result as STIntersects, just as the documentation says. So, even if we “filter away” all cases where Filter returns anything else than 1 we will get rows where the column “Filtered” returns 0.

Here is a picture of my practical example from SQL Server Management console. As you see I get 144 rows back. I use the exactly same function in the where-part and in the select part but apparently get different answers. Fully logical from how the function is designed, but I don’t like it.

I guess that the reason for this design is that it is difficult or impossible to get the same result without the index

But to me it looks quite ugly.

Good news to windows users, PostGIS 1.5 now in Stack builder

Today PostGIS 1.5.0 reached Stack builder. That means that if you are using windows it’s now very simple to try the new functionality. Thanks Regina and Leo

That means that a fully functional installation with PostGreSQL 8.4.2 and PostGIS 1.5.0 is just a few clicks away.

Download the oneclick-installer of PostGreSQL from
http://www.enterprisedb.com/products/pgdownload.do#windows

Then, in the end of the installation choose yes to start Stack builder and mark PostGIS 1.5 under spatial extensions.

Ready to go 🙂

PostGIS 1.5.0 released

I hardly believed my eyes the other day when I saw the headlines roll. PostGIS 1.5.0 is released and totally without any suffix like beta or rc 🙂

With 1.5.0 the knowledge has reached PostGIS that the planet Tellus is not flat but a globe. You can now calculate the distance from Stockholm, Sweden to Oshakati in northern Namibia without the need to dig a tunnel to get the same answer from practical measuring. If you use the new geographical storage type you will get the distance around the globe from point 1 to point 2. 

But still most of the PostGIS functionality is only applicable to the planar way of looking at the earth, one part at a time.

The full release document can be found here:

http://postgis.org/news/20100204