0Pricing
Supabase Backend as a Service · درس

البيانات الجغرافية المكانية المتقدمة (PostGIS)

ادمج PostGIS للتعامل مع البيانات الجغرافية المكانية المعقدة، وتنفيذ استعلامات تعتمد على الموقع، وإنشاء ميزات الخرائط داخل تطبيقك

البيانات الجغرافية المكانية المتقدمة (PostGIS) درس مجاني في Supabase Backend as a Service على CoddyKit. هذا هو الدرس 2 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Supabase Backend as a Service، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Supabase Backend as a Service 3 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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 GEOMETRY and GEOGRAPHY types.
  • Created tables and inserted data using WKT.
  • Performed basic spatial queries like distance calculation and proximity searches with ST_Distance() and ST_DWithin().

PostGIS opens up a vast array of possibilities for location-aware applications!

الأسئلة الشائعة

هل درس «البيانات الجغرافية المكانية المتقدمة (PostGIS)» مجاني؟

نعم — نص درس «البيانات الجغرافية المكانية المتقدمة (PostGIS)» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Supabase Backend as a Service، انتقل إلى CoddyKit PRO. تتضمن دورة Supabase Backend as a Service 3 دروس في المجموع.

ماذا ستتعلم في «البيانات الجغرافية المكانية المتقدمة (PostGIS)»؟

ادمج PostGIS للتعامل مع البيانات الجغرافية المكانية المعقدة، وتنفيذ استعلامات تعتمد على الموقع، وإنشاء ميزات الخرائط داخل تطبيقك تتمرن على Supabase Backend as a Service مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Supabase Backend as a Service؟

لا تُشترط خبرة سابقة. Supabase Backend as a Service على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 3.

كم من الوقت يستغرق درس «البيانات الجغرافية المكانية المتقدمة (PostGIS)»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Supabase Backend as a Service هذا؟

نعم. كل درس في Supabase Backend as a Service يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استخدام إضافات Postgres
  2. البيانات الجغرافية المكانية المتقدمة (PostGIS)
  3. البحث في النص الكامل والتضمينات المتجهية باستخدام pgvector
← العودة إلى Supabase Backend as a Service