Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Improvements for voltage tag handling

Russ Garrett2407d09ae8f8
diff --git a/schema/functions.sql b/schema/functions.sql
index 8a41a2d..1e95ed9 100644
--- a/schema/functions.sql
+++ b/schema/functions.sql
@@ -51,7 +51,7 @@ DECLARE
parts TEXT[];
res NUMERIC;
BEGIN
- parts := regexp_matches(value, '([0-9][0-9,]+)[;]?.*', '');
+ parts := regexp_matches(value, '([0-9][0-9,]*)[;]?.*', '');
BEGIN
res := replace(parts[1], ',', '.')::NUMERIC;
EXCEPTION WHEN OTHERS THEN
@@ -61,6 +61,39 @@ BEGIN
END
$$ LANGUAGE plpgsql;
+-- Convert a text numeric value into a number. Both the period (.) and the comma (,)
+-- are accepted as decimal separators.
+-- Trailing text is discarded. Leading text, or any other invalid value, results in a NULL.
+CREATE OR REPLACE FUNCTION convert_number(value TEXT) RETURNS NUMERIC
+IMMUTABLE
+PARALLEL SAFE
+RETURNS NULL ON NULL INPUT
+AS $$
+DECLARE
+ parts TEXT[];
+ res NUMERIC;
+BEGIN
+ parts := regexp_matches(value, '^([0-9][0-9,\.]*)', '');
+ res := replace(parts[1], ',', '.')::NUMERIC;
+ RETURN res;
+END
+$$ LANGUAGE plpgsql;
+
+-- Convert a text integer value into an integer
+-- Trailing text is discarded. Leading text, or any other invalid value, results in a NULL.
+CREATE OR REPLACE FUNCTION convert_integer(value TEXT) RETURNS INTEGER
+IMMUTABLE
+PARALLEL SAFE
+RETURNS NULL ON NULL INPUT
+AS $$
+DECLARE
+ parts TEXT[];
+BEGIN
+ parts := regexp_matches(value, '^([0-9]+)', '');
+ RETURN parts[1];
+END
+$$ LANGUAGE plpgsql;
+
-- Get the nth element of a semicolon-delimited list
CREATE OR REPLACE FUNCTION nth_semi(input TEXT, index INTEGER) RETURNS TEXT
PARALLEL SAFE
@@ -82,8 +115,7 @@ AS $$
DECLARE
parts TEXT[];
BEGIN
- parts = string_to_array(input, ';');
- RETURN parts[1];
+ RETURN nth_semi(input, 1);
END
$$ LANGUAGE plpgsql;
@@ -94,23 +126,37 @@ IMMUTABLE
AS $$
DECLARE
parts TEXT[];
- voltage_int REAL;
+ voltage_tmp INTEGER;
+ int_voltages INTEGER[];
+ array_size INTEGER;
retval REAL[];
BEGIN
parts = string_to_array(voltage, ';');
IF array_length(parts::anyarray, 1) > 1 THEN
- FOR I IN array_lower(parts::anyarray, 1)..array_upper(parts::anyarray, 1) LOOP
- retval[I] = convert_voltage(parts[I]) / 1000;
- END LOOP;
+ FOR I IN array_lower(parts::anyarray, 1)..array_upper(parts::anyarray, 1) LOOP
+ int_voltages[I] = convert_integer(parts[I]);
+ END LOOP;
ELSIF circuits IS NOT NULL THEN
- voltage_int = convert_voltage(voltage) / 1000;
- FOR I IN 1..circuits LOOP
- retval[I] = voltage_int;
- END LOOP;
+ voltage_tmp = convert_integer(voltage);
+ FOR I IN 1..circuits LOOP
+ int_voltages[I] = voltage_tmp;
+ END LOOP;
ELSE
- retval[1] = convert_voltage(voltage) / 1000;
+ int_voltages[1] = convert_integer(voltage);
END IF;
+ -- Remove NULLs and sort the array
+ int_voltages = sort(array_remove(int_voltages, NULL));
+ array_size = array_upper(int_voltages, 1);
+ IF array_size = 0 OR array_size IS NULL THEN
+ RETURN NULL;
+ END IF;
+
+ -- Convert to kV and reverse the sorted array so highest voltage is first.
+ FOR I IN 1..array_size LOOP
+ retval[array_size - I + 1] = int_voltages[I]::NUMERIC / 1000;
+ END LOOP;
+
return retval;
END
$$ LANGUAGE plpgsql;
diff --git a/tegola/layers.yml b/tegola/layers.yml
index afc751f..78573f0 100644
--- a/tegola/layers.yml
+++ b/tegola/layers.yml
@@ -17,11 +17,13 @@ field_sets:
sql: round(ST_Area(geometry))
voltages:
- name: voltage
- sql: convert_voltage(voltage) / 1000
+ sql: convert_number(nth_semi(voltage, 1)) / 1000
- name: voltage_2
- sql: convert_voltage(nth_semi(voltage, 2)) / 1000
+ sql: convert_number(nth_semi(voltage, 2)) / 1000
- name: voltage_3
- sql: convert_voltage(nth_semi(voltage, 3)) / 1000
+ sql: convert_number(nth_semi(voltage, 3)) / 1000
+ - name: voltage_4
+ sql: convert_number(nth_semi(voltage, 4)) / 1000
wiki:
- name: wikidata
sql: tags -> 'wikidata'
@@ -126,11 +128,11 @@ layers:
# below that.
where: >
ST_Length(geometry) > !PIXEL_WIDTH! / 4 AND
- (convert_voltage(voltage) > 199000 OR
- !ZOOM! >= 4 AND convert_voltage(voltage) > 100000 OR
- !ZOOM! >= 6 AND convert_voltage(voltage) > 24000 OR
+ (voltages[1] > 199 OR
+ !ZOOM! >= 4 AND voltages[1] > 100 OR
+ !ZOOM! >= 6 AND voltages[1] > 24 OR
!ZOOM! >= 8)
- order_by: convert_voltage(voltage) ASC NULLS FIRST
+ order_by: voltages[1] ASC NULLS FIRST
- name: power_tower
map: