
jbe@39: 
jbe@39: CREATE FUNCTION GeoJSON_LinearRing_vertices(jsonb, text = 'epoint_lonlat')
jbe@39:   RETURNS SETOF jsonb
jbe@39:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@39:     SELECT "result" FROM
jbe@39:       ( SELECT jsonb_array_length($1) - 1 ) AS "lastindex_row" ("lastindex")
jbe@39:       CROSS JOIN LATERAL jsonb_array_elements(
jbe@39:         CASE WHEN
jbe@39:           coords_to_epoint(
jbe@39:             ($1->0->>0)::float8,
jbe@39:             ($1->0->>1)::float8,
jbe@39:             $2
jbe@39:           ) = coords_to_epoint(
jbe@39:             ($1->"lastindex"->>0)::float8,
jbe@39:             ($1->"lastindex"->>1)::float8,
jbe@39:             $2
jbe@39:           )
jbe@39:         THEN
jbe@39:           $1 - "lastindex"
jbe@39:         ELSE
jbe@39:           $1
jbe@39:         END
jbe@39:       ) AS "result_row" ("result")
jbe@39:   $$;
jbe@39: 
jbe@39: -- NOTE: drop function to throw error if indices depend on it
jbe@39: --       (they need to be rebuilt)
jbe@39: DROP FUNCTION GeoJSON_to_ecluster(jsonb, text);
jbe@39: 
jbe@39: CREATE FUNCTION GeoJSON_to_ecluster(jsonb, text = 'epoint_lonlat')
jbe@39:   RETURNS ecluster
jbe@39:   LANGUAGE sql IMMUTABLE STRICT AS $$
jbe@39:     SELECT CASE $1->>'type'
jbe@39:     WHEN 'Point' THEN
jbe@39:       coords_to_epoint(
jbe@39:         ($1->'coordinates'->>0)::float8,
jbe@39:         ($1->'coordinates'->>1)::float8,
jbe@39:         $2
jbe@39:       )::ecluster
jbe@39:     WHEN 'MultiPoint' THEN
jbe@39:       ( SELECT ecluster_create_multipoint(array_agg(
jbe@39:           coords_to_epoint(
jbe@39:             ("coord"->>0)::float8,
jbe@39:             ("coord"->>1)::float8,
jbe@39:             $2
jbe@39:           )
jbe@39:         ))
jbe@39:         FROM jsonb_array_elements($1->'coordinates') AS "coord"
jbe@39:       )
jbe@39:     WHEN 'LineString' THEN
jbe@39:       ( SELECT ecluster_create_path(array_agg(
jbe@39:           coords_to_epoint(
jbe@39:             ("coord"->>0)::float8,
jbe@39:             ("coord"->>1)::float8,
jbe@39:             $2
jbe@39:           )
jbe@39:         ))
jbe@39:         FROM jsonb_array_elements($1->'coordinates') AS "coord"
jbe@39:       )
jbe@39:     WHEN 'MultiLineString' THEN
jbe@39:       ( SELECT ecluster_concat(array_agg(
jbe@39:           ( SELECT ecluster_create_path(array_agg(
jbe@39:               coords_to_epoint(
jbe@39:                 ("coord"->>0)::float8,
jbe@39:                 ("coord"->>1)::float8,
jbe@39:                 $2
jbe@39:               )
jbe@39:             ))
jbe@39:             FROM jsonb_array_elements("coord_array") AS "coord"
jbe@39:           )
jbe@39:         ))
jbe@39:         FROM jsonb_array_elements($1->'coordinates') AS "coord_array"
jbe@39:       )
jbe@39:     WHEN 'Polygon' THEN
jbe@39:       ( SELECT ecluster_concat(array_agg(
jbe@39:           ( SELECT ecluster_create_polygon(array_agg(
jbe@39:               coords_to_epoint(
jbe@39:                 ("coord"->>0)::float8,
jbe@39:                 ("coord"->>1)::float8,
jbe@39:                 $2
jbe@39:               )
jbe@39:             ))
jbe@39:             FROM GeoJSON_LinearRing_vertices("coord_array", $2) AS "coord"
jbe@39:           )
jbe@39:         ))
jbe@39:         FROM jsonb_array_elements($1->'coordinates') AS "coord_array"
jbe@39:       )
jbe@39:     WHEN 'MultiPolygon' THEN
jbe@39:       ( SELECT ecluster_concat(array_agg(
jbe@39:           ( SELECT ecluster_concat(array_agg(
jbe@39:               ( SELECT ecluster_create_polygon(array_agg(
jbe@39:                   coords_to_epoint(
jbe@39:                     ("coord"->>0)::float8,
jbe@39:                     ("coord"->>1)::float8,
jbe@39:                     $2
jbe@39:                   )
jbe@39:                 ))
jbe@39:                 FROM GeoJSON_LinearRing_vertices("coord_array", $2) AS "coord"
jbe@39:               )
jbe@39:             ))
jbe@39:             FROM jsonb_array_elements("coord_array_array") AS "coord_array"
jbe@39:           )
jbe@39:         ))
jbe@39:         FROM jsonb_array_elements($1->'coordinates') AS "coord_array_array"
jbe@39:       )
jbe@39:     WHEN 'GeometryCollection' THEN
jbe@39:       ( SELECT ecluster_concat(array_agg(
jbe@39:           GeoJSON_to_ecluster("geometry", $2)
jbe@39:         ))
jbe@39:         FROM jsonb_array_elements($1->'geometries') AS "geometry"
jbe@39:       )
jbe@39:     WHEN 'Feature' THEN
jbe@39:       GeoJSON_to_ecluster($1->'geometry', $2)
jbe@39:     WHEN 'FeatureCollection' THEN
jbe@39:       ( SELECT ecluster_concat(array_agg(
jbe@39:           GeoJSON_to_ecluster("feature", $2)
jbe@39:         ))
jbe@39:         FROM jsonb_array_elements($1->'features') AS "feature"
jbe@39:       )
jbe@39:     ELSE
jbe@39:       NULL
jbe@39:     END
jbe@39:   $$;
jbe@39: 




