Skip to content

Commit 55cbe6e

Browse files
committed
add postgis example
1 parent 62bd001 commit 55cbe6e

File tree

11 files changed

+171
-1
lines changed

11 files changed

+171
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- Display all parameters in the debug component (instead of only row-level parameters).
77
- Update dashmap for better file lookup performance.
88
- Fix table sorting.
9+
- Fix a bug with Basic Authentication.
10+
See [#72](https://github.com/lovasoa/SQLpage/pull/72). Thanks to @edgrip for the contribution !
911

1012
## 0.10.0 (2023-08-20)
1113

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM debian:12-slim
2+
3+
WORKDIR /var/www
4+
5+
ADD https://github.com/lovasoa/SQLpage/releases/download/v0.7.2/sqlpage-linux.tgz .
6+
7+
RUN apt-get update && \
8+
apt-get -y install sqlite3 libsqlite3-mod-spatialite && \
9+
tar xvzf sqlpage-linux.tgz && \
10+
rm sqlpage-linux.tgz
11+
12+
COPY . .
13+
14+
CMD ["sqlite3"]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Using PostGIS to build a geographic data application
2+
3+
## Introduction
4+
5+
This is a small application that uses [PostGIS](https://postgis.net/)
6+
to save data associated to greographic coordinates.
7+
8+
If you are using a SQLite database instead see [this other example instead](../make%20a%20geographic%20data%20application%20using%20sqlite%20extensions/),
9+
which implements the same application using the `spatialite` extension.
10+
11+
### Installation
12+
13+
You need to install the `postgis` extension for postgres. Follow [the official instructions](https://postgis.net/documentation/getting_started/).
14+
15+
Then you can instruct SQLPage to connect to your database by editing the [`sqlpage/sqlpage.json`](./sqlpage/sqlpage.json) file.
16+
17+
## Screenshots
18+
19+
![](./screenshots/code.png)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
INSERT INTO spatial_data (title, geom, description)
2+
VALUES (
3+
:Title,
4+
ST_MakePoint(
5+
CAST(:Longitude AS REAL),
6+
CAST(:Latitude AS REAL
7+
), 4326),
8+
:Text
9+
) RETURNING
10+
'redirect' AS component,
11+
'index.sql' AS link;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
SELECT 'shell' as component,
2+
'Map' AS title,
3+
'/' as link,
4+
'book' as icon;
5+
6+
SELECT 'map' AS component,
7+
ST_Y(geom) AS latitude,
8+
ST_X(geom) AS longitude
9+
FROM spatial_data
10+
WHERE id = $id::int;
11+
12+
SELECT 'map' as component,
13+
'Points of interest' as title,
14+
2 as zoom,
15+
700 as height;
16+
SELECT title,
17+
ST_Y(geom) AS latitude,
18+
ST_X(geom) AS longitude,
19+
'point.sql?id=' || id as link
20+
FROM spatial_data;
21+
22+
23+
SELECT 'list' as component,
24+
'My points' as title;
25+
SELECT title,
26+
'point.sql?id=' || id as link,
27+
'red' as color,
28+
'world-pin' as icon
29+
FROM spatial_data;
30+
31+
SELECT 'form' AS component,
32+
'Add a point' AS title,
33+
'add_point.sql' AS action;
34+
35+
SELECT 'Title' AS name;
36+
SELECT 'Latitude' AS name, 'number' AS type, 0.00000001 AS step;
37+
SELECT 'Longitude' AS name, 'number' AS type, 0.00000001 AS step;
38+
SELECT 'Text' AS name, 'textarea' AS type;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
SELECT 'shell' as component,
2+
title,
3+
'/' as link,
4+
'index' as menu_item,
5+
'book' as icon
6+
FROM spatial_data
7+
WHERE id = $id::int;
8+
9+
SELECT 'datagrid' as component, title FROM spatial_data WHERE id = $id::int;
10+
11+
SELECT 'Latitude' as title,
12+
ST_Y(geom) as description,
13+
'purple' as color,
14+
'world-latitude' as icon
15+
FROM spatial_data WHERE id = $id::int;
16+
17+
SELECT 'Longitude' as title,
18+
ST_X(geom) as description,
19+
'purple' as color,
20+
'world-longitude' as icon
21+
FROM spatial_data WHERE id = $id::int;
22+
23+
SELECT 'Created at' as title,
24+
created_at as description,
25+
'calendar' as icon,
26+
'Date and time of creation' as footer
27+
FROM spatial_data WHERE id = $id::int;
28+
29+
SELECT 'Label' as title,
30+
title as description,
31+
'geo:' || ST_Y(geom) || ',' || ST_X(geom) || '?z=16' AS link,
32+
'blue' as color,
33+
'world' as icon,
34+
'User-generated point name' as footer
35+
FROM spatial_data
36+
WHERE id = $id::int;
37+
38+
SELECT 'text' as component,
39+
description as contents_md
40+
FROM spatial_data
41+
WHERE id = $id::int;
42+
43+
SELECT 'list' as component, 'Closest points' as title;
44+
SELECT to_label as title,
45+
ROUND(distance::decimal, 3) || ' km' as description,
46+
'point.sql?id=' || to_id as link,
47+
'red' as color,
48+
'world-pin' as icon
49+
FROM distances
50+
WHERE from_id = $id::int
51+
ORDER BY distance
52+
LIMIT 5;
53+
54+
SELECT 'map' AS component,
55+
ST_Y(geom) AS latitude, ST_X(geom) AS longitude
56+
FROM spatial_data WHERE id = $id::int;
846 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CREATE EXTENSION IF NOT EXISTS postgis;
2+
3+
-- Create a spatialite-enabled database
4+
CREATE TABLE spatial_data (
5+
id SERIAL,
6+
title TEXT NOT NULL,
7+
geom geometry,
8+
description TEXT NOT NULL,
9+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
10+
);
11+
12+
13+
CREATE VIEW distances AS
14+
SELECT from_point.id AS from_id,
15+
from_point.title AS from_label,
16+
to_point.id AS to_id,
17+
to_point.title AS to_label,
18+
ST_Distance(
19+
from_point.geom,
20+
to_point.geom,
21+
TRUE
22+
) AS distance
23+
FROM spatial_data AS from_point, spatial_data AS to_point
24+
WHERE from_point.id != to_point.id;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database_url": "postgres://my_username:my_password@localhost:5432/my_geographic_database"
3+
}

examples/make a geographic data application using sqlite extensions/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM debian:12-slim
22

33
WORKDIR /var/www
44

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

77
RUN apt-get update && \
88
apt-get -y install sqlite3 libsqlite3-mod-spatialite && \

0 commit comments

Comments
 (0)