pggeodb.nancy.inra.fr/db_cefs - db_cefs on pggeodb.nancy.inra.fr
Previous topic Chapter index Next topic

Function: _st_concavehull

 

 

Schema

public

 

Owner

postgres

 

Descriptions

There is no description for function _st_concavehull

 

Options

Option

Value

Returns

public.geometry

Language

plpgsql

Parameters

param_inputgeom public.geometry

 

Definition

CREATE OR REPLACE FUNCTION public._st_concavehull (
 param_inputgeom public.geometry
)
RETURNS public.geometry AS
$span$
DECLARE
    
vexhull GEOMETRY;
var_resultgeom geometry;
var_inputgeom geometry;
vexring GEOMETRY;
cavering GEOMETRY;
cavept geometry[];
seglength double precision;
var_tempgeom geometry;
scale_factor integer := 1;
i integer;

BEGIN

-- First compute the ConvexHull of the geometry
vexhull := ST_ConvexHull(param_inputgeom);
var_inputgeom := param_inputgeom;
--A point really has no concave hull
IF ST_GeometryType(vexhull) = 'ST_Point' OR ST_GeometryType(vexHull) = 'ST_LineString' THEN
RETURN
vexhull;
END IF;

-- convert the hull perimeter to a linestring so we can manipulate individual points
vexring := CASE WHEN ST_GeometryType(vexhull) = 'ST_LineString' THEN vexhull ELSE ST_ExteriorRing(vexhull) END;
IF abs(ST_X(ST_PointN(vexring,1))) < 1 THEN --scale the geometry to prevent stupid precision errors - not sure it works so make low for now
scale_factor := 100;
vexring := ST_Scale(vexring, scale_factor,scale_factor);
var_inputgeom := ST_Scale(var_inputgeom, scale_factor, scale_factor);
--RAISE NOTICE 'Scaling';
END IF;
seglength := ST_Length(vexring)/least(ST_NPoints(vexring)*2,1000) ;

vexring := ST_Segmentize(vexring, seglength);
-- find the point on the original geom that is closest to each point of the convex hull and make a new linestring out of it.
cavering := ST_Collect(
ARRAY(

SELECT
ST_ClosestPoint(var_inputgeom, pt ) As the_geom
FROM (
SELECT  ST_PointN(vexring, n ) As pt, n
FROM
generate_series(1, ST_NPoints(vexring) ) As n
) As pt

)
)
;


var_resultgeom := ST_MakeLine(geom)
FROM ST_Dump(cavering) As foo;

IF ST_IsSimple(var_resultgeom) THEN
var_resultgeom := ST_MakePolygon(var_resultgeom);
--RAISE NOTICE 'is Simple: %', var_resultgeom;
ELSE
--RAISE NOTICE 'is not Simple: %', var_resultgeom;
var_resultgeom := ST_ConvexHull(var_resultgeom);
END IF;

IF scale_factor > 1 THEN -- scale the result back
var_resultgeom := ST_Scale(var_resultgeom, 1/scale_factor, 1/scale_factor);
END IF;
RETURN var_resultgeom;

END;
$span$
LANGUAGE
'plpgsql'
IMMUTABLE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST
100;

This file was generated with SQL Manager for PostgreSQL (www.pgsqlmanager.com) at 13/03/2014 13:23
Previous topic Chapter index Next topic