34 #include "access/gist.h" 35 #include "access/itup.h" 38 #include "utils/elog.h" 40 #include "../postgis_config.h" 43 #include "lwgeom_pg.h" 100 PG_FREE_IF_COPY(geom,0);
101 PG_RETURN_INT32(srid);
109 int srid = PG_GETARG_INT32(1);
111 PG_RETURN_POINTER(g);
122 static int maxtyplen = 20;
125 text_ob = palloc0(VARHDRSZ + maxtyplen);
126 result = VARDATA(text_ob);
131 strcpy(result,
"POINT");
133 strcpy(result,
"MULTIPOINT");
135 strcpy(result,
"LINESTRING");
137 strcpy(result,
"CIRCULARSTRING");
139 strcpy(result,
"COMPOUNDCURVE");
141 strcpy(result,
"MULTILINESTRING");
143 strcpy(result,
"MULTICURVE");
145 strcpy(result,
"POLYGON");
147 strcpy(result,
"TRIANGLE");
149 strcpy(result,
"CURVEPOLYGON");
151 strcpy(result,
"MULTIPOLYGON");
153 strcpy(result,
"MULTISURFACE");
155 strcpy(result,
"GEOMETRYCOLLECTION");
157 strcpy(result,
"POLYHEDRALSURFACE");
159 strcpy(result,
"TIN");
161 strcpy(result,
"UNKNOWN");
166 SET_VARSIZE(text_ob, strlen(result) + VARHDRSZ);
168 PG_FREE_IF_COPY(gser, 0);
170 PG_RETURN_TEXT_P(text_ob);
180 # define type_str_len 31 194 type_text = cstring2text(type_str);
196 PG_FREE_IF_COPY(gser, 0);
197 PG_RETURN_TEXT_P(type_text);
218 PG_FREE_IF_COPY(geom, 0);
224 PG_RETURN_INT32(count);
245 PG_FREE_IF_COPY(geom, 0);
246 PG_RETURN_INT32(ret);
260 POSTGIS_DEBUG(2,
"LWGEOM_geometryn_collection called.");
264 idx = PG_GETARG_INT32(1);
272 if ( idx == 0 ) PG_RETURN_POINTER(geom);
278 if ( idx < 0 ) PG_RETURN_NULL();
279 if ( idx >= coll->
ngeoms ) PG_RETURN_NULL();
281 subgeom = coll->
geoms[idx];
290 PG_FREE_IF_COPY(geom, 0);
292 PG_RETURN_POINTER(result);
310 PG_FREE_IF_COPY(geom, 0);
314 elog(NOTICE,
"Could not compute geometry dimensions");
318 PG_RETURN_INT32(dimension);
338 POSTGIS_DEBUG(2,
"LWGEOM_exteriorring_polygon called.");
361 extring = poly->
rings[0];
385 if ( triangle->
bbox )
400 PG_FREE_IF_COPY(geom, 0);
401 PG_RETURN_POINTER(result);
433 result = poly->
nrings - 1;
437 PG_FREE_IF_COPY(geom, 0);
442 PG_RETURN_INT32(result);
465 POSTGIS_DEBUG(2,
"LWGEOM_interiorringn_polygon called.");
467 wanted_index = PG_GETARG_INT32(1);
468 if ( wanted_index < 1 )
473 geom = PG_GETARG_GSERIALIZED_P(0);
478 PG_FREE_IF_COPY(geom, 0);
486 PG_FREE_IF_COPY(geom, 0);
495 if ( wanted_index >= poly->
nrings )
498 PG_FREE_IF_COPY(geom, 0);
502 ring = poly->
rings[wanted_index];
523 if (wanted_index >= curvepoly->
nrings)
525 PG_FREE_IF_COPY(geom, 0);
534 PG_FREE_IF_COPY(geom, 0);
535 PG_RETURN_POINTER(result);
547 int where = PG_GETARG_INT32(1);
562 where = where + count + 1;
579 PG_FREE_IF_COPY(geom, 0);
599 geom = PG_GETARG_GSERIALIZED_P(0);
602 lwpgerror(
"Argument to ST_X() must be a point");
612 PG_FREE_IF_COPY(geom, 0);
613 PG_RETURN_FLOAT8(p.
x);
628 geom = PG_GETARG_GSERIALIZED_P(0);
631 lwpgerror(
"Argument to ST_Y() must be a point");
641 PG_FREE_IF_COPY(geom, 0);
643 PG_RETURN_FLOAT8(p.
y);
659 geom = PG_GETARG_GSERIALIZED_P(0);
662 lwpgerror(
"Argument to ST_Z() must be a point");
675 PG_FREE_IF_COPY(geom, 0);
677 PG_RETURN_FLOAT8(p.
z);
692 geom = PG_GETARG_GSERIALIZED_P(0);
695 lwpgerror(
"Argument to ST_M() must be a point");
708 PG_FREE_IF_COPY(geom, 0);
710 PG_RETURN_FLOAT8(p.
m);
736 PG_FREE_IF_COPY(geom, 0);
768 PG_FREE_IF_COPY(geom, 0);
786 text *wkttext = PG_GETARG_TEXT_P(0);
792 POSTGIS_DEBUG(2,
"LWGEOM_from_text");
793 POSTGIS_DEBUGF(3,
"wkt: [%s]", wkt);
796 PG_PARSER_ERROR(lwg_parser_result);
798 lwgeom = lwg_parser_result.
geom;
802 elog(WARNING,
"OGC WKT expected, EWKT provided - use GeomFromEWKT() for this");
806 if ( PG_NARGS() > 1 )
812 PG_RETURN_POINTER(geom_result);
827 bytea *bytea_wkb = (bytea*)PG_GETARG_BYTEA_P(0);
840 PG_FREE_IF_COPY(bytea_wkb, 0);
844 elog(WARNING,
"OGC WKB expected, EWKB provided - use GeometryFromEWKB() for this");
847 if ( PG_NARGS() > 1 )
849 srid = PG_GETARG_INT32(1);
854 PG_RETURN_POINTER(geom);
867 POSTGIS_DEBUG(2,
"Called.");
869 geom = PG_GETARG_GSERIALIZED_P(0);
875 POSTGIS_DEBUGF(3,
"WKT size = %u, WKT length = %u", (
unsigned int)wkt_size, (
unsigned int)strlen(wkt));
878 result = cstring2text(wkt);
882 PG_FREE_IF_COPY(geom, 0);
883 PG_RETURN_TEXT_P(result);
899 geom = PG_GETARG_GSERIALIZED_P(0);
903 if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
905 text *wkb_endian = PG_GETARG_TEXT_P(1);
907 if ( ! strncmp(VARDATA(wkb_endian),
"xdr", 3) ||
908 ! strncmp(VARDATA(wkb_endian),
"XDR", 3) )
923 result = palloc(wkb_size + VARHDRSZ);
924 memcpy(VARDATA(result), wkb, wkb_size);
925 SET_VARSIZE(result, wkb_size + VARHDRSZ);
929 PG_FREE_IF_COPY(geom, 0);
930 PG_RETURN_BYTEA_P(result);
949 PG_FREE_IF_COPY(geom, 0);
950 PG_RETURN_BOOL(closed);
int ptarray_calculate_gbox_cartesian(const POINTARRAY *pa, GBOX *gbox)
Calculate box (x/y) and add values to gbox.
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
int lwgeom_is_closed(const LWGEOM *geom)
Return true or false depending on whether a geometry is a linear feature that closes on itself...
LWLINE * lwline_construct_empty(int srid, char hasz, char hasm)
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Datum LWGEOM_asBinary(PG_FUNCTION_ARGS)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
void lwgeom_free(LWGEOM *geom)
Datum LWGEOM_y_point(PG_FUNCTION_ARGS)
LWPOINT * lwcompound_get_startpoint(const LWCOMPOUND *lwcmp)
LWCURVEPOLY * lwgeom_as_lwcurvepoly(const LWGEOM *lwgeom)
void gserialized_set_srid(GSERIALIZED *s, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function)...
#define POLYHEDRALSURFACETYPE
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
int getPoint3dm_p(const POINTARRAY *pa, int n, POINT3DM *point)
Datum LWGEOM_z_point(PG_FUNCTION_ARGS)
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS)
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
PG_FUNCTION_INFO_V1(LWGEOM_get_srid)
Datum LWGEOM_get_srid(PG_FUNCTION_ARGS)
Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
Datum LWGEOM_asText(PG_FUNCTION_ARGS)
Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
int getPoint3dz_p(const POINTARRAY *pa, int n, POINT3DZ *point)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
void lwpoly_free(LWPOLY *poly)
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM...
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
LWPOINT * lwcompound_get_endpoint(const LWCOMPOUND *lwcmp)
#define SRID_UNKNOWN
Unknown SRID value.
Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
char * text2cstring(const text *textptr)
Datum LWGEOM_x_point(PG_FUNCTION_ARGS)
Datum LWGEOM_set_srid(PG_FUNCTION_ARGS)
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Datum geometry_geometrytype(PG_FUNCTION_ARGS)
Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
Datum LWGEOM_isclosed(PG_FUNCTION_ARGS)
Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
int lwgeom_dimension(const LWGEOM *geom)
For an LWGEOM, returns 0 for points, 1 for lines, 2 for polygons, 3 for volume, and the max dimension...
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
#define FLAGS_GET_M(flags)
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
LWPOINT * lwline_get_lwpoint(const LWLINE *line, int where)
Returns freshly allocated LWPOINT that corresponds to the index where.
void lwcollection_free(LWCOLLECTION *col)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
void lwgeom_set_srid(LWGEOM *geom, int srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
void * lwalloc(size_t size)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
#define LW_PARSER_CHECK_ALL
Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
int lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
LWPOINT * lwcompound_get_lwpoint(const LWCOMPOUND *lwcmp, int where)
Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
void lwline_release(LWLINE *lwline)
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
This library is the generic geometry handling section of PostGIS.
uint32_t gserialized_max_header_size(void)
Returns the size in bytes to read from toast to get the basic information from a geometry: GSERIALIZE...