Spatial Data

A spatial database stores and queries data that has location and shape: maps, roads, buildings, rivers, cell towers. Data types:

  • Point (a hospital), line or polyline (a road) and polygon (a district), with regions made of polygons.
  • Raster data (a grid of pixels, as in satellite images) and vector data (points, lines and polygons with coordinates).

Spatial objects also have non-spatial attributes (name, population).

Spatial Operations and Queries

  • Measurements: distance, area, length, perimeter.
  • Topological relations: disjoint, touches, overlaps, contains, within, equals and crosses.
  • Range (window) query: all objects in a given area.
  • Nearest-neighbour query: the closest object to a point (the nearest hospital).
  • Spatial join: pairs of objects from two sets that satisfy a spatial relation (rivers that cross districts).

SQL support: SQL/MM, PostGIS, Oracle Spatial; functions such as ST_Distance, ST_Within and ST_Area.

Spatial Indexing

Because there is no natural one-dimensional order of space, special indexes are used:

  • Grid file: the space is divided into cells.
  • Quadtree: a square is recursively split into four quadrants.
  • R-tree: objects are grouped under minimum bounding rectangles (MBRs) in a balanced tree; a search descends only into rectangles that overlap the query.

Queries use a filter-and-refine approach: first the MBRs filter candidates, then the exact geometry is tested.

Geographic Information Systems (GIS)

A GIS captures, stores, analyses and displays geographic data. Data is arranged in layers (roads, rivers, land use) that can be overlaid. Functions: overlay analysis, buffering (all places within 2 km of a road), network analysis (shortest route) and map display. Applications: urban planning, navigation, disaster management, agriculture and utilities.

Definition: Minimum bounding rectangle (MBR). The smallest axis-parallel rectangle that completely encloses a spatial object, used as a cheap approximation in spatial indexes.

Table 4.3: Spatial index structures

Index Idea Note
Grid file Fixed cells Simple, uneven for clustered data
Quadtree Recursive four-way split Adapts to density
R-tree Nested MBRs Widely used

Example 4.3: Distance, area and containment

Problem: A land plot is the triangle with corners (0,0), (4,0) and (0,3). Find its area and perimeter, and test whether the points (1,1) and (3,2) lie inside it. Also find the distance between (0,0) and (3,4).

Solution:

  1. Area by the shoelace formula = |0x0 + 4x3 + 0x0 - (0x4 + 0x0 + 3x0)| / 2 = 12 / 2 = 6.
  2. The sides are 4, 3 and 5, so the perimeter = 12.
  3. A point (x, y) is inside if x / 4 + y / 3 < 1: for (1,1) this is 0.583, inside; for (3,2) it is 1.417, outside.
  4. Distance between (0,0) and (3,4) = √(9 + 16) = 5.

Result: Area 6, perimeter 12, the point (1,1) is inside and (3,2) is outside; the distance is 5.

from math import dist
poly = [(0, 0), (4, 0), (0, 3)]
n = len(poly)
area = abs(sum(poly[i][0] * poly[(i + 1) % n][1]
               - poly[(i + 1) % n][0] * poly[i][1]
               for i in range(n))) / 2
perimeter = sum(dist(poly[i], poly[(i + 1) % n]) for i in range(n))
print("Area:", area, " Perimeter:", perimeter)
def inside(p):
    x, y = p
    hit = False
    for i in range(n):
        (x1, y1), (x2, y2) = poly[i], poly[(i + 1) % n]
        if (y1 > y) != (y2 > y):
            cross = (x2 - x1) * (y - y1) / (y2 - y1) + x1
            if x < cross:
                hit = not hit
    return hit
print("(1,1) inside:", inside((1, 1)))
print("(3,2) inside:", inside((3, 2)))
print("Distance:", dist((0, 0), (3, 4)))

Output

Area: 6.0  Perimeter: 12.0
(1,1) inside: True
(3,2) inside: False
Distance: 5.0

Common mistakes

  • Treating latitude and longitude as flat x and y coordinates when measuring long distances.
  • Using a B-tree on x and y separately for two-dimensional range queries.
  • Skipping the refinement step, so that overlapping MBRs give false answers.
  • Overlaying layers that use different coordinate systems.

Key takeaways

  • Spatial data types: point, line, polygon; raster and vector.
  • Queries: range, nearest neighbour and spatial join.
  • R-tree uses MBRs; queries filter first and refine afterwards.
  • GIS organises data in layers for overlay and network analysis.

Practice questions

  1. What is a nearest-neighbour query?
  2. What is stored in an R-tree?
  3. What is a layer in a GIS?

Answers to the practice questions

  1. A query for the object closest to a given location.
  2. Objects grouped under nested minimum bounding rectangles.
  3. A set of data of one kind, such as roads, that can be overlaid on other layers.

Sources

  • Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit IV