33 #include "access/gist.h"
34 #include "access/itup.h"
37 #include "utils/builtins.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 int32_t 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);
179 "ST_MultiLineString",
181 "ST_GeometryCollection",
187 "ST_PolyhedralSurface",
204 PG_FREE_IF_COPY(gser, 0);
205 PG_RETURN_TEXT_P(type_text);
226 PG_FREE_IF_COPY(geom, 0);
232 PG_RETURN_INT32(
count);
253 PG_FREE_IF_COPY(geom, 0);
254 PG_RETURN_INT32(ret);
268 POSTGIS_DEBUG(2,
"LWGEOM_geometryn_collection called.");
272 idx = PG_GETARG_INT32(1);
280 if ( idx == 0 ) PG_RETURN_POINTER(geom);
286 if ( idx < 0 ) PG_RETURN_NULL();
287 if ( idx >= (
int32) coll->
ngeoms ) PG_RETURN_NULL();
289 subgeom = coll->
geoms[idx];
298 PG_FREE_IF_COPY(geom, 0);
300 PG_RETURN_POINTER(result);
318 PG_FREE_IF_COPY(geom, 0);
322 elog(NOTICE,
"Could not compute geometry dimensions");
326 PG_RETURN_INT32(dimension);
346 POSTGIS_DEBUG(2,
"LWGEOM_exteriorring_polygon called.");
369 extring = poly->
rings[0];
393 if ( triangle->
bbox )
408 PG_FREE_IF_COPY(geom, 0);
409 PG_RETURN_POINTER(result);
441 result = poly->
nrings - 1;
445 PG_FREE_IF_COPY(geom, 0);
450 PG_RETURN_INT32(result);
473 POSTGIS_DEBUG(2,
"LWGEOM_interiorringn_polygon called.");
475 wanted_index = PG_GETARG_INT32(1);
476 if ( wanted_index < 1 )
481 geom = PG_GETARG_GSERIALIZED_P(0);
486 PG_FREE_IF_COPY(geom, 0);
494 PG_FREE_IF_COPY(geom, 0);
506 PG_FREE_IF_COPY(geom, 0);
510 ring = poly->
rings[wanted_index];
533 PG_FREE_IF_COPY(geom, 0);
542 PG_FREE_IF_COPY(geom, 0);
543 PG_RETURN_POINTER(result);
555 int where = PG_GETARG_INT32(1);
570 where = where +
count + 1;
587 PG_FREE_IF_COPY(geom, 0);
606 lwpgerror(
"Argument to ST_X() must have type POINT");
612 PG_RETURN_FLOAT8(pt.
x);
626 lwpgerror(
"Argument to ST_Y() must have type POINT");
632 PG_RETURN_FLOAT8(pt.
y);
647 lwpgerror(
"Argument to ST_Z() must have type POINT");
653 PG_RETURN_FLOAT8(pt.
z);
667 lwpgerror(
"Argument to ST_M() must have type POINT");
673 PG_RETURN_FLOAT8(pt.
m);
699 PG_FREE_IF_COPY(geom, 0);
731 PG_FREE_IF_COPY(geom, 0);
749 text *wkttext = PG_GETARG_TEXT_P(0);
755 POSTGIS_DEBUG(2,
"LWGEOM_from_text");
756 POSTGIS_DEBUGF(3,
"wkt: [%s]", wkt);
759 PG_PARSER_ERROR(lwg_parser_result);
761 lwgeom = lwg_parser_result.
geom;
765 elog(WARNING,
"OGC WKT expected, EWKT provided - use GeomFromEWKT() for this");
769 if ( PG_NARGS() > 1 )
775 PG_RETURN_POINTER(geom_result);
790 bytea *bytea_wkb = PG_GETARG_BYTEA_P(0);
794 uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb);
798 lwpgerror(
"Unable to parse WKB");
802 PG_FREE_IF_COPY(bytea_wkb, 0);
806 elog(WARNING,
"OGC WKB expected, EWKB provided - use GeometryFromEWKB() for this");
809 if ( PG_NARGS() > 1 )
811 srid = PG_GETARG_INT32(1);
816 PG_RETURN_POINTER(geom);
828 int dbl_dig_for_wkt = DBL_DIG;
830 POSTGIS_DEBUG(2,
"Called.");
832 geom = PG_GETARG_GSERIALIZED_P(0);
835 if (PG_NARGS() > 1) dbl_dig_for_wkt = PG_GETARG_INT32(1);
840 POSTGIS_DEBUGF(3,
"WKT size = %u, WKT length = %u", (
unsigned int)wkt_size, (
unsigned int)strlen(wkt));
843 result = cstring_to_text(wkt);
847 PG_FREE_IF_COPY(geom, 0);
848 PG_RETURN_TEXT_P(result);
867 geom = PG_GETARG_GSERIALIZED_P(0);
872 if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
874 text *wkb_endian = PG_GETARG_TEXT_P(1);
876 if ( ! strncmp(VARDATA(wkb_endian),
"xdr", 3) ||
877 ! strncmp(VARDATA(wkb_endian),
"XDR", 3) )
892 result = palloc(wkb_size + VARHDRSZ);
893 memcpy(VARDATA(result), wkb, wkb_size);
894 SET_VARSIZE(result, wkb_size + VARHDRSZ);
898 PG_FREE_IF_COPY(geom, 0);
899 PG_RETURN_BYTEA_P(result);
918 PG_FREE_IF_COPY(geom, 0);
919 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 *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
int gserialized_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
int gserialized_peek_first_point(const GSERIALIZED *g, POINT4D *out_point)
Pull the first point values of a GSERIALIZED.
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void gserialized_set_srid(GSERIALIZED *g, 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 *g)
Check if a GSERIALIZED has a Z ordinate.
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
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...
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_set_srid(LWGEOM *geom, int32_t srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
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.
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)
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
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 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)
void lwcollection_free(LWCOLLECTION *col)
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)
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.
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
LWLINE * lwline_construct_empty(int32_t srid, char hasz, char hasm)
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)
static char * stTypeName[]
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)
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
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.