Lädt...

🔧 PostgreSQL Extensions: Using PostGIS for Geospatial and Time-Series Data


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The data landscape is vast and multifaceted, with different kinds of data requiring different handling techniques. For geospatial data, PostGIS extends PostgreSQL with powerful capabilities for handling location data. When combined with Timescale's time-series functionality, you get a database superpower: the ability to track "where" and "when" together.

Let's explore how to set up and use these tools together to build powerful location-aware, time-series applications.

🗺️ PostGIS + Timescale = Location & Time Analytics

First, connect to your Timescale instance and enable PostGIS:

CREATE EXTENSION postgis IF NOT EXISTS;

💡 Tip: In Timescale, navigate to Operations > Extensions from your service overview for installation instructions.

Creating a Geospatial Time-Series Table

Now, let's create create a practical example table that tracks delivery vehicles and their GPS coordinates over time.

CREATE TABLE vehicle_location (
    time TIMESTAMPTZ NOT NULL,
    vehicle_id INT NOT NULL,
    location GEOGRAPHY(POINT, 4326)
);

Here, location is a GEOGRAPHY type column that stores GPS coordinates (using the 4326/WGS84 coordinate system) while time records the time the GPS coordinate was logged for a specific vehicle_id.

To create a time-series hypertable from this regular PostgreSQL table and insert some dummy data, use:

SELECT create_hypertable('vehicle_location', 'time');
CREATE INDEX ON vehicle_location(vehicle_id, time DESC);

INSERT INTO vehicle_location VALUES 
  ('2023-05-29 20:00:00', 1, 'POINT(15.3672 -87.7231)'),
  ('2023-05-30 20:00:00', 1, 'POINT(15.3652 -80.7331)'),
  ('2023-05-31 20:00:00', 1, 'POINT(15.2672 -85.7431)');

Powerful Queries You Can Run

1. Find vehicle locations by time range
To fetch all locations of a specific vehicle (let's say, vehicle 1) during a specific period, you can query Timescale like this:

SELECT * 
FROM vehicle_location 
WHERE vehicle_id = 1 AND time BETWEEN '2023-05-30 00:00:00' AND '2023-05-31 23:59:59';

2. Get last known locations of all vehicles
To efficiently fetch the last known location of all vehicles using the Timescale SkipScan feature, we can write a slightly more complex query:

SELECT DISTINCT ON (vehicle_id) vehicle_id, ST_AsText(location) AS location 
FROM vehicle_location 
WHERE time > now() - INTERVAL '24 hours' 
ORDER BY vehicle_id, 
time DESC;

The ST_AsText() function converts binary geospatial data into readable coordinates.

3. Find vehicles near a specific location
To fetch all vehicles that were within 1 kilometer of a specific point (15.2 -85.743) at any time, we can use the following SQL:

SELECT DISTINCT vehicle_id 
FROM vehicle_location 
WHERE ST_DWithin(
          location, 
          ST_GeogFromText('POINT(15.2 -85.743)'), 
          1000
       );

In this query, ST_DWithin(location, ST_GeogFromText('POINT(15.2 -85.743)'), 1000) checks whether location is within 1,000 meters of the point (15.2 -85.743).

The Strategic Advantage

When we combine PostGIS and Timescale, PostgreSQL becomes a powerful platform for analyzing both where and when events occur. This opens possibilities for:

  • Fleet tracking and management

  • Location-based analytics

  • IoT sensor networks

  • Movement pattern analysis

  • Geofencing with historical context

Both extensions offer much more functionality than shown here, enabling complex analyses of geospatial time-series data.

Get Started with PostGIS + Timescale

Remember, efficient data analysis is all about choosing the right tools. With PostGIS and Timescale in your arsenal, you're equipped to face a multitude of data challenges, describing when things happened and where they occurred.

Happy traveling in time and space!

Learn More

Looking to learn more about extending PostgreSQL for scale and times-series scenarios? Check out the tutorials in the Timescale documentation to get started.

...

🎥 The Open Source Geospatial Community, PostGIS, & Postgres


📈 44.71 Punkte
🎥 Video | Youtube

🐧 Mit PostGIS Geoinformationen in PostgreSQL integrieren


📈 35.56 Punkte
🐧 Server

🔧 Leveraging Kaggle for Free Geographical Data: A Guide to Integrating with PostGIS via QGIS


📈 29.57 Punkte
🔧 Programmierung

🔧 NEON MAPP - XYZ Spatial Data Interfaces for 🌩 PostGIS


📈 29.57 Punkte
🔧 Programmierung

🔧 PostgreSQL in Geospatial Applications: Unleashing the Power of Location Data


📈 28.98 Punkte
🔧 Programmierung

🔧 Leveraging PostGIS to Write And Read FlatGeobuf Files


📈 28.08 Punkte
🔧 Programmierung

🔧 PostGIS and Heroku Postgres… Location, Location, Location!


📈 28.08 Punkte
🔧 Programmierung

🔧 [ptbr] Consumir Funções do PostGIS direto do Python


📈 26.95 Punkte
🔧 Programmierung

🔧 Unlocking Spatial Capabilities with PostGIS on Apache AGE


📈 26.95 Punkte
🔧 Programmierung

🕵️ PostGIS up to 2.3.2 ST_AsX3D denial of service


📈 26.95 Punkte
🕵️ Sicherheitslücken

🔧 Podcast: Geospatial Data, Data Science and More!


📈 24.12 Punkte
🔧 Programmierung

🔧 Outlier Detection in Election Data Using Geospatial Analysis - AKWA IBOM


📈 24.07 Punkte
🔧 Programmierung

📰 Use mobility data to derive insights using Amazon SageMaker geospatial capabilities


📈 24.07 Punkte
🔧 AI Nachrichten

📰 The Municipal Bond Market Is Using Geospatial Data For Climate Risk Evaluation


📈 24.07 Punkte
📰 IT Security Nachrichten

📰 Damage assessment using Amazon SageMaker geospatial capabilities and custom SageMaker models


📈 22.59 Punkte
🔧 AI Nachrichten

🔧 Build Visual Studio extensions using Visual Studio extensions


📈 21.54 Punkte
🔧 Programmierung

📰 Cobwebs’ geospatial data and spatial analysis enable orgs to get location intelligence


📈 21.5 Punkte
📰 IT Security Nachrichten

📰 The Power of Geospatial Intelligence and Similarity Analysis for Data Mapping


📈 21.5 Punkte
🔧 AI Nachrichten

📰 The Language of Maps: A Guide to Geospatial Data Formats and Coordinates


📈 21.5 Punkte
🔧 AI Nachrichten

🍏 GRASS GIS 8.2.1 - Geospatial data management, visualization and analysis.


📈 21.5 Punkte
🍏 iOS / Mac OS

📰 Monitoring Lake Mead drought using the new Amazon SageMaker geospatial capabilities


📈 21.46 Punkte
🔧 AI Nachrichten

🔧 Introducing pgzx: create PostgreSQL extensions using Zig


📈 21.22 Punkte
🔧 Programmierung

📰 3D Geospatial Data Integration with Python: The Ultimate Guide


📈 20.38 Punkte
🔧 AI Nachrichten

📰 5 Visualizations with Python to Show Simultaneous Changes in Geospatial Data


📈 20.38 Punkte
🔧 AI Nachrichten

📰 How to Learn Geospatial Data Science in 2023


📈 20.38 Punkte
🔧 AI Nachrichten

📰 Geospatial Data Wrangling for Pandas Experts


📈 20.38 Punkte
🔧 AI Nachrichten

📰 Build an agronomic data platform with Amazon SageMaker geospatial capabilities


📈 20.38 Punkte
🔧 AI Nachrichten

📰 AI can alter geospatial data to create deepfake geography


📈 20.38 Punkte
📰 IT Security Nachrichten

📰 The Role Of Geospatial Data In Cybersecurity


📈 20.38 Punkte
📰 IT Security Nachrichten