From e84cd533b1d1ec056f17cd6117de1ee93e6157a4 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 24 Aug 2004 15:50:04 +0000 Subject: [PATCH] PointN() ported. git-svn-id: http://svn.osgeo.org/postgis/trunk@740 b70326c6-7e19-0410-871a-916f4a2858ee --- lwgeom/MISSING_OBJECTS | 1 - lwgeom/lwgeom_ogc.c | 62 +++++++++++++++++++++++++++++++++++++++++ lwgeom/lwpostgis.sql.in | 17 ++++++++--- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lwgeom/MISSING_OBJECTS b/lwgeom/MISSING_OBJECTS index 661a298f2..3572c5af8 100644 --- a/lwgeom/MISSING_OBJECTS +++ b/lwgeom/MISSING_OBJECTS @@ -28,7 +28,6 @@ FUNC: KEEPING FUNCTION: [envelope(geometry)] FUNC: KEEPING FUNCTION: [x(geometry)] FUNC: KEEPING FUNCTION: [y(geometry)] FUNC: KEEPING FUNCTION: [z(geometry)] -FUNC: KEEPING FUNCTION: [pointn(geometry, integer)] FUNC: KEEPING FUNCTION: [max_distance(geometry, geometry)] FUNC: KEEPING FUNCTION: [optimistic_overlap(geometry, geometry, double precision)] FUNC: KEEPING FUNCTION: [segmentize(geometry, double precision)] diff --git a/lwgeom/lwgeom_ogc.c b/lwgeom/lwgeom_ogc.c index 87954e9f4..818957937 100644 --- a/lwgeom/lwgeom_ogc.c +++ b/lwgeom/lwgeom_ogc.c @@ -41,6 +41,8 @@ Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS); Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS); // ---- NumInteriorRings(geometry) Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS); +// ---- PointN(geometry, integer) +Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS); // internal @@ -387,6 +389,7 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS) pfree_inspected(inspected); PG_RETURN_NULL(); } + pfree_inspected(inspected); ring = poly->rings[wanted_index+1]; @@ -406,3 +409,62 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS) PG_RETURN_POINTER(result); } +// PointN(GEOMETRY,INTEGER) -- find the first linestring in GEOMETRY, +// return the point at index INTEGER (0 is 1st point). Return NULL if +// there is no LINESTRING(..) in GEOMETRY or INTEGER is out of bounds. +PG_FUNCTION_INFO_V1(LWGEOM_pointn_linestring); +Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS) +{ + LWGEOM *geom; + int32 wanted_index; + LWGEOM_INSPECTED *inspected; + LWLINE *line = NULL; + POINTARRAY *pts; + LWPOINT *point; + char *serializedpoint; + LWGEOM *result; + int i; + + wanted_index = PG_GETARG_INT32(1); + if ( wanted_index < 0 ) + PG_RETURN_NULL(); // index out of range + + geom = (LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + inspected = lwgeom_inspect(SERIALIZED_FORM(geom)); + + for (i=0; ingeometries; i++) + { + line = lwgeom_getline_inspected(inspected, i); + if ( line ) break; + } + + if ( line == NULL ) PG_RETURN_NULL(); + + // Ok, now we have a line. Let's see if it has enough points. + if ( wanted_index > line->points->npoints-1 ) + { + pfree_inspected(inspected); + PG_RETURN_NULL(); + } + pfree_inspected(inspected); + + // Construct a point array + pts = pointArray_construct(getPoint(line->points, wanted_index), + line->points->ndims, 1); + + // Construct an LWPOINT + point = lwpoint_construct(line->points->ndims, lwgeom_getSRID(geom), + pts); + + // Serialized the point + serializedpoint = lwpoint_serialize(point); + + // And we construct the line (copy again) + result = LWGEOM_construct(serializedpoint, lwgeom_getSRID(geom), + lwgeom_hasBBOX(geom->type)); + + pfree(point); + pfree(serializedpoint); + + PG_RETURN_POINTER(result); +} diff --git a/lwgeom/lwpostgis.sql.in b/lwgeom/lwpostgis.sql.in index a72e77e76..2092253f6 100644 --- a/lwgeom/lwpostgis.sql.in +++ b/lwgeom/lwpostgis.sql.in @@ -11,6 +11,9 @@ -- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- $Log$ +-- Revision 1.13 2004/08/24 15:50:04 strk +-- PointN() ported. +-- -- Revision 1.12 2004/08/24 15:05:34 strk -- Added NumInteriorRings() and InteriorRingN() -- @@ -885,14 +888,20 @@ CREATEFUNCTION InteriorRingN(geometry,integer) AS '@MODULE_FILENAME@','LWGEOM_interiorringn_polygon' LANGUAGE 'C' WITH (isstrict); - ------------------------------------------------------------------------- - -CREATEFUNCTION geometrytype(geometry) +CREATEFUNCTION GeometryType(geometry) RETURNS text AS '@MODULE_FILENAME@', 'LWGEOM_getTYPE' LANGUAGE 'C' WITH (isstrict); +CREATEFUNCTION PointN(geometry,integer) + RETURNS geometry + AS '@MODULE_FILENAME@','LWGEOM_pointn_linestring' + LANGUAGE 'C' WITH (isstrict); + + + +------------------------------------------------------------------------ + -- -- Aggregate functions -- -- 2.40.0