Skip to article frontmatterSkip to article content

Why (do we need to talk about) (Spatial) Database

About Data Management

How do you manage your photos?

Sultan Mosque, Singapore.

Figure 1:Sultan Mosque, Singapore. Photo by Charles Postiaux on Unsplash

Managing photos is very similar to the way of managing data, and vice versa.

The photo management.

The photo management.

Data Management

Definition (Oracle)

Database Operations - CRUD

What is Database and Spatial Database

Database: an integrated set of data on a particular subject, which is often used to store, and organize data

Spatial (or Geographic) database: database containing geographic data of a particular subject for a particular area

Spatial Database

Spatial Database

Characteristics of (spatial) database


class: left, middle

Database Management Systems (DBMS)

Basic and key characteristics of DBMS:

DBMS clients

Relational Database

Primary key, Foreign key, Composite key

The keys in Relational Database.

The keys in Relational Database.

Logical relation types

One-to-one

Every entity on the left can connect to one and only one entity on the right.

One-to-one demo.

One-to-one demo.

One-to-many

Every entity on the left can connect to multiple entities on the right. Every entity on the right can connect to one and only one entity on the left.

One-to-many demo.

One-to-many demo.

Many-to-one

Every entity on the left can connect to one and only one entity on the right. Every entity on the right can connect to multiple entities on the left.

Many-to-one demo.

Many-to-one demo.

Many-to-many

Every entity on the left can connect to multiple entities on the right. Every entity on the right can connect to multiple entities on the left.

Many-to-many demo.

Many-to-many demo.

A database for food order & delivery system.

A database for food order & delivery system.

Advantages of Relational Database

Normalization: Preparing a relational database

Demonstration of database normalisation

A demo table, before normalisation.

A demo table, before normalisation.

Step 0: Observation on the unnormalized table

Database Normalization is mainly to separate information from different types of entities to separated tables.

First step is to observe the composition and structure of the database: how many and what are the different ‘entity’ or ‘object’ in the table.

First step is to observe the composition and structure of the database: how many and what are the different ‘entity’ or ‘object’ in the table.

Step 1: Fill Missing Values

Step 1 fills the empty cells, and each cell has one value. But the problem of data redundancy has increased.

The original data may organised in an ordered way such that the repeated values are skipped if it is same as the one before.

The original data may organised in an ordered way such that the repeated values are skipped if it is same as the one before.

Step 2: Table Decomposition

Decopmose the table. Still, there are one table with two types of ‘object’.

Decopmose the table. Still, there are one table with two types of ‘object’.

Step 3: Table Decomposition again

Keep decompose the table again, until every table only represent a single type of object.

Keep decompose the table again, until every table only represent a single type of object.

Done Normalisation

Finalise the table decomposition and the normalisation is complete.

Finalise the table decomposition and the normalisation is complete.

Table join and relate

Since now the tables are separated and represent a single object, how to ‘combine’ them again for other data operation and usage? Table join and relate are the two frequently used database operation in GIS.

Table join

A join operation brings together two tables by using a common field or a primary key and a foreign key.

Table relate

].column[

Comparison of Join and Relate

Join

Relate

The Special of ‘Spatial’ in ‘Spatial Database’

What about ‘spatial’ in spatial database? The location, proximity, and direction in Google Map.

What about ‘spatial’ in spatial database? The location, proximity, and direction in Google Map.

Spatial is indeed special!

Spatial join

A spatial join uses a spatial relationship to join two sets of spatial features and their attribute data.

In Figure 19:

Spatial Join Example

Figure 19:‘Merging’ (join/relate) table by spatial relationships.

Example of spatial database

Spatial Indexing

Operationaly, what is the addition of SPATIAL to the database?

Image source: Wikipedia: R-tree

Image source: Wikipedia: R-tree

Example of spatial query (PostGIS)

Create table

CREATE TABLE public.cities (
    id serial PRIMARY KEY,
    name VARCHAR(255),
    population INT,
    geom GEOMETRY(Point, 4326) -- Point geometry, SRID 4326 (WGS 84 geographic coordinate system)
);

Insert data rows (point)

INSERT INTO public.cities (name, population, geom) VALUES
('London', 8982000, ST_SetSRID(ST_MakePoint(-0.1278, 51.5074), 4326)),
('Paris', 2141000, ST_SetSRID(ST_MakePoint(2.3522, 48.8566), 4326)),
('New York', 8419000, ST_SetSRID(ST_MakePoint(-74.0060, 40.7128), 4326));

Example of spatial query (PostGIS)

Finding cities within a search distance from a point

    SELECT name
    FROM public.cities
    WHERE ST_DWithin(
        geom::geography,
        ST_SetSRID(ST_MakePoint(-0.1278, 51.5074), 4326)::geography,
        100000 -- distance in meters
    );

Spatial Joins

-- Assuming another table 'countries' with a 'geom' column (Polygon)
SELECT c.name AS city_name, co.name AS country_name
FROM public.cities AS c, public.countries AS co
WHERE ST_Contains(co.geom, c.geom);

Do calculation (area)

    SELECT ST_Area(ST_GeomFromText('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))', 4326));