Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using geometry with srid 4326, what unit of measure does STDistance return?

So here is my set up:

I have a table such as:

Id, Lat, Long, GeoPoint, GeomPoint

both GeoPoint (geography) and GeomPoint (geometry) are set to SRID 4326

I have the following query:

    DECLARE @radiiCollection TABLE 
      ( [ID]       INT IDENTITY(1, 1) PRIMARY KEY, 
       [Radius]   GEOMETRY, 
       [RefPoint] GEOMETRY, 
       [RefAddr]  VARCHAR(255), 
       [MinLat]   VARCHAR(255), 
       [MaxLat]   VARCHAR(255), 
       [MinLng]   VARCHAR(255), 
       [MaxLng]   VARCHAR(255)) 

    DECLARE @point GEOMETRY = geometry::Point(-111.84493459999999, 33.3902569, 4326) 
    DECLARE @gpoint GEOGRAPHY = geography::Point(33.3902569, -111.84493459999999, 4326);

    INSERT INTO @radiicollection 
                (radius, 
                 refpoint, 
                 refaddr, 
                 maxlat, 
                 maxlng, 
                 minlat, 
                 minlng) 
    VALUES      ( @point.MakeValid().STBuffer(16093.40), 
                  @point, 
                  '10 miles of 85210', 
                  33.51734689767781, 
                  -111.6923852740045, 
                  33.26298081643247, 
                  -111.99703818130439 ) 

    SELECT 
      GeomPoint, 
      GeoPoint
     INTO #temp 
    FROM ( 
      SELECT row_number() OVER ( 
          PARTITION BY [ds].[ADDR], 
          [ds].[APT], 
          [ds].[ZIP] ORDER BY [ds].[IND_ID] ASC 
          ) recid1, rGeop1.geompoint, rgeop1.GeoPoint 
      FROM [r].[main] ds 
      JOIN [r].[GeoPoint] rGeoP1 
        ON rGeoP1.[UID] = ds.[UID] 
      JOIN @radiiCollection rr 
        ON GeomPoint.STWithin(rr.radius) = 1 
      WHERE 1 = 1 
        AND ( 
          ( 
            ( 
              ( 
                try_cast(latitude AS DECIMAL(9, 1)) BETWEEN CONVERT(DECIMAL(9, 1), 33.26298081643247)
                  AND CONVERT(DECIMAL(9, 1), 33.51734689767781) 
                AND try_cast(longitude AS DECIMAL(9, 1)) BETWEEN CONVERT(DECIMAL(9, 1), - 111.99703818130439)
                  AND CONVERT(DECIMAL(9, 1), - 111.6923852740045) 
                ) 
              ) 
            ) 
          ) 
      ) f 
    WHERE recid1 = 1

So, pulling both, I then pull STDistance for both the GeoPoint and the GeomPoint like so:

select top 10 
 try_cast(GeoPoint.STDistance(@gpoint) as  float) DistanceGeo,
 try_cast(GeoMPoint.STDistance(@point.MakeValid()) as float) DistanceGeom
from #temp

but what I'm getting is a little weird:

    DistanceGeo     DistanceGeom
    -----------------------------------------
    10495.1674191715    0.111544285781801
    10249.4175883919    0.100540150724826
    12307.1907929483    0.1262738924781
    11804.655587608     0.116453906202276
    10249.4175883919    0.100540150724826
    9607.03640753812    0.101614826463312
    11130.8413059431    0.100596791997409
    10249.4175883919    0.100540150724826
    6973.69243171186    0.0644901191669685
    9605.88647121359    0.0967178499759486

From what I understand SRID determines unit of measure for the spatial column, but obviously, since both of these are SRID 4326 that is not the case? Any help would be appreciated. I believe what I'm seeing here is degrees instead of meters. Is there an easy way in SQL to convert degrees to meters? Or a way to change what unit STDistance uses for output?

like image 275
zach Avatar asked Aug 12 '14 17:08

zach


2 Answers

You can get a definitive answer on which unit of measurement STDistance is using with the following query. For 4326 (for example), it's the meter.

SELECT *
FROM sys.spatial_reference_systems
WHERE spatial_reference_id = 4326
like image 156
Morgan Thrapp Avatar answered Sep 19 '22 14:09

Morgan Thrapp


With SqlGeomety, you are dealing with "projected coordinates".

In your case, using SqlGeometry with WGS84 (4326), your units are degrees. So STDistance(), STArea(), ... will return degrees.

You have to use a projected coordinate system that uses meters (like Lambert 93 for france or UTM for USA) to have coordinates in meters and calculations in meters.

like image 43
XavierFischer Avatar answered Sep 20 '22 14:09

XavierFischer