Spatial Queries and Relationships

Understand bounding boxes, Within, Contains, Intersects, Touches, and Overlaps to perform common extent-based queries.

🎯 Introduction

By the end of this article, you will be able to distinguish attribute queries from spatial queries and understand how to determine which objects lie within an area.

🔍 Conventional Queries and Spatial Queries

A conventional query asks “Does the name equal A?” or “Is the value greater than 10?” A spatial query asks “Where is the object?”, “Do these objects touch?”, or “Is the object within this area?”

A bounding box (BBox) is a rectangle expressed as [minimum longitude, minimum latitude, maximum longitude, maximum latitude]. It is fast to evaluate and useful for selecting potentially relevant objects first.

A BBox quickly narrows the candidate set—for example, to objects near the viewport after the map moves. It does not provide a final point-in-polygon result.

🧭 Common Spatial Relationships

RelationshipMeaning
WithinA is within B
ContainsA contains B; the inverse of Within
IntersectsThe objects touch or overlap
TouchesThe objects meet only at their boundaries
OverlapsTwo objects of the same dimension partially overlap, with neither fully containing the other

These relationships translate application questions into spatial conditions: Within finds stations inside an area, Intersects finds facilities intersecting a road, and Touches finds polygons that meet only at their boundaries.

Point-in-polygon tests usually start with a coarse bounding-box filter and then check the actual Polygon boundary. Being inside a polygon’s bounding rectangle does not mean a point is inside the polygon.

⚖️ Boundary Cases

When a point lies on a Polygon boundary, explicitly define whether it counts as within. Some functions exclude boundaries; others include them in the covered area. If adjacent polygons share a boundary and both count its points, statistics may double-count them.

🧾 Summary

  • Spatial queries filter objects by their spatial relationships.
  • A BBox is a fast preliminary filter, not a final geometry test.
  • Within and Contains reverse the roles of the two objects.
  • Boundary assignment affects statistics and must be defined in advance.

❓ Knowledge Check

Q1: Does a BBox match prove a point is inside a polygon?

No. A BBox is only a rectangular candidate extent. The polygon may be concave or contain holes.

Q2: Does Intersects include objects that only touch at a boundary?

Usually, but check the spatial library’s definition before using it.

🧪 Exercise

For “find bus stops within a park,” specify the preliminary filter, precise geometry test, and boundary rule.

// Write your code here

🎉 You now understand the most common GIS spatial relationship queries!