Skip to content

Instantly share code, notes, and snippets.

@rdeguzman
Last active February 5, 2024 14:27
Show Gist options
  • Save rdeguzman/99e7fce88458aca678f52bf1a876d36a to your computer and use it in GitHub Desktop.
Save rdeguzman/99e7fce88458aca678f52bf1a876d36a to your computer and use it in GitHub Desktop.
postgis buffer a lonlat point with radius in meters
TLDR;
----------------------------------------------------
st_transform(
st_buffer(
st_transform(st_geomFromText('POINT(145.228914 -37.92674)', 4326), 900913),
50 --radius in meters
),
4326
)
----------------------------------------------------
// we transform the point from SRID 4326 to GOOGLE UTM mercator
// this means its a grid not a sphere
// we then buffer the point with 50 meters
// the resulting geometry is a point in UTM buffered in 50 meters
SELECT st_buffer(
st_transform(
st_geomFromText('POINT(145.228914 -37.92674)', 4326),
900913
),
50
)
//we can verify this by envelope, notice the coordinates in meters
SELECT
st_astext(
st_envelope(
st_buffer(
st_transform(st_geomFromText('POINT(145.228914 -37.92674)', 4326), 900913),
50
)
)
)
st_astext
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
POLYGON((16166758.7549401 -4569131.79492395,16166758.7549401 -4569031.79492395,16166858.7549401 -4569031.79492395,16166858.7549401 -4569131.79492395,16166758.7549401 -4569131.79492395))
// transform it back to 4326
SELECT
st_transform(
st_buffer(
st_transform(st_geomFromText('POINT(145.228914 -37.92674)', 4326), 900913),
50
),
4326
)
// now we get the coordinates in 4326
SELECT
st_astext(
st_transform(
st_buffer(
st_transform(st_geomFromText('POINT(145.228914 -37.92674)', 4326), 900913),
50
),
4326
)
)
st_astext
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
POLYGON((145.229363157642 -37.92674,145.229354527204 -37.9268091193645,145.229328967552 -37.9268755824493,145.22928746093 -37.9269368351258,145.229231602415 -37.9269905235039,145.229163538616 -37.9270345843878,145.229085885188 -37.9270673245611,145.229001626309 -37.927087485853,145.228914 -37.9270942934869,145.228826373691 -37.927087485853,145.228742114812 -37.9270673245611,145.228664461384 -37.9270345843878,145.228596397585 -37.9269905235039,145.228540539069 -37.9269368351258,145.228499032448 -37.9268755824493,145.228473472796 -37.9268091193645,145.228464842358 -37.92674,145.228473472796 -37.9266708805706,145.228499032448 -37.9266044173007,145.228540539069 -37.9265431643473,145.228596397585 -37.9264894756426,145.228664461384 -37.926445414432,145.228742114812 -37.9264126739818,145.228826373691 -37.9263925125049,145.228914 -37.926385704806,145.229001626309 -37.9263925125049,145.229085885188 -37.9264126739818,145.229163538616 -37.926445414432,145.229231602415 -37.9264894756426,145.22928746093 -37.9265431643473,145.229328967552 -37.9266044173007,145.229354527204 -37.9266708805706,145.229363157642 -37.92674))
(1 row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment