1

I have a table (locations) that has a field called Point (geometry). I wrote a query that passes the top and bottom latitude coordinates and the bottom and top longitude coordinates. I want to retrieve all records that are within the area of the coordinates I pass the stored procedure. When I run this it returns zero records even though I know there is a record that matches the criteria. Any ideas what I might be doing wrong?

    DECLARE @categoryid AS int,@leftlong AS float,@rightlong AS float,@toplat AS                         float,@bottomlat AS float
    DECLARE @searcharea  geometry, @polygon AS varchar(500);
    SET @leftlong = -85.605469
    SET @toplat = 42.303468
    SET @rightlong = -85.594912
    SET @bottomlat = 42.297564
    SET @polygon =  CAST(@leftlong AS varchar(20)) + ' ' + CAST(@toplat AS varchar(20)) + ',' + 
            CAST(@leftlong AS varchar(20)) + ' ' + cast(@bottomlat AS varchar(20)) + ',' + 
            cast(@rightlong AS varchar(20)) + ' ' + cast(@bottomlat AS varchar(20)) + ',' + 
            cast(@rightlong AS varchar(20)) + ' ' + cast(@toplat AS varchar(20)) + ',' + 
            CAST(@leftlong AS varchar(20)) + ' ' + CAST(@toplat AS varchar(20))

    SET @searcharea = geometry::STGeomFromText('POLYGON ((' + @polygon + '))', 0);

    SELECT *
    FROM locations l
    WHERE l.point.STWithin(@searcharea) = 1

1 Answer 1

0

There are two likely potential sources of the problem:

  1. Usage of geometry instead of geography type. When working with latitude and longitude data, you generally want to use the geography type in order to do calculations on the ellipsoid instead of on the plane. These calculations can different considerably - this is a lot of information available on this distinction in various articles such as this whitepaper.

  2. Mismatching SRIDs - is the srid of the data in your locations table also 0 for all rows? If these do not match between your data and your @searcharea, no results will be returned.

1
  • I figured out my problem. I had my lat/long mixed up when storing my point. Duh. Thanks for the white paper link regardless. That was very informative.
    – Paul Riker
    Jul 12, 2011 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.