From 677809e06bb180fa56f32f064ded41850e2a4823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 10 Feb 2021 00:08:55 +0200 Subject: [PATCH] IV: finding bends --- IV/bend.sql | 26 +++++++++++++++++++++++++ IV/db | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ IV/figures.sql | 4 ++++ 3 files changed, 81 insertions(+) create mode 100644 IV/bend.sql create mode 100755 IV/db create mode 100644 IV/figures.sql diff --git a/IV/bend.sql b/IV/bend.sql new file mode 100644 index 0000000..272f8ae --- /dev/null +++ b/IV/bend.sql @@ -0,0 +1,26 @@ +create or replace function detect_bends(line geometry) returns table(bend geometry) as $$ +/* for each bend, should return: + - size (area) + - shape (cmp, compactness index) +*/ +declare + pi real; + p geometry; + p1 geometry; + p2 geometry; + p3 geometry; +begin + pi = radians(180); + + for p in (select (dp).geom from st_dumppoints(line) as dp) loop + p3 = p2; + p2 = p1; + p1 = p; + if p3 is null then continue; end if; + raise notice 'ANGLE %', degrees(pi - st_angle(p1, p2, p2, p3)); + end loop; +end +$$ language plpgsql; + +drop table if exists bends; +create table bends as (select * from detect_bends((select way from figures where name='fig3'))); diff --git a/IV/db b/IV/db new file mode 100755 index 0000000..0bfccfd --- /dev/null +++ b/IV/db @@ -0,0 +1,51 @@ +#!/bin/bash +set -euo pipefail + +name=wm-mj + +_psql() { + env \ + PGPASSWORD=osm \ + PGHOST=127.0.0.1 \ + PGUSER=osm \ + PGDATABASE=osm \ + psql "$@" +} + +_wait_for_postgres() { + >&2 echo -n "Waiting for postgres" + for _ in $(seq 240); do + if _psql -qc '\q' 2>/dev/null; then + >&2 echo " up" + exit 0 + fi + >&2 echo -n . + sleep 1 + done + >&2 echo " down" + exit 1 +} + +case ${1:-} in + start) + docker run -d --rm \ + -p 5432:5432 \ + -e POSTGRES_DBNAME=osm \ + -e POSTGRES_USER=osm \ + -e POSTGRES_PASSWORD=osm \ + --name "$name" \ + postgis/postgis:13-3.1-alpine + _wait_for_postgres + ;; + stop) + docker stop "$name" + ;; + "" | --) + [[ $# -gt 1 ]] && shift + _psql "$@" + ;; + *) + >&2 echo "Unknown command: '$*'" + exit 1 + ;; +esac diff --git a/IV/figures.sql b/IV/figures.sql new file mode 100644 index 0000000..6b1b50f --- /dev/null +++ b/IV/figures.sql @@ -0,0 +1,4 @@ +drop table if exists figures; +create table figures (name text, way geometry); + +insert into figures (name, way) values ('fig3', ST_GeomFromText('LINESTRING(0 0, 12 0, 13 4, 20 2, 20 0, 32 0, 33 10, 38 16, 43 15, 44 10, 44 0, 60 0)'));