Skip to content
  • Home
  • About
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact Us
Geoscience.blogYour Compass for Earth's Wonders & Outdoor Adventures
  • Home
  • About
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact Us
Posted on December 28, 2022 (Updated on July 21, 2025)

Postgis – wrong ST_Distance with geography points

Hiking & Activities

PostGIS: That Pesky ST_Distance and Those Tricky Geography Points

PostGIS, bless its heart, is a spatial powerhouse for PostgreSQL. It lets you do all sorts of cool things with geographic data. One of its star players is ST_Distance, which, as you might guess, calculates the distance between two points. But here’s the thing: when you’re dealing with geography types, things can get a little…wonky. You might scratch your head and wonder, “Why is this distance so off?” Let’s dive into why that happens and how to fix it.

See, the geography type in PostGIS is smart. It knows the Earth isn’t flat! It uses latitude and longitude on the WGS 84 spheroid to represent locations. This is great because it accounts for the Earth’s curve, especially when you’re measuring distances across continents. The ST_Distance function, when used with geography, figures out the shortest distance over that curved surface.

So, what’s the catch? Where do things go sideways? Well, the biggest gotcha is the units. ST_Distance, with geography types, spits out the answer in meters. Yep, meters! I’ve been burned by this more than once. You’re used to geometry types where the units depend on your spatial reference system (SRID), and suddenly you’re getting meters. If you’re expecting kilometers or miles, you’ll need to do a little converting. It’s like accidentally ordering a coffee in milliliters when you wanted a pint!

Another sneaky culprit is coordinate precision. PostGIS can handle super-precise coordinates, but even tiny differences can add up, especially over vast distances. Think of it like aiming a laser pointer at the moon – a minuscule change at your end makes a huge difference up there. So, make sure your coordinates are as accurate as possible. Garbage in, garbage out, as they say!

And speaking of accuracy, keep an eye on your SRIDs. Geography types are WGS 84 by default, but if you mix them with geometry types that have different SRIDs, you’re asking for trouble. PostGIS might try to convert things behind the scenes, but it’s always safer to convert everything to the same SRID yourself before calculating distances. Trust me, it’ll save you a headache. Or, you can cast your geometries to geography types – that works too!

Finally, remember that ST_Distance gives you the shortest distance along the spheroid. For some super-specific scenarios, you might actually want the great-circle distance – the shortest distance on a perfect sphere. The Earth isn’t a perfect sphere, but it’s close enough that the difference is usually negligible, especially for shorter hops. If you absolutely need that great-circle distance, PostGIS has functions like ST_DistanceSpheroid or ST_DistanceSphere where you can specify the shape you want to use.

So, the bottom line? ST_Distance with geography points is powerful, but it’s not foolproof. By remembering that it returns meters, double-checking your coordinate precision, keeping your SRIDs straight, and picking the right distance calculation, you can avoid those head-scratching moments and get the accurate results you need. Happy mapping!

You may also like

Field Gear Repair: Your Ultimate Guide to Fixing Tears On The Go

Outdoor Knife Sharpening: Your Ultimate Guide to a Razor-Sharp Edge

Don’t Get Lost: How to Care for Your Compass & Test its Accuracy

Disclaimer

Our goal is to help you find the best products. When you click on a link to Amazon and make a purchase, we may earn a small commission at no extra cost to you. This helps support our work and allows us to continue creating honest, in-depth reviews. Thank you for your support!

Categories

  • Climate & Climate Zones
  • Data & Analysis
  • Earth Science
  • Energy & Resources
  • Facts
  • General Knowledge & Education
  • Geology & Landform
  • Hiking & Activities
  • Historical Aspects
  • Human Impact
  • Modeling & Prediction
  • Natural Environments
  • Outdoor Gear
  • Polar & Ice Regions
  • Regional Specifics
  • Review
  • Safety & Hazards
  • Software & Programming
  • Space & Navigation
  • Storage
  • Water Bodies
  • Weather & Forecasts
  • Wildlife & Biology

New Posts

  • Mauna Kea: The Real King of the Mountains (and You’ve Probably Never Heard Of It)
  • GHZWACKJ Water Shoes: Dolphin-Cute Comfort for Your Aquatic Adventures!
  • Osprey Daylite Jr.: The Perfect Pack for Little Explorers (and Their Parents!)
  • The Himalayas: Earth’s Newest Mountains, Still Growing!
  • Calvin Klein Lillian Backpack: Is This Signature Style Worth the Hype?
  • CNAFGBGG Tent Storage Bag: Is This Matcha Green Goodness Worth It?
  • South America’s Rivers: More Than Just Water Flowing
  • Funko Pop Fashion Backpack Mini Backpack – Review 2025
  • Oboz Bangtail Mid: My Feet Officially Laugh in the Face of Winter!
  • Bears in Mount Shasta: Staying Safe in Their Backyard
  • FIELDCRAFT Expeditionary Alpha Pack: A Hunter’s (and Hiker’s) New Best Friend?
  • Jack Wolfskin Outdoors Walking Phantom – Buying Guide
  • Mount Shasta: How Old Is This California Giant, Really?
  • Nike ZoomX Zegama: Conquering the Trails with Confidence (and a Little Sass)

Categories

  • Home
  • About
  • Privacy Policy
  • Disclaimer
  • Terms and Conditions
  • Contact Us
  • English
  • Deutsch
  • Français

Copyright (с) geoscience.blog 2025

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT