view schema/search_functions.sql @ 3703:b07511ff859e

Don't include calculated area in unchanged bottleneck detection. This makes BN imports considerably faster. The only downside is, that when die waterway area changes there is no easy way to recalculate the areas of existing BN. But the semantics in that case are somewhat hard anyway (think of historization for the old area) so this should be ok.
author Sascha Wilde <wilde@intevation.de>
date Wed, 19 Jun 2019 12:34:48 +0200
parents 194feb1a7e37
children b1fe61b58048
line wrap: on
line source

-- This is Free Software under GNU Affero General Public License v >= 3.0
-- without warranty, see README.md and license for details.

-- SPDX-License-Identifier: AGPL-3.0-or-later
-- License-Filename: LICENSES/AGPL-3.0.txt

-- Copyright (C) 2018 by via donau
--   – Österreichische Wasserstraßen-Gesellschaft mbH
-- Software engineering by Intevation GmbH

-- Author(s):
--  * Sascha Wilde <wilde@intevation.de>

CREATE OR REPLACE FUNCTION search_bottlenecks(search_string text) RETURNS jsonb
  LANGUAGE plpgsql
  AS $$
DECLARE
  _result jsonb;
BEGIN
  SELECT COALESCE(json_agg(r),'[]')
    INTO _result
    FROM (SELECT objnam AS name,
                 ST_AsGeoJSON(ST_Centroid(area))::json AS geom,
                 'bottleneck' AS type
            FROM waterway.bottlenecks
            WHERE objnam ILIKE '%' || search_string || '%'
          ORDER BY name) r;
  RETURN _result;
END;
$$;

CREATE OR REPLACE FUNCTION search_cities(search_string text) RETURNS jsonb
  LANGUAGE plpgsql
  AS $$
DECLARE
  _result jsonb;
BEGIN
  SELECT COALESCE(json_agg(r),'[]')
    INTO _result
    FROM (SELECT name || ' (' || country_code || ')' AS name,
                 ST_AsGeoJSON(location)::json AS geom,
                 'city' AS type
            FROM waterway.geonames
            WHERE feature_code IN ('PPLA', 'PPLA1', 'PPLA2', 'PPLA3', 'PPLC')
                  AND (name ILIKE '%' || search_string || '%'
                       OR alternatenames ~* ('(^|,)' || search_string || '($|,)'))
            ORDER BY array_position(ARRAY['PPLC', 'PPLA', 'PPLA1', 'PPLA2', 'PPLA3'],
                                          feature_code::text),
                     name) r;
  RETURN _result;
END;
$$;

CREATE OR REPLACE FUNCTION search_gauges(search_string text) RETURNS jsonb
  LANGUAGE plpgsql
  AS $$
DECLARE
  _result jsonb;
BEGIN
  SELECT COALESCE(json_agg(r),'[]')
    INTO _result
    FROM (SELECT objname AS name,
                 ST_AsGeoJSON(geom)::json AS geom,
                 'gauge' AS type
            FROM waterway.gauges
            WHERE objname ILIKE '%' || search_string || '%'
          ORDER BY name) r;
  RETURN _result;
END;
$$;

CREATE OR REPLACE FUNCTION search_sections(search_string text) RETURNS jsonb
  LANGUAGE plpgsql
  AS $$
DECLARE
  _result jsonb;
BEGIN
  SELECT COALESCE(json_agg(r),'[]')
    INTO _result
    FROM (SELECT objnam AS name,
                 ST_AsGeoJSON(ST_Centroid(area))::json AS geom,
                 'section' AS type
            FROM waterway.sections
            WHERE objnam ILIKE '%' || search_string || '%'
               OR nobjnam ILIKE '%' || search_string || '%'
          ORDER BY name) r;
  RETURN _result;
END;
$$;

CREATE OR REPLACE FUNCTION search_most(search_string text) RETURNS jsonb
  LANGUAGE plpgsql
  AS $$
BEGIN
  RETURN search_bottlenecks(search_string)
         || search_gauges(search_string)
         || search_sections(search_string)
         || search_cities(search_string);
END;
$$;