Fortgeschrittene Geodaten (PostGIS)
Integrieren Sie PostGIS, um komplexe Geodaten zu verarbeiten, standortbasierte Abfragen auszuführen und Kartenfunktionen in Ihrer App zu entwickeln
Fortgeschrittene Geodaten (PostGIS) ist eine kostenlose Supabase Backend as a Service-Lektion auf CoddyKit. Dies ist Lektion 2 von 3. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Supabase Backend as a Service-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Supabase Backend as a Service-Kurs umfasst insgesamt 3 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Unlocking Geospatial Power
Welcome to Advanced Geospatial Data (PostGIS)! In this lesson, we'll explore how to handle location-based data directly within your Supabase database.
Geospatial data is crucial for apps that deal with maps, locations, navigation, or proximity services.
What is PostGIS?
PostGIS is a powerful, open-source extension for PostgreSQL that adds support for geographic objects.
- It transforms your database into a spatial database.
- It allows you to store, query, and analyze location data (points, lines, polygons).
- Supabase, being built on PostgreSQL, fully supports PostGIS.
Why Use PostGIS?
Traditional databases store locations as separate latitude/longitude columns, making complex queries difficult.
PostGIS provides specialized functions to:
- Calculate distances between points.
- Find locations within a specific radius.
- Determine if points are inside an area.
- Perform complex spatial joins and analyses.
Enabling PostGIS Extension
Before using PostGIS, you need to enable the extension in your Supabase project. This is a one-time step done via the SQL Editor in your Supabase dashboard.
Simply run the following command:
CREATE EXTENSION IF NOT EXISTS postgis;Geospatial Data Types
PostGIS introduces new data types to store spatial information:
GEOMETRY: Stores data on a 2D Cartesian plane (flat earth model). Good for small-scale, local areas.GEOGRAPHY: Stores data on a spherical globe (real-world coordinates). Best for large-scale, global applications.
We'll primarily use GEOMETRY for simplicity in examples, but GEOGRAPHY is often preferred for real-world distances.
Creating a Table with Spatial Data
Let's create a table to store points of interest, like coffee shops, using a GEOMETRY column. The SRID (Spatial Reference ID) 4326 is standard for latitude/longitude.
CREATE TABLE coffee_shops (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
location GEOMETRY(Point, 4326)
);Inserting Geospatial Points
To insert a point, we use the ST_GeomFromText() function, which converts a Well-Known Text (WKT) representation into a PostGIS geometry object. The format is 'POINT(longitude latitude)'.
INSERT INTO coffee_shops (name, location) VALUES
('Espresso Hub', ST_GeomFromText('POINT(-0.1278 51.5074)', 4326)),
('Bean Scene', ST_GeomFromText('POINT(-0.1419 51.5085)', 4326)),
('Daily Grind', ST_GeomFromText('POINT(-0.1000 51.5200)', 4326));Finding Nearest Locations
One common task is finding locations near a specific point. We can use ST_Distance() to calculate the distance between two geometry points. The unit of distance depends on the SRID (e.g., meters for 4326 on GEOGRAPHY type, degrees for GEOMETRY type).
Here, we find shops near a reference point (e.g., user's current location).
SELECT
name,
ST_Distance(
location,
ST_GeomFromText('POINT(-0.1300 51.5090)', 4326)
) AS distance_in_degrees
FROM
coffee_shops
ORDER BY
distance_in_degrees
LIMIT 1;Efficient Proximity Search
For more efficient proximity searches, especially when working with GEOGRAPHY types for real-world distances, ST_DWithin() is excellent. It checks if two geometries are within a specified distance.
Let's find all coffee shops within 1000 meters of a specific point using GEOGRAPHY for accurate distance calculation.
SELECT
name
FROM
coffee_shops
WHERE
ST_DWithin(
location::geography,
ST_GeomFromText('POINT(-0.1300 51.5090)', 4326)::geography,
1000 -- 1000 meters
);PostGIS Query Check
You've learned how to enable PostGIS, create tables with spatial columns, and insert/query geospatial data.
Which PostGIS function would you typically use to find all points of interest that are located within 500 meters of a user's current position, assuming you're using the GEOGRAPHY type?
Recap: Geospatial Data
Great job! You've taken your first steps into the world of geospatial data with PostGIS and Supabase.
- We enabled the PostGIS extension.
- Learned about
GEOMETRYandGEOGRAPHYtypes. - Created tables and inserted data using WKT.
- Performed basic spatial queries like distance calculation and proximity searches with
ST_Distance()andST_DWithin().
PostGIS opens up a vast array of possibilities for location-aware applications!
Lerne Supabase Backend as a Service mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 11
- Lektionen
- 40
Häufig gestellte Fragen
Ist die Lektion „Fortgeschrittene Geodaten (PostGIS)“ kostenlos?
Ja — der vollständige Text von „Fortgeschrittene Geodaten (PostGIS)“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Supabase Backend as a Service-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Supabase Backend as a Service-Kurs umfasst insgesamt 3 Lektionen.
Was lerne ich in „Fortgeschrittene Geodaten (PostGIS)“?
Integrieren Sie PostGIS, um komplexe Geodaten zu verarbeiten, standortbasierte Abfragen auszuführen und Kartenfunktionen in Ihrer App zu entwickeln Du übst Supabase Backend as a Service mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Supabase Backend as a Service zu starten?
Keine Vorkenntnisse erforderlich. Supabase Backend as a Service auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 3.
Wie lange dauert die Lektion „Fortgeschrittene Geodaten (PostGIS)“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Supabase Backend as a Service-Lektion Code schreiben und ausführen?
Ja. Jede Supabase Backend as a Service-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Postgres-Erweiterungen verwenden
- Fortgeschrittene Geodaten (PostGIS)
- Volltextsuche und Vektor-Embeddings mit pgvector