Radius Latitude Longitude

  • I have a table with latitude longitude in the format.  45.123456 , -111.123456 , Primary Key .   I want to find all ids within a radius in miles.

    Thanks in advance . Have a great day.

     

     

  • You use the (longitude, latitude) to create/define a @Point. and then you use STDistance to calculate the distance between them.

    STDistance (geography Data Type) - SQL Server | Microsoft Learn

  • Thanks that was a good exercise but that's for two points . I was hoping some type of calculation that draws a radius and returns all the points that fall within that radius or circle.  If you have any links for radius please share.

  • HeftSteady wrote:

    Thanks that was a good exercise but that's for two points . I was hoping some type of calculation that draws a radius and returns all the points that fall within that radius or circle.  If you have any links for radius please share.

    All points inside a circle of radius x? That's an infinite number.


  • Here is a picture . Let me know if you need it in another language.

    Attachments:
    You must be logged in to view attached files.
  • x

    • This reply was modified 1 months, 1 weeks ago by HeftSteady.
  • If you cannot provide a concise answer . Please move onto the next topic . Have a great day and sql on!

  • Sorry, wrong function. you want STContains.

    Remarks

    STContains() always returns null if the spatial reference IDs (SRIDs) of the geometry instances do not match.

    Examples

    The following example uses STContains() to test two geometry instances to see if the first instance contains the second instance.

    DECLARE @g geometry;

    DECLARE @h geometry;

    SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);

    SET @h = geometry::STGeomFromText('POINT(1 1)', 0);

    SELECT @g.STContains(@h);

  • hi hope this helps

    CREATE TABLE locations (

    id INT PRIMARY KEY IDENTITY(1,1),

    latitude FLOAT NOT NULL,

    longitude FLOAT NOT NULL

    );

    INSERT INTO locations (latitude, longitude)

    VALUES

    (45.123456, -111.123456), -- Center point

    (45.200000, -111.200000), -- Nearby

    (45.500000, -111.500000), -- A bit farther

    (46.000000, -112.000000), -- Farther away

    (44.900000, -111.050000); -- Close south

     

    Declare your center point and radius

    DECLARE @lat FLOAT = 45.123456;DECLARE @lon FLOAT = -111.123456;

    DECLARE @radius_miles FLOAT = 10;

    -- Create a GEOGRAPHY point for the center

    DECLARE @center GEOGRAPHY = GEOGRAPHY::Point(@lat, @lon, 4326);

    -- Query locations within the radius

    SELECT id,

    latitude,

    longitude,

    GEOGRAPHY::Point(latitude, longitude, 4326).STDistance(@center) / 1609.34 AS distance_miles

    FROM locations

    WHERE GEOGRAPHY::Point(latitude, longitude, 4326).STDistance(@center) <= @radius_miles * 1609.34

    ORDER BY distance_miles;

     

     

    • This reply was modified 4 days, 15 hours ago by naumon765.
    • This reply was modified 4 days, 15 hours ago by naumon765.
    Attachments:
    You must be logged in to view attached files.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply