Skip to content

Commit

Permalink
publish an example for using geographical data
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Jul 11, 2023
1 parent f259a90 commit 575bbfd
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
services:
test:
image: debian:stable-slim
volumes:
- .:/var/www
depends_on:
- db
environment:
DATABASE_URL: ${DB:-postgres}://root:secret@db/sqlpage
web:
build: { context: "." }
ports:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM debian:12-slim

WORKDIR /var/www

ADD https://github.com/lovasoa/SQLpage/releases/download/v0.7.2/sqlpage-linux.tgz .

RUN apt-get update && \
apt-get -y install sqlite3 libsqlite3-mod-spatialite && \
tar xvzf sqlpage-linux.tgz && \
rm sqlpage-linux.tgz

COPY . .

CMD ["sqlite3"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Using spatialite to build a geographic data application

## Introduction

This is a small application that uses [spatialite](https://www.gaia-gis.it/fossil/libspatialite/index)
to save data associated to greographic coordinates.

### Installation

You need to install the `spatialite` extension for SQLite. On Debian, or Ubuntu, you can do it with:

```bash
sudo apt install libsqlite3-mod-spatialite
```

Then you can run this application normally with SQLPage.

Notice the `sqlite_extensions` configuration parameter in [`sqlpage/sqlpage.json`](./sqlpage/sqlpage.json).

## Screenshots

![](./screenshots/code.png)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
INSERT INTO spatial_data (label, geom)
VALUES (
:Label,
MakePoint(
CAST(:Longitude AS REAL),
CAST(:Latitude AS REAL
), 4326)
) RETURNING
'redirect' AS component,
'index.sql' AS link;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT 'list' as component,
'My points' as title;
SELECT label as title,
'point.sql?id=' || id as link,
'red' as color,
'world-pin' as icon
FROM spatial_data;

SELECT 'form' AS component,
'Add a point' AS title,
'add_point.sql' AS action;

SELECT 'Label' AS name;
SELECT 'Latitude' AS name, 'number' AS type, 0.00000001 AS step;
SELECT 'Longitude' AS name, 'number' AS type, 0.00000001 AS step;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SELECT 'datagrid' as component, label as title FROM spatial_data WHERE id = $id;

SELECT 'Latitude' as title,
ST_Y(geom) as description,
'purple' as color,
'world-latitude' as icon
FROM spatial_data WHERE id = $id;

SELECT 'Longitude' as title,
ST_X(geom) as description,
'purple' as color,
'world-longitude' as icon
FROM spatial_data WHERE id = $id;

SELECT 'Created at' as title,
created_at as description,
'calendar' as icon,
'Date and time of creation' as footer
FROM spatial_data WHERE id = $id;

SELECT 'Label' as title,
label as description,
'geo:' || ST_Y(geom) || ',' || ST_X(geom) || '?z=16' AS link,
'blue' as color,
'world' as icon,
'User-generated point name' as footer
FROM spatial_data
WHERE id = $id;

SELECT 'list' as component, 'Closest points' as title;
SELECT to_label as title,
ROUND(CvtToKm(distance), 3) || ' km' as description,
'point.sql?id=' || to_id as link,
'red' as color,
'world-pin' as icon
FROM distances
WHERE from_id = $id
ORDER BY distance
LIMIT 5;

SELECT 'map' AS component,
ST_Y(geom) AS latitude, ST_X(geom) AS longitude
FROM spatial_data WHERE id = $id;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Create a spatialite-enabled database
CREATE TABLE spatial_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT NOT NULL,
geom POINT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

SELECT InitSpatialMetaData();

CREATE VIEW distances AS
SELECT from_point.id AS from_id,
from_point.label AS from_label,
to_point.id AS to_id,
to_point.label AS to_label,
ST_Distance(
from_point.geom,
to_point.geom,
TRUE
) AS distance
FROM spatial_data AS from_point, spatial_data AS to_point
WHERE from_point.id != to_point.id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sqlite_extensions": [
"mod_spatialite"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<iframe
height="350"
frameborder="0"
scrolling="no"
marginheight="0"
marginwidth="0"
src="https://www.openstreetmap.org/export/embed.html?bbox={{longitude}}%2C{{latitude}}"
></iframe>

0 comments on commit 575bbfd

Please sign in to comment.