34 #include "access/gist.h"
35 #include "access/itup.h"
38 #include "utils/builtins.h"
39 #include "utils/elog.h"
41 #include "../postgis_config.h"
44 #include "lwgeom_pg.h"
101 PG_FREE_IF_COPY(geom,0);
102 PG_RETURN_INT32(srid);
110 int srid = PG_GETARG_INT32(1);
112 PG_RETURN_POINTER(g);
123 static int maxtyplen = 20;
126 text_ob = palloc0(VARHDRSZ + maxtyplen);
127 result = VARDATA(text_ob);
132 strcpy(result,
"POINT");
134 strcpy(result,
"MULTIPOINT");
136 strcpy(result,
"LINESTRING");
138 strcpy(result,
"CIRCULARSTRING");
140 strcpy(result,
"COMPOUNDCURVE");
142 strcpy(result,
"MULTILINESTRING");
144 strcpy(result,
"MULTICURVE");
146 strcpy(result,
"POLYGON");
148 strcpy(result,
"TRIANGLE");
150 strcpy(result,
"CURVEPOLYGON");
152 strcpy(result,
"MULTIPOLYGON");
154 strcpy(result,
"MULTISURFACE");
156 strcpy(result,
"GEOMETRYCOLLECTION");
158 strcpy(result,
"POLYHEDRALSURFACE");
160 strcpy(result,
"TIN");
162 strcpy(result,
"UNKNOWN");
167 SET_VARSIZE(text_ob, strlen(result) + VARHDRSZ);
169 PG_FREE_IF_COPY(gser, 0);
171 PG_RETURN_TEXT_P(text_ob);
181 # define type_str_len 31
195 type_text = cstring_to_text(type_str);
197 PG_FREE_IF_COPY(gser, 0);
198 PG_RETURN_TEXT_P(type_text);
219 PG_FREE_IF_COPY(geom, 0);
225 PG_RETURN_INT32(
count);
246 PG_FREE_IF_COPY(geom, 0);
247 PG_RETURN_INT32(ret);
261 POSTGIS_DEBUG(2,
"LWGEOM_geometryn_collection called.");
265 idx = PG_GETARG_INT32(1);
273 if ( idx == 0 ) PG_RETURN_POINTER(geom);
279 if ( idx < 0 ) PG_RETURN_NULL();
280 if ( idx >= (
int32) coll->
ngeoms ) PG_RETURN_NULL();
282 subgeom = coll->
geoms[idx];
291 PG_FREE_IF_COPY(geom, 0);
293 PG_RETURN_POINTER(result);
311 PG_FREE_IF_COPY(geom, 0);
315 elog(NOTICE,
"Could not compute geometry dimensions");
319 PG_RETURN_INT32(dimension);
339 POSTGIS_DEBUG(2,
"LWGEOM_exteriorring_polygon called.");
362 extring = poly->
rings[0];
386 if ( triangle->
bbox )
401 PG_FREE_IF_COPY(geom, 0);
402 PG_RETURN_POINTER(result);
434 result = poly->
nrings - 1;
438 PG_FREE_IF_COPY(geom, 0);
443 PG_RETURN_INT32(result);
466 POSTGIS_DEBUG(2,
"LWGEOM_interiorringn_polygon called.");
468 wanted_index = PG_GETARG_INT32(1);
469 if ( wanted_index < 1 )
474 geom = PG_GETARG_GSERIALIZED_P(0);
479 PG_FREE_IF_COPY(geom, 0);
487 PG_FREE_IF_COPY(geom, 0);
499 PG_FREE_IF_COPY(geom, 0);
503 ring = poly->
rings[wanted_index];
526 PG_FREE_IF_COPY(geom, 0);
535 PG_FREE_IF_COPY(geom, 0);
536 PG_RETURN_POINTER(result);
548 int where = PG_GETARG_INT32(1);
563 where = where +
count + 1;
580 PG_FREE_IF_COPY(geom, 0);
600 geom = PG_GETARG_GSERIALIZED_P(0);
603 lwpgerror(
"Argument to ST_X() must be a point");
613 PG_FREE_IF_COPY(geom, 0);
614 PG_RETURN_FLOAT8(p.
x);
629 geom = PG_GETARG_GSERIALIZED_P(0);
632 lwpgerror(
"Argument to ST_Y() must be a point");
642 PG_FREE_IF_COPY(geom, 0);
644 PG_RETURN_FLOAT8(p.
y);
660 geom = PG_GETARG_GSERIALIZED_P(0);
663 lwpgerror(
"Argument to ST_Z() must be a point");
676 PG_FREE_IF_COPY(geom, 0);
678 PG_RETURN_FLOAT8(p.
z);
693 geom = PG_GETARG_GSERIALIZED_P(0);
696 lwpgerror(
"Argument to ST_M() must be a point");
709 PG_FREE_IF_COPY(geom, 0);
711 PG_RETURN_FLOAT8(p.
m);
737 PG_FREE_IF_COPY(geom, 0);
769 PG_FREE_IF_COPY(geom, 0);
787 text *wkttext = PG_GETARG_TEXT_P(0);
793 POSTGIS_DEBUG(2,
"LWGEOM_from_text");
794 POSTGIS_DEBUGF(3,
"wkt: [%s]", wkt);
797 PG_PARSER_ERROR(lwg_parser_result);
799 lwgeom = lwg_parser_result.
geom;
803 elog(WARNING,
"OGC WKT expected, EWKT provided - use GeomFromEWKT() for this");
807 if ( PG_NARGS() > 1 )
813 PG_RETURN_POINTER(geom_result);
828 bytea *bytea_wkb = (bytea*)PG_GETARG_BYTEA_P(0);
841 PG_FREE_IF_COPY(bytea_wkb, 0);
845 elog(WARNING,
"OGC WKB expected, EWKB provided - use GeometryFromEWKB() for this");
848 if ( PG_NARGS() > 1 )
850 srid = PG_GETARG_INT32(1);
855 PG_RETURN_POINTER(geom);
867 int dbl_dig_for_wkt = DBL_DIG;
869 POSTGIS_DEBUG(2,
"Called.");
871 geom = PG_GETARG_GSERIALIZED_P(0);
874 if (PG_NARGS() > 1) dbl_dig_for_wkt = PG_GETARG_INT32(1);
879 POSTGIS_DEBUGF(3,
"WKT size = %u, WKT length = %u", (
unsigned int)wkt_size, (
unsigned int)strlen(wkt));
882 result = cstring_to_text(wkt);
886 PG_FREE_IF_COPY(geom, 0);
887 PG_RETURN_TEXT_P(result);
903 geom = PG_GETARG_GSERIALIZED_P(0);
907 if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
909 text *wkb_endian = PG_GETARG_TEXT_P(1);
911 if ( ! strncmp(VARDATA(wkb_endian),
"xdr", 3) ||
912 ! strncmp(VARDATA(wkb_endian),
"XDR", 3) )
927 result = palloc(wkb_size + VARHDRSZ);
928 memcpy(VARDATA(result), wkb, wkb_size);
929 SET_VARSIZE(result, wkb_size + VARHDRSZ);
933 PG_FREE_IF_COPY(geom, 0);
934 PG_RETURN_BYTEA_P(result);
953 PG_FREE_IF_COPY(geom, 0);
954 PG_RETURN_BOOL(closed);
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
int ptarray_calculate_gbox_cartesian(const POINTARRAY *pa, GBOX *gbox)
Calculate box (x/y) and add values to gbox.
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)...
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).
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
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.
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...
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
int lwgeom_is_closed(const LWGEOM *geom)
Return true or false depending on whether a geometry is a linear feature that closes on itself.
#define LW_PARSER_CHECK_ALL
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
void lwgeom_free(LWGEOM *geom)
LWPOINT * lwcompound_get_lwpoint(const LWCOMPOUND *lwcmp, uint32_t where)
void lwline_release(LWLINE *lwline)
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
LWLINE * lwline_construct_empty(int srid, char hasz, char hasm)
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
LWPOINT * lwcompound_get_startpoint(const LWCOMPOUND *lwcmp)
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
int getPoint3dz_p(const POINTARRAY *pa, uint32_t n, POINT3DZ *point)
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...
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
#define POLYHEDRALSURFACETYPE
LWCURVEPOLY * lwgeom_as_lwcurvepoly(const LWGEOM *lwgeom)
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
#define FLAGS_GET_M(flags)
void lwcollection_free(LWCOLLECTION *col)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
void * lwalloc(size_t size)
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
void lwpoly_free(LWPOLY *poly)
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
int getPoint3dm_p(const POINTARRAY *pa, uint32_t n, POINT3DM *point)
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
#define SRID_UNKNOWN
Unknown SRID value.
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...
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
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 lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
LWPOINT * lwcompound_get_endpoint(const LWCOMPOUND *lwcmp)
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
This library is the generic geometry handling section of PostGIS.
Datum LWGEOM_asBinary(PG_FUNCTION_ARGS)
Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(LWGEOM_get_srid)
Datum LWGEOM_get_srid(PG_FUNCTION_ARGS)
Datum LWGEOM_isclosed(PG_FUNCTION_ARGS)
Datum LWGEOM_asText(PG_FUNCTION_ARGS)
Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS)
Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
Datum geometry_geometrytype(PG_FUNCTION_ARGS)
Datum LWGEOM_x_point(PG_FUNCTION_ARGS)
Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
Datum LWGEOM_y_point(PG_FUNCTION_ARGS)
Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
Datum LWGEOM_set_srid(PG_FUNCTION_ARGS)
Datum LWGEOM_z_point(PG_FUNCTION_ARGS)
char * text_to_cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.