Day 4: The Geometry vs. Geography Debate: Planar speed vs. spherical accuracy.

Lesson 4 60 min

The Geometry vs. Geography Debate: Planar Speed vs. Spherical Accuracy

Welcome back, architects and engineers! Today, we're diving into a foundational decision in geospatial system design that separates the rookies from the veterans: GEOMETRY versus GEOGRAPHY. This isn't just about picking a data type; it's about understanding the fundamental trade-offs that dictate performance, accuracy, and ultimately, the scalability of your entire geospatial infrastructure. When you're dealing with systems processing 100 million requests per second, a subtle choice here can mean the difference between effortless scaling and a multi-million dollar infrastructure nightmare.

Agenda for Today:

Component Architecture

Client App 100M RPS Load SQL Query PostgreSQL 17 PostGIS Spatial Engine GEOMETRY gist_geometry_ops Math: Planar/Cartesian ULTRA FAST GEOGRAPHY gist_geography_ops Math: Geodesic/Spherical HIGH ACCURACY Local Scale Global Scale GiST Indexed Storage (MBR) Precise Results
  • Unpacking GEOMETRY: The planar speed demon.

  • Unpacking GEOGRAPHY: The spherical truth-teller.

  • The core dilemma: When speed beats accuracy, and when accuracy is non-negotiable.

  • Real-world impact: How this choice shapes ultra-high-scale systems.

  • Hands-on comparison: See the difference in action.

Core Concepts: Planar vs. Spherical Worlds

Imagine you're drawing on a piece of paper. That's GEOMETRY. It operates on a flat, Cartesian plane. Distances are calculated using simple Euclidean math (think Pythagoras). It's incredibly fast because the math is straightforward. But here's the catch: the Earth isn't flat. If you try to measure the distance between New York and London on a flat map, you'll get a significantly different (and incorrect) result compared to measuring it on a globe. This distortion becomes more pronounced the larger the distances or areas you're dealing with.

Now, imagine you're measuring distances directly on a globe. That's GEOGRAPHY. It treats the Earth as a spheroid (or ellipsoid, to be precise, like the WGS84 datum which is SRID 4326). Calculations here use spherical trigonometry, accounting for the curvature of the Earth. This is inherently more complex and computationally intensive. Functions like ST_Distance on GEOGRAPHY types will give you distances in meters, accurately reflecting the real-world distance along the Earth's surface.

System Design Concept: The Performance-Accuracy Trade-off

Flowchart

Start Query Analyze Spatial Predicate Global Accuracy Required? YES GEOGRAPHY Type (Spherical Math - Heavy CPU) NO GEOMETRY Type (Planar Math - High Scale) Spatial Refinement (Final Exact Calculation) Return Result

This debate is a classic example of the performance-accuracy trade-off, a cornerstone of system design. In distributed systems, every millisecond, every CPU cycle, counts.

  • GEOMETRY:

  • Pros: Lightning fast operations. Ideal for localized queries (e.g., "find all stores within 5km of this point"), rendering on maps that use projected coordinate systems (like Web Mercator, SRID 3857), and applications where relative positions or small area calculations are paramount.

  • Cons: Inaccurate for large distances or areas, especially across different latitude bands. Results are in the units of the projection (e.g., meters for 3857, degrees for 4326 if treated as geometry).

  • Architecture Fit: Excellent for real-time, low-latency proximity searches in ride-sharing, food delivery, or local recommendations. You'd typically use a projected SRID (like UTM zones for specific regions or 3857 for global web maps) to minimize local distortion.

  • GEOGRAPHY:

  • Pros: Unquestionably accurate for real-world distances and areas on the Earth's surface. Results are always in meters.

  • Cons: Significantly slower computation due to complex spherical math (e.g., Haversine formula, Vincenty's formula for more precision).

  • Architecture Fit: Essential for applications requiring high precision over large or global distances: logistics, flight path planning, global asset tracking, climate modeling, or any scenario where absolute real-world distances are critical, irrespective of scale.

The Production System Dilemma: 100 Million RPS Scale

State Machine

New Spatial Data Request Is Global Accuracy Critical? YES (Global/Long Distance) Use GEOGRAPHY (SRID 4326 / Spherical) NO (Local/Planar Speed) Use GEOMETRY (SRID 3857 / Planar) Query Optimized Result: High Precision Result: 100M RPS Support

When your system handles 100 million requests per second, the choice between GEOMETRY and GEOGRAPHY isn't theoretical; it's a strategic decision with profound implications.

Imagine a global ride-sharing platform:

  • Local Driver Matching (GEOMETRY): When a user requests a ride, you need to find the closest 10 drivers within a small radius (e.g., 5km). These queries are executed millions of times per second. Using GEOMETRY (with a suitable local projection or 3857) allows these queries to be incredibly fast, leveraging highly optimized planar indexing (like GiST). The minor planar distortion over 5km is negligible for practical purposes. If you used GEOGRAPHY here, the increased CPU cycles per query would quickly overwhelm your database servers, necessitating vastly more hardware and introducing unacceptable latency.

  • Long-Distance ETA Calculation (GEOGRAPHY): For a cross-city or inter-state trip, calculating an accurate estimated time of arrival involves precise distance calculations over potentially hundreds or thousands of kilometers. Here, GEOGRAPHY is indispensable. The accuracy is paramount, even if it takes a few more milliseconds. These queries are typically less frequent than local matching, perhaps happening once per ride request, allowing the system to absorb the higher computational cost.

The Key Insight for Veterans: It's not about which one is "better" in absolute terms. It's about cost-benefit analysis for your specific use case. Do you need high accuracy at global scale, or lightning speed at local scale? Often, a sophisticated system will use both. You might store data as GEOGRAPHY for analytical precision but create derived GEOMETRY columns (perhaps in a materialized view or a separate service) for high-frequency, low-latency local queries.

Hands-On: Seeing the Difference

Let's set up a quick experiment to demonstrate the accuracy difference. We'll compare distances between two points: one local (Eiffel Tower to Arc de Triomphe) and one global (Eiffel Tower to Statue of Liberty) using both GEOMETRY (projected to Web Mercator, SRID 3857) and GEOGRAPHY (WGS84, SRID 4326).

Assignment: Compare Distances

Your task is to set up a PostGIS database, insert two sets of points, and then query the distances using both GEOMETRY and GEOGRAPHY types. Observe the differences.

Steps:

  1. Set up PostGIS: Ensure you have a running PostgreSQL instance with the PostGIS extension enabled. Our script will handle this.

  2. Create a table: Define a table landmarks with columns for name, geom_3857 (GEOMETRY, SRID 3857), and geog_4326 (GEOGRAPHY, SRID 4326).

  3. Insert data: Insert the following points:

  • Eiffel Tower, Paris: (Lon: 2.2945, Lat: 48.8584)

  • Arc de Triomphe, Paris: (Lon: 2.2950, Lat: 48.8738)

  • Statue of Liberty, NYC: (Lon: -74.0445, Lat: 40.6892)

  • Remember to convert WGS84 (Lon/Lat) to SRID 3857 for the GEOMETRY column using ST_Transform(ST_SetSRID(ST_MakePoint(longitude, latitude), 4326), 3857).

  1. Query local distance: Calculate the distance between Eiffel Tower and Arc de Triomphe using both ST_Distance(geom_3857) and ST_Distance(geog_4326).

  2. Query global distance: Calculate the distance between Eiffel Tower and Statue of Liberty using both ST_Distance(geom_3857) and ST_Distance(geog_4326).

  3. Analyze results: Compare the distances. Note how GEOMETRY with SRID 3857 provides results in meters but will be less accurate for large distances compared to GEOGRAPHY, which also provides results in meters but with spherical accuracy.

Solution Hints:

  • PostGIS Functions:

  • ST_MakePoint(longitude, latitude): Creates a point geometry.

  • ST_SetSRID(geometry, srid): Assigns a Spatial Reference ID.

  • ST_Transform(geometry, new_srid): Converts geometry from one SRID to another.

  • ST_Distance(geom1, geom2): Calculates distance. For GEOMETRY, it's planar. For GEOGRAPHY, it's spherical.

  • SRIDs:

  • 4326: WGS84 (Latitude/Longitude, default for GEOGRAPHY).

  • 3857: Web Mercator (Common for web maps, planar, units in meters).

  • Data Insertion Example (Conceptual):

sql
INSERT INTO landmarks (name, geom_3857, geog_4326) VALUES
('Eiffel Tower', ST_Transform(ST_SetSRID(ST_MakePoint(2.2945, 48.8584), 4326), 3857), ST_SetSRID(ST_MakePoint(2.2945, 48.8584), 4326)::geography),
-- ... similarly for other points
;
  • Query Example (Conceptual):

sql
SELECT
ST_Distance(et.geom_3857, at.geom_3857) AS local_geom_distance_meters,
ST_Distance(et.geog_4326, at.geog_4326) AS local_geog_distance_meters
FROM landmarks et, landmarks at
WHERE et.name = 'Eiffel Tower' AND at.name = 'Arc de Triomphe';

In the next lesson, we'll delve deeper into spatial indexing strategies, critical for making these queries performant at scale. Until then, experiment with these types and internalize the critical trade-offs.

Need help?