How the ST_RELATE() function works in Mariadb?

The MariaDB ST_RELATE() function is a powerful spatial function that provides detailed insights into how two geometries interact, using the Dimensionally Extended 9-Intersection Model (DE-9IM).

Posted on

The Spatial Relationship Decoder

Imagine you’re analyzing city maps and need to know if a proposed park sits completely within a school district, touches a residential zone, or overlaps with protected wetlands. This is where MariaDB’s ST_RELATE() function becomes your spatial detective. It doesn’t just tell you if two geometries interact - it reveals exactly how they interact using the powerful Dimensionally Extended 9-Intersection Model (DE-9IM).

Unlike simpler spatial functions that return true/false answers, ST_RELATE() provides a complete relationship matrix between two geometries. This matrix becomes invaluable when you need precise details about how features connect, overlap, or separate in your spatial data.

Understanding the Relationship Matrix

At its core, ST_RELATE() generates a 3x3 matrix that describes all possible intersections between two geometries’ interiors, boundaries, and exteriors. The result comes as a 9-character string where each character represents a specific type of intersection:

  • ‘T’ (True): Intersection exists
  • ‘F’ (False): No intersection
  • ‘0’, ‘1’, ‘2’: Dimension of intersection
  • ‘*’ (Don’t care): Wildcard for pattern matching

Here’s a basic example comparing two simple geometries:

SET @square1 = ST_GeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))');
SET @square2 = ST_GeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))');

SELECT ST_RELATE(@square1, @square2) AS relationship_matrix;

This might return ‘212101212’, indicating how these squares overlap at their corners.

Practical Relationship Patterns

While the full matrix provides complete information, you’ll often check for specific relationship patterns. MariaDB supports pattern matching with these common predicates:

-- Check if geometry1 contains geometry2
SELECT ST_RELATE(@geom1, @geom2, 'T*****FF*') AS contains_relation;

-- Check if geometries touch at boundaries
SELECT ST_RELATE(@geom1, @geom2, 'FT*******') AS touches_relation;

-- Check for overlapping areas
SELECT ST_RELATE(@geom1, @geom2, 'T*T***T**') AS overlaps_relation;

These patterns let you ask precise questions about spatial relationships without interpreting the full matrix yourself.

Real-World Use Cases

Zoning Compliance Checks

Municipal planners can verify property compliance:

SELECT p.property_id
FROM properties p
JOIN zoning z ON ST_RELATE(p.geometry, z.geometry, 'T*****FF*')
WHERE z.zone_type = 'RESIDENTIAL';

This finds properties completely within residential zones (contains relationship).

Transportation Network Analysis

Transport engineers can analyze road connections:

SELECT a.road_id, b.road_id
FROM roads a, roads b
WHERE a.road_id < b.road_id
AND ST_RELATE(a.geometry, b.geometry, 'F***T****');

This identifies roads that touch at endpoints but don’t overlap.

Environmental Impact Studies

Ecologists can assess protected area interactions:

SELECT d.development_id
FROM proposed_developments d
JOIN protected_areas p ON ST_RELATE(d.geometry, p.geometry, 'T********')
WHERE p.protection_level = 'HIGH';

Finds developments overlapping with highly protected areas.

Performance Considerations

While powerful, ST_RELATE() can be computationally intensive. Optimize your queries by:

  1. Using spatial indexes:

    ALTER TABLE cities ADD SPATIAL INDEX(geometry);
    
  2. Filtering first with simpler functions:

    SELECT * FROM parcels
    WHERE ST_Intersects(geometry, @search_area)
    AND ST_RELATE(geometry, @search_area, 'T*****FF*');
    
  3. Caching frequent relationship checks

Common Mistakes to Avoid

  1. Misinterpreting the matrix: Remember the order is (Interior, Boundary, Exterior) for both geometries
  2. Overlooking SRIDs: Ensure both geometries use the same coordinate system
  3. Confusing contains/within: The matrix is directional - A relating to B differs from B relating to A
  4. Ignoring validity: Invalid geometries may produce misleading results
-- Always check geometry validity first
SELECT ST_RELATE(@geom1, @geom2)
WHERE ST_IsValid(@geom1) AND ST_IsValid(@geom2);

The Precision Tool for Spatial Analysis

ST_RELATE() stands apart in MariaDB’s spatial toolkit by providing detailed insights into how geometries interact. Its key strengths include:

  • Comprehensive relationship analysis beyond simple true/false tests
  • Pattern matching for common spatial predicates
  • Consistency with standards (DE-9IM model)
  • Integration with spatial indexes for optimized queries

Whether you’re developing GIS applications, performing geographic research, or managing spatial data infrastructure, understanding ST_RELATE() will give you unprecedented control over analyzing spatial relationships. The function’s ability to reveal not just whether but exactly how spatial features relate makes it indispensable for professional spatial analysis.

Remember that while simpler functions like ST_Intersects() or ST_Contains() might suffice for basic checks, ST_RELATE() becomes essential when you need the full story about your spatial data’s relationships. Its detailed output provides the foundation for sophisticated spatial analysis and decision-making systems.