forked from norBIT/alkisimport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nas2alb-functions.sql
99 lines (90 loc) · 3.83 KB
/
nas2alb-functions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/***************************************************************************
* *
* Project: norGIS ALKIS Import *
* Purpose: ALB-Daten in norBIT WLDGE-Strukturen aus ALKIS-Daten füllen *
* Author: Jürgen E. Fischer <[email protected]> *
* *
***************************************************************************
* Copyright (c) 2012-2018, Jürgen E. Fischer <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
\unset ON_ERROR_STOP
SET application_name='ALKIS-Import - Liegenschaftsbuchübernahme';
SET client_min_messages TO notice;
\set ON_ERROR_STOP
SET search_path = :"alkis_schema", :"postgis_schema", public;
CREATE OR REPLACE FUNCTION alkis_toint(v anyelement) RETURNS integer AS $$
DECLARE
res integer;
BEGIN
SELECT v::int INTO res;
RETURN res;
EXCEPTION WHEN OTHERS THEN
RETURN NULL;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION alkis_round(double precision) RETURNS varchar AS $$
SELECT regexp_replace(regexp_replace(round($1::numeric,9)::text, E'(\\.\\d*[1-9])0+$', E'\\1'), E'\\.0+$', '');
$$ LANGUAGE 'sql' IMMUTABLE;
CREATE OR REPLACE FUNCTION alkis_flsnrk(f ax_flurstueck) RETURNS varchar AS $$
BEGIN
RETURN
CASE
WHEN f.gml_id LIKE 'DESL%' THEN
to_char(alkis_toint(f.zaehler),'fm0000') || '/' || to_char(coalesce(alkis_toint(f.nenner),0),'fm0000')
WHEN f.gml_id LIKE 'DETH%' THEN
to_char(alkis_toint(f.zaehler),'fm00000') || '/' || to_char(coalesce(alkis_toint(f.nenner),0),'fm0000')
WHEN f.gml_id LIKE 'DESN%' OR f.gml_id LIKE 'DETH%' THEN
to_char(alkis_toint(f.zaehler),'fm00000') || '/' || substring(f.flurstueckskennzeichen,15,4)
ELSE
to_char(alkis_toint(f.zaehler),'fm00000') || '/' || to_char(coalesce(mod(alkis_toint(f.nenner),1000)::int,0),'fm000')
END;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT SET search_path = :"alkis_schema", :"postgis_schema", public;
CREATE OR REPLACE FUNCTION alkis_flsnr(f ax_flurstueck) RETURNS varchar AS $$
BEGIN
RETURN
to_char(alkis_toint(f.land),'fm00') || to_char(alkis_toint(f.gemarkungsnummer),'fm0000') ||
'-' || to_char(coalesce(f.flurnummer,0),'fm000') ||
'-' || alkis_flsnrk(f);
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT SET search_path = :"alkis_schema", :"postgis_schema", public;
CREATE OR REPLACE FUNCTION alkis_flskoord(f ax_flurstueck) RETURNS varchar AS $$
DECLARE
g GEOMETRY;
BEGIN
BEGIN
SELECT st_pointonsurface(f.wkb_geometry) INTO g;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'st_pointonsurface-Ausnahme bei %', alkis_flsnr(f);
BEGIN
SELECT st_centroid(f.wkb_geometry) INTO g;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'st_centroid-Ausnahme bei %', alkis_flsnr(f);
RETURN NULL;
END;
END;
RETURN to_char(st_x(g)*10::int,'fm00000000') ||' '|| to_char(st_y(g)*10::int,'fm00000000');
END;
$$ LANGUAGE plpgsql IMMUTABLE;
SELECT alkis_dropobject('alkis_truncate');
CREATE OR REPLACE FUNCTION alkis_truncate(s0 varchar, l integer) RETURNS varchar AS $$
DECLARE
s VARCHAR := trim(s0);
BEGIN
IF length(s)>l THEN
IF l>3 THEN
RETURN substr(s, 1, l-3) || '...';
ELSE
RETURN substr(s, 1, l);
END IF;
ELSE
RETURN s;
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE;