
jbe@73: SET LOCAL search_path TO @extschema@;
jbe@73: 
jbe@0: 
jbe@0: ------------------------------------------------------------
jbe@0: -- dummy input/output functions for dummy index key types --
jbe@0: ------------------------------------------------------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ekey_point_in_dummy(cstring)
jbe@0:   RETURNS ekey_point
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_notimpl';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ekey_point_out_dummy(ekey_point)
jbe@0:   RETURNS cstring
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_notimpl';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ekey_area_in_dummy(cstring)
jbe@0:   RETURNS ekey_area
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_notimpl';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ekey_area_out_dummy(ekey_area)
jbe@0:   RETURNS cstring
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_notimpl';
jbe@0: 
jbe@0: 
jbe@0: --------------------------
jbe@0: -- text input functions --
jbe@0: --------------------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_in(cstring)
jbe@0:   RETURNS epoint
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_in';
jbe@46: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_with_sample_count_in(cstring)
jbe@46:   RETURNS epoint_with_sample_count
jbe@46:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_with_sample_count_in';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_in(cstring)
jbe@0:   RETURNS ebox
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_in';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_in(cstring)
jbe@0:   RETURNS ecircle
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_in';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecluster_in(cstring)
jbe@0:   RETURNS ecluster
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecluster_in';
jbe@0: 
jbe@0: 
jbe@0: ---------------------------
jbe@0: -- text output functions --
jbe@0: ---------------------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_out(epoint)
jbe@0:   RETURNS cstring
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_out';
jbe@46: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_with_sample_count_out(epoint_with_sample_count)
jbe@46:   RETURNS cstring
jbe@46:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_with_sample_count_out';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_out(ebox)
jbe@0:   RETURNS cstring
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_out';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_out(ecircle)
jbe@0:   RETURNS cstring
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_out';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecluster_out(ecluster)
jbe@0:   RETURNS cstring
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecluster_out';
jbe@0: 
jbe@0: 
jbe@0: --------------------------
jbe@0: -- binary I/O functions --
jbe@0: --------------------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_recv(internal)
jbe@0:   RETURNS epoint
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_recv';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_recv(internal)
jbe@0:   RETURNS ebox
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_recv';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_recv(internal)
jbe@0:   RETURNS ecircle
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_recv';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_send(epoint)
jbe@0:   RETURNS bytea
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_send';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_send(ebox)
jbe@69:   RETURNS bytea
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_send';
jbe@46: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_send(ecircle)
jbe@69:   RETURNS bytea
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_send';
jbe@0: 
jbe@0: 
jbe@0: --------------------
jbe@0: -- B-tree support --
jbe@0: --------------------
jbe@0: 
jbe@0: -- begin of B-tree support for epoint
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_btree_lt(epoint, epoint)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_epoint_lt';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_btree_le(epoint, epoint)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_epoint_le';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_btree_eq(epoint, epoint)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_epoint_eq';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_btree_ne(epoint, epoint)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_epoint_ne';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_btree_ge(epoint, epoint)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_epoint_ge';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_btree_gt(epoint, epoint)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_epoint_gt';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_btree_cmp(epoint, epoint)
jbe@0:   RETURNS int4
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_epoint_cmp';
jbe@0: 
jbe@0: -- end of B-tree support for epoint
jbe@0: 
jbe@0: -- begin of B-tree support for ebox
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_btree_lt(ebox, ebox)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ebox_lt';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_btree_le(ebox, ebox)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ebox_le';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_btree_eq(ebox, ebox)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ebox_eq';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_btree_ne(ebox, ebox)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ebox_ne';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_btree_ge(ebox, ebox)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ebox_ge';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_btree_gt(ebox, ebox)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ebox_gt';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_btree_cmp(ebox, ebox)
jbe@0:   RETURNS int4
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ebox_cmp';
jbe@0: 
jbe@0: -- end of B-tree support for ebox
jbe@0: 
jbe@0: -- begin of B-tree support for ecircle
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_btree_lt(ecircle, ecircle)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_lt';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_btree_le(ecircle, ecircle)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_le';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_btree_eq(ecircle, ecircle)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_eq';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_btree_ne(ecircle, ecircle)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_ne';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_btree_ge(ecircle, ecircle)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_ge';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_btree_gt(ecircle, ecircle)
jbe@69:   RETURNS boolean
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_gt';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_btree_cmp(ecircle, ecircle)
jbe@0:   RETURNS int4
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_cmp';
jbe@0: 
jbe@0: -- end of B-tree support for ecircle
jbe@0: 
jbe@0: 
jbe@0: ----------------
jbe@0: -- type casts --
jbe@0: ----------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION cast_epoint_to_ebox(epoint)
jbe@0:   RETURNS ebox
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_to_ebox';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION cast_epoint_to_ecircle(epoint)
jbe@0:   RETURNS ecircle
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_to_ecircle';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION cast_epoint_to_ecluster(epoint)
jbe@0:   RETURNS ecluster
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_to_ecluster';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION cast_ebox_to_ecluster(ebox)
jbe@0:   RETURNS ecluster
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_to_ecluster';
jbe@0: 
jbe@0: 
jbe@0: ---------------------------
jbe@0: -- constructor functions --
jbe@0: ---------------------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint(float8, float8)
jbe@0:   RETURNS epoint
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_create_epoint';
jbe@0: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_latlon(float8, float8)
jbe@73:   RETURNS epoint
jbe@73:   LANGUAGE SQL IMMUTABLE STRICT AS $$
jbe@73:     SELECT @extschema@.epoint($1, $2)
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_lonlat(float8, float8)
jbe@73:   RETURNS epoint
jbe@73:   LANGUAGE SQL IMMUTABLE STRICT AS $$
jbe@73:     SELECT @extschema@.epoint($2, $1)
jbe@73:   $$;
jbe@73: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_with_sample_count(epoint, int4)
jbe@46:   RETURNS epoint_with_sample_count
jbe@46:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_create_epoint_with_sample_count';
jbe@46: 
jbe@69: CREATE OR REPLACE FUNCTION empty_ebox()
jbe@0:   RETURNS ebox
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_create_empty_ebox';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox(float8, float8, float8, float8)
jbe@0:   RETURNS ebox
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_create_ebox';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox(epoint, epoint)
jbe@69:   RETURNS ebox
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_create_ebox_from_epoints';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle(float8, float8, float8)
jbe@0:   RETURNS ecircle
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_create_ecircle';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle(epoint, float8)
jbe@69:   RETURNS ecircle
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_create_ecircle_from_epoint';
jbe@0: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_concat(ecluster[])
jbe@73:   RETURNS ecluster
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT pg_catalog.array_to_string($1, ' ')::@extschema@.ecluster
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_concat(ecluster, ecluster)
jbe@73:   RETURNS ecluster
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT (
jbe@73:       $1::pg_catalog.text OPERATOR(pg_catalog.||) ' ' OPERATOR(pg_catalog.||)
jbe@73:       $2::pg_catalog.text
jbe@73:     )::@extschema@.ecluster
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_create_multipoint(epoint[])
jbe@73:   RETURNS ecluster
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT
jbe@73:       pg_catalog.array_to_string(
jbe@73:         pg_catalog.array_agg(
jbe@73:           'point (' OPERATOR(pg_catalog.||) unnest OPERATOR(pg_catalog.||) ')'
jbe@73:         ),
jbe@73:         ' '
jbe@73:       )::@extschema@.ecluster
jbe@73:     FROM pg_catalog.unnest($1)
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_create_path(epoint[])
jbe@73:   RETURNS ecluster
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT CASE WHEN "str" OPERATOR(pg_catalog.=) '' THEN
jbe@73:       'empty'::@extschema@.ecluster
jbe@73:     ELSE
jbe@73:       (
jbe@73:         'path (' OPERATOR(pg_catalog.||) "str" OPERATOR(pg_catalog.||) ')'
jbe@73:       )::@extschema@.ecluster
jbe@73:     END
jbe@73:     FROM pg_catalog.array_to_string($1, ' ') AS "str"
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_create_outline(epoint[])
jbe@73:   RETURNS ecluster
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT CASE WHEN "str" OPERATOR(pg_catalog.=) '' THEN
jbe@73:       'empty'::@extschema@.ecluster
jbe@73:     ELSE
jbe@73:       (
jbe@73:         'outline (' OPERATOR(pg_catalog.||) "str" OPERATOR(pg_catalog.||) ')'
jbe@73:       )::@extschema@.ecluster
jbe@73:     END
jbe@73:     FROM pg_catalog.array_to_string($1, ' ') AS "str"
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_create_polygon(epoint[])
jbe@73:   RETURNS ecluster
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT CASE WHEN "str" OPERATOR(pg_catalog.=) '' THEN
jbe@73:       'empty'::@extschema@.ecluster
jbe@73:     ELSE
jbe@73:       (
jbe@73:         'polygon (' OPERATOR(pg_catalog.||) pg_catalog.array_to_string($1, ' ')
jbe@73:         OPERATOR(pg_catalog.||) ')'
jbe@73:       )::@extschema@.ecluster
jbe@73:     END
jbe@73:     FROM pg_catalog.array_to_string($1, ' ') AS "str"
jbe@73:   $$;
jbe@73: 
jbe@0: 
jbe@0: ----------------------
jbe@0: -- getter functions --
jbe@0: ----------------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION latitude(epoint)
jbe@0:   RETURNS float8
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_lat';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION longitude(epoint)
jbe@0:   RETURNS float8
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_lon';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION min_latitude(ebox)
jbe@0:   RETURNS float8
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_lat_min';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION max_latitude(ebox)
jbe@0:   RETURNS float8
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_lat_max';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION min_longitude(ebox)
jbe@69:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_lon_min';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION max_longitude(ebox)
jbe@69:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_lon_max';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION center(ecircle)
jbe@69:   RETURNS epoint
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_center';
jbe@69: 
jbe@69: CREATE OR REPLACE FUNCTION radius(ecircle)
jbe@69:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_radius';
jbe@0: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_extract_points(ecluster)
jbe@73:   RETURNS SETOF epoint
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT "match"[2]::@extschema@.epoint
jbe@73:     FROM pg_catalog.regexp_matches(
jbe@73:       $1::pg_catalog.text, e'(^| )point \\(([^)]+)\\)', 'g'
jbe@73:     ) AS "match"
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_extract_paths(ecluster)
jbe@73:   RETURNS SETOF epoint[]
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT (
jbe@73:       SELECT pg_catalog.array_agg("m2"[1]::@extschema@.epoint)
jbe@73:       FROM pg_catalog.regexp_matches("m1"[2], e'[^ ]+ [^ ]+', 'g') AS "m2"
jbe@73:     )
jbe@73:     FROM pg_catalog.regexp_matches(
jbe@73:       $1::pg_catalog.text, e'(^| )path \\(([^)]+)\\)', 'g'
jbe@73:     ) AS "m1"
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_extract_outlines(ecluster)
jbe@73:   RETURNS SETOF epoint[]
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT (
jbe@73:       SELECT pg_catalog.array_agg("m2"[1]::@extschema@.epoint)
jbe@73:       FROM pg_catalog.regexp_matches("m1"[2], e'[^ ]+ [^ ]+', 'g') AS "m2"
jbe@73:     )
jbe@73:     FROM pg_catalog.regexp_matches(
jbe@73:       $1::pg_catalog.text, e'(^| )outline \\(([^)]+)\\)', 'g'
jbe@73:     ) AS "m1"
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_extract_polygons(ecluster)
jbe@73:   RETURNS SETOF epoint[]
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT (
jbe@73:       SELECT pg_catalog.array_agg("m2"[1]::@extschema@.epoint)
jbe@73:       FROM pg_catalog.regexp_matches("m1"[2], e'[^ ]+ [^ ]+', 'g') AS "m2"
jbe@73:     )
jbe@73:     FROM pg_catalog.regexp_matches(
jbe@73:       $1::pg_catalog.text, e'(^| )polygon \\(([^)]+)\\)', 'g'
jbe@73:     ) AS "m1"
jbe@73:   $$;
jbe@73: 
jbe@0: 
jbe@0: ---------------
jbe@0: -- operators --
jbe@0: ---------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_ebox_overlap_proc(epoint, ebox)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_ebox_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_ecircle_overlap_proc(epoint, ecircle)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_ecircle_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_ecluster_overlap_proc(epoint, ecluster)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_ecluster_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_ecluster_may_overlap_proc(epoint, ecluster)
jbe@10:   RETURNS boolean
jbe@10:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_ecluster_may_overlap';
jbe@10: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_overlap_proc(ebox, ebox)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_ecircle_may_overlap_proc(ebox, ecircle)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_ecircle_may_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ebox_ecluster_may_overlap_proc(ebox, ecluster)
jbe@10:   RETURNS boolean
jbe@10:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ebox_ecluster_may_overlap';
jbe@10: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_overlap_proc(ecircle, ecircle)
jbe@16:   RETURNS boolean
jbe@16:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_overlap';
jbe@16: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_ecluster_overlap_proc(ecircle, ecluster)
jbe@10:   RETURNS boolean
jbe@10:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_ecluster_overlap';
jbe@10: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_ecluster_may_overlap_proc(ecircle, ecluster)
jbe@16:   RETURNS boolean
jbe@16:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_ecluster_may_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecluster_overlap_proc(ecluster, ecluster)
jbe@69:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecluster_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecluster_may_overlap_proc(ecluster, ecluster)
jbe@69:   RETURNS boolean
jbe@0:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecluster_may_overlap';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecluster_contains_proc(ecluster, ecluster)
jbe@69:   RETURNS boolean
jbe@16:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecluster_contains';
jbe@46: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_distance_proc(epoint, epoint)
jbe@46:   RETURNS float8
jbe@46:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_distance';
jbe@16: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_ecircle_distance_proc(epoint, ecircle)
jbe@69:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_ecircle_distance';
jbe@10: 
jbe@69: CREATE OR REPLACE FUNCTION epoint_ecluster_distance_proc(epoint, ecluster)
jbe@69:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_epoint_ecluster_distance';
jbe@16: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_distance_proc(ecircle, ecircle)
jbe@69:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_distance';
jbe@20: 
jbe@69: CREATE OR REPLACE FUNCTION ecircle_ecluster_distance_proc(ecircle, ecluster)
jbe@69:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecircle_ecluster_distance';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION ecluster_distance_proc(ecluster, ecluster)
jbe@0:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecluster_distance';
jbe@16: 
jbe@69: CREATE OR REPLACE FUNCTION fair_distance_operator_proc(ecluster, epoint_with_sample_count)
jbe@16:   RETURNS float8
jbe@69:   LANGUAGE C IMMUTABLE STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_ecluster_epoint_sc_fair_distance';
jbe@42: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ebox_overlap_commutator(ebox, epoint)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ecircle_overlap_commutator(ecircle, epoint)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ecluster_overlap_commutator(ecluster, epoint)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecircle_ecluster_overlap_commutator(ecluster, ecircle)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecircle_overlap_castwrap(ebox, ecircle)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.&&) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecircle_overlap_castwrap(ecircle, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.&&) $2::@extschema@.ecluster';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecluster_overlap_castwrap(ebox, ecluster)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.&&) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecluster_overlap_castwrap(ecluster, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.&&) $2::@extschema@.ecluster';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ecluster_may_overlap_commutator(ecluster, epoint)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecircle_may_overlap_commutator(ecircle, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecluster_may_overlap_commutator(ecluster, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecircle_ecluster_may_overlap_commutator(ecluster, ecircle)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_contains_commutator(ecluster, ecluster)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.@>) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_contains_castwrap(ebox, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS $$
jbe@73:     SELECT
jbe@73:     $1::@extschema@.ecluster OPERATOR(@extschema@.@>) $2::@extschema@.ecluster
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_contains_swapped_castwrap(ebox, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE AS $$
jbe@73:     SELECT
jbe@73:     $2::@extschema@.ecluster OPERATOR(@extschema@.@>) $1::@extschema@.ecluster
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecluster_contains_castwrap(ebox, ecluster)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.@>) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecluster_contains_castwrap(ecluster, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $2::@extschema@.ecluster OPERATOR(@extschema@.@>) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_ebox_contains_castwrap(ecluster, ebox)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.@>) $2::@extschema@.ecluster';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecluster_ebox_contains_castwrap(ebox, ecluster)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $2 OPERATOR(@extschema@.@>) $1::@extschema@.ecluster';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ecircle_distance_commutator(ecircle, epoint)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.<->) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ecluster_distance_commutator(ecluster, epoint)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.<->) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ecircle_ecluster_distance_commutator(ecluster, ecircle)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.<->) $1';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ebox_distance_castwrap(epoint, ebox)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.<->) $2::@extschema@.ecluster';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION epoint_ebox_distance_castwrap(ebox, epoint)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_distance_castwrap(ebox, ebox)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE AS $$
jbe@73:     SELECT
jbe@73:     $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2::@extschema@.ecluster
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecircle_distance_castwrap(ebox, ecircle)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecircle_distance_castwrap(ecircle, ebox)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.<->) $2::@extschema@.ecluster';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecluster_distance_castwrap(ebox, ecluster)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION ebox_ecluster_distance_castwrap(ecluster, ebox)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.<->) $2::@extschema@.ecluster';
jbe@73: 
jbe@42: 
jbe@0: ----------------
jbe@0: -- GiST index --
jbe@0: ----------------
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_consistent(internal, internal, smallint, oid, internal)
jbe@0:   RETURNS boolean
jbe@0:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_consistent';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_union(internal, internal)
jbe@0:   RETURNS internal
jbe@0:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_union';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_compress_epoint(internal)
jbe@0:   RETURNS internal
jbe@0:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_compress_epoint';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_compress_ecircle(internal)
jbe@0:   RETURNS internal
jbe@0:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_compress_ecircle';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_compress_ecluster(internal)
jbe@0:   RETURNS internal
jbe@0:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_compress_ecluster';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_decompress(internal)
jbe@69:   RETURNS internal
jbe@69:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_decompress';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_penalty(internal, internal, internal)
jbe@69:   RETURNS internal
jbe@69:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_penalty';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_picksplit(internal, internal)
jbe@69:   RETURNS internal
jbe@69:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_picksplit';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_same(internal, internal, internal)
jbe@69:   RETURNS internal
jbe@69:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_same';
jbe@0: 
jbe@69: CREATE OR REPLACE FUNCTION pgl_gist_distance(internal, internal, smallint, oid)
jbe@69:   RETURNS internal
jbe@69:   LANGUAGE C STRICT
jbe@69:   AS '$libdir/latlon-v0010', 'pgl_gist_distance';
jbe@69: 
jbe@73: 
jbe@73: ---------------------
jbe@73: -- alias functions --
jbe@73: ---------------------
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION distance(epoint, epoint)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $1 OPERATOR(@extschema@.<->) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION distance(ecluster, epoint)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE AS 'SELECT $1 OPERATOR(@extschema@.<->) $2';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION distance_within(epoint, epoint, float8)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.&&) @extschema@.ecircle($2, $3)';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION distance_within(ecluster, epoint, float8)
jbe@73:   RETURNS boolean
jbe@73:   LANGUAGE sql IMMUTABLE
jbe@73:   AS 'SELECT $1 OPERATOR(@extschema@.&&) @extschema@.ecircle($2, $3)';
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION fair_distance(ecluster, epoint, int4 = 10000)
jbe@73:   RETURNS float8
jbe@73:   LANGUAGE sql IMMUTABLE AS $$
jbe@73:     SELECT
jbe@73:     $1 OPERATOR(@extschema@.<=>) @extschema@.epoint_with_sample_count($2, $3)
jbe@73:   $$;
jbe@73: 
jbe@73: 
jbe@73: --------------------------------
jbe@73: -- other data storage formats --
jbe@73: --------------------------------
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION coords_to_epoint(float8, float8, text = 'epoint')
jbe@73:   RETURNS epoint
jbe@73:   LANGUAGE plpgsql IMMUTABLE STRICT AS $$
jbe@73:     DECLARE
jbe@73:       "result" @extschema@.epoint;
jbe@73:     BEGIN
jbe@73:       IF $3 OPERATOR(pg_catalog.=) 'epoint_lonlat' THEN
jbe@73:         -- avoid dynamic command execution for better performance
jbe@73:         RETURN @extschema@.epoint($2, $1);
jbe@73:       END IF;
jbe@73:       IF
jbe@73:         $3 OPERATOR(pg_catalog.=) 'epoint' OR
jbe@73:         $3 OPERATOR(pg_catalog.=) 'epoint_latlon'
jbe@73:       THEN
jbe@73:         -- avoid dynamic command execution for better performance
jbe@73:         RETURN @extschema@.epoint($1, $2);
jbe@73:       END IF;
jbe@73:       EXECUTE
jbe@73:         'SELECT ' OPERATOR(pg_catalog.||) $3 OPERATOR(pg_catalog.||) '($1, $2)'         INTO STRICT "result" USING $1, $2;
jbe@73:       RETURN "result";
jbe@73:     END;
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION GeoJSON_LinearRing_vertices(jsonb, text = 'epoint_lonlat')
jbe@73:   RETURNS SETOF jsonb
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT "result" FROM
jbe@73:       ( SELECT pg_catalog.jsonb_array_length($1) - 1 )
jbe@73:       AS "lastindex_row" ("lastindex")
jbe@73:       CROSS JOIN LATERAL pg_catalog.jsonb_array_elements(
jbe@73:         CASE WHEN
jbe@73:           @extschema@.coords_to_epoint(
jbe@73:             ($1 OPERATOR(pg_catalog.->) 0 OPERATOR(pg_catalog.->>) 0)
jbe@73:             ::pg_catalog.float8,
jbe@73:             ($1 OPERATOR(pg_catalog.->) 0 OPERATOR(pg_catalog.->>) 1)
jbe@73:             ::pg_catalog.float8,
jbe@73:             $2
jbe@73:           ) OPERATOR(pg_catalog.=) @extschema@.coords_to_epoint(
jbe@73:             ($1 OPERATOR(pg_catalog.->) "lastindex" OPERATOR(pg_catalog.->>) 0)
jbe@73:             ::pg_catalog.float8,
jbe@73:             ($1 OPERATOR(pg_catalog.->) "lastindex" OPERATOR(pg_catalog.->>) 1)
jbe@73:             ::pg_catalog.float8,
jbe@73:             $2
jbe@73:           )
jbe@73:         THEN
jbe@73:           $1 - "lastindex"
jbe@73:         ELSE
jbe@73:           $1
jbe@73:         END
jbe@73:       ) AS "result_row" ("result")
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION GeoJSON_to_epoint(jsonb, text = 'epoint_lonlat')
jbe@73:   RETURNS epoint
jbe@73:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@73:     SELECT CASE
jbe@73:     WHEN $1 OPERATOR(pg_catalog.->>) 'type' OPERATOR(pg_catalog.=) 'Point' THEN
jbe@73:       @extschema@.coords_to_epoint(
jbe@73:         ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 0)
jbe@73:         ::pg_catalog.float8,
jbe@73:         ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 1)
jbe@73:         ::pg_catalog.float8,
jbe@73:         $2
jbe@73:       )
jbe@73:     WHEN $1->>'type' = 'Feature' THEN
jbe@73:       @extschema@.GeoJSON_to_epoint($1 OPERATOR(pg_catalog.->) 'geometry', $2)
jbe@73:     ELSE
jbe@73:       NULL
jbe@73:     END
jbe@73:   $$;
jbe@73: 
jbe@73: CREATE OR REPLACE FUNCTION GeoJSON_to_ecluster(jsonb, text = 'epoint_lonlat')
jbe@73:   RETURNS ecluster
jbe@76:   LANGUAGE plpgsql IMMUTABLE STRICT AS $$
jbe@76:     DECLARE
jbe@76:       "tp" TEXT = $1 OPERATOR(pg_catalog.->>) 'type';
jbe@76:     BEGIN
jbe@76:       IF "tp" = 'Point' THEN RETURN
jbe@76:         @extschema@.coords_to_epoint(
jbe@76:           ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 0)
jbe@76:           ::pg_catalog.float8,
jbe@76:           ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 1)
jbe@76:           ::pg_catalog.float8,
jbe@76:           $2
jbe@76:         )::@extschema@.ecluster;
jbe@76:       END IF;
jbe@76:       raise notice 'DEBUG2';
jbe@76:       IF "tp" = 'MultiPoint' THEN RETURN
jbe@76:         ( SELECT @extschema@.ecluster_create_multipoint(pg_catalog.array_agg(
jbe@76:             @extschema@.coords_to_epoint(
jbe@76:               ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
jbe@76:               ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
jbe@76:               $2
jbe@76:             )
jbe@76:           ))
jbe@76:           FROM pg_catalog.jsonb_array_elements(
jbe@76:             $1 OPERATOR(pg_catalog.->) 'coordinates'
jbe@76:           ) AS "coord"
jbe@76:         );
jbe@76:       END IF;
jbe@76:       IF "tp" = 'LineString' THEN RETURN
jbe@76:         ( SELECT @extschema@.ecluster_create_path(pg_catalog.array_agg(
jbe@76:             @extschema@.coords_to_epoint(
jbe@76:               ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
jbe@76:               ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
jbe@76:               $2
jbe@76:             )
jbe@76:           ))
jbe@76:           FROM pg_catalog.jsonb_array_elements(
jbe@76:             $1 OPERATOR(pg_catalog.->) 'coordinates'
jbe@76:           ) AS "coord"
jbe@76:         );
jbe@76:       END IF;
jbe@76:       IF "tp" = 'MultiLineString' THEN RETURN
jbe@76:         ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
jbe@76:             ( SELECT @extschema@.ecluster_create_path(pg_catalog.array_agg(
jbe@76:                 @extschema@.coords_to_epoint(
jbe@76:                   ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
jbe@76:                   ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
jbe@76:                   $2
jbe@76:                 )
jbe@76:               ))
jbe@76:               FROM pg_catalog.jsonb_array_elements("coord_array") AS "coord"
jbe@76:             )
jbe@76:           ))
jbe@76:           FROM pg_catalog.jsonb_array_elements(
jbe@76:             $1 OPERATOR(pg_catalog.->) 'coordinates'
jbe@76:           ) AS "coord_array"
jbe@76:         );
jbe@76:       END IF;
jbe@76:       IF "tp" = 'Polygon' THEN RETURN
jbe@76:         ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
jbe@76:             ( SELECT @extschema@.ecluster_create_polygon(pg_catalog.array_agg(
jbe@76:                 @extschema@.coords_to_epoint(
jbe@76:                   ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
jbe@76:                   ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
jbe@76:                   $2
jbe@76:                 )
jbe@76:               ))
jbe@76:               FROM @extschema@.GeoJSON_LinearRing_vertices("coord_array", $2)
jbe@76:               AS "coord"
jbe@76:             )
jbe@76:           ))
jbe@76:           FROM pg_catalog.jsonb_array_elements(
jbe@76:             $1 OPERATOR(pg_catalog.->) 'coordinates'
jbe@76:           ) AS "coord_array"
jbe@76:         );
jbe@76:       END IF;
jbe@76:       IF "tp" = 'MultiPolygon' THEN RETURN
jbe@76:         ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
jbe@76:             ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
jbe@76:                 ( SELECT @extschema@.ecluster_create_polygon(
jbe@76:                     pg_catalog.array_agg(
jbe@76:                       @extschema@.coords_to_epoint(
jbe@76:                         ("coord" OPERATOR(pg_catalog.->>) 0)
jbe@76:                         ::pg_catalog.float8,
jbe@76:                         ("coord" OPERATOR(pg_catalog.->>) 1)
jbe@76:                         ::pg_catalog.float8,
jbe@76:                         $2
jbe@76:                       )
jbe@75:                     )
jbe@73:                   )
jbe@76:                   FROM @extschema@.GeoJSON_LinearRing_vertices(
jbe@76:                     "coord_array", $2
jbe@76:                   ) AS "coord"
jbe@75:                 )
jbe@76:               ))
jbe@76:               FROM pg_catalog.jsonb_array_elements("coord_array_array")
jbe@76:               AS "coord_array"
jbe@76:             )
jbe@76:           ))
jbe@76:           FROM jsonb_array_elements(
jbe@76:             $1 OPERATOR(pg_catalog.->) 'coordinates'
jbe@76:           ) AS "coord_array_array"
jbe@76:         );
jbe@76:       END IF;
jbe@76:       IF "tp" = 'GeometryCollection' THEN RETURN
jbe@76:         ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
jbe@76:             @extschema@.GeoJSON_to_ecluster("geometry", $2)
jbe@76:           ))
jbe@76:           FROM pg_catalog.jsonb_array_elements(
jbe@76:             $1 OPERATOR(pg_catalog.->) 'geometries'
jbe@76:           ) AS "geometry"
jbe@76:         );
jbe@76:       END IF;
jbe@76:       IF "tp" = 'Feature' THEN RETURN
jbe@76:         @extschema@.GeoJSON_to_ecluster(
jbe@76:           $1 OPERATOR(pg_catalog.->) 'geometry', $2
jbe@76:         );
jbe@76:       END IF;
jbe@76:       IF "tp" = 'FeatureCollection' THEN RETURN
jbe@76:         ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
jbe@76:             @extschema@.GeoJSON_to_ecluster("feature", $2)
jbe@76:           ))
jbe@76:           FROM pg_catalog.jsonb_array_elements(
jbe@76:             $1 OPERATOR(pg_catalog.->) 'features'
jbe@76:           ) AS "feature"
jbe@76:         );
jbe@76:       END IF;
jbe@76:       RETURN NULL;
jbe@76:     END;
jbe@73:   $$;
jbe@73: 




