PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ LWGEOM_in()

Datum LWGEOM_in ( PG_FUNCTION_ARGS  )

Definition at line 84 of file lwgeom_inout.c.

85{
86 char *input = PG_GETARG_CSTRING(0);
87 int32 geom_typmod = -1;
88 char *str = input;
89 LWGEOM_PARSER_RESULT lwg_parser_result;
90 LWGEOM *lwgeom;
91 GSERIALIZED *ret;
92 int32_t srid = 0;
93
94 if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
95 geom_typmod = PG_GETARG_INT32(2);
96 }
97
98 lwgeom_parser_result_init(&lwg_parser_result);
99
100 /* Empty string. */
101 if ( str[0] == '\0' ) {
102 ereport(ERROR,(errmsg("parse error - invalid geometry")));
103 PG_RETURN_NULL();
104 }
105
106 /* Starts with "SRID=" */
107 if( strncasecmp(str,"SRID=",5) == 0 )
108 {
109 /* Roll forward to semi-colon */
110 char *tmp = str;
111 while ( tmp && *tmp != ';' )
112 tmp++;
113
114 /* Check next character to see if we have WKB */
115 if ( tmp && *(tmp+1) == '0' )
116 {
117 /* Null terminate the SRID= string */
118 *tmp = '\0';
119 /* Set str to the start of the real WKB */
120 str = tmp + 1;
121 /* Move tmp to the start of the numeric part */
122 tmp = input + 5;
123 /* Parse out the SRID number */
124 srid = atoi(tmp);
125 }
126 }
127
128 /* WKB? Let's find out. */
129 if ( str[0] == '0' )
130 {
131 size_t hexsize = strlen(str);
132 unsigned char *wkb = bytes_from_hexbytes(str, hexsize);
133 /* TODO: 20101206: No parser checks! This is inline with current 1.5 behavior, but needs discussion */
134 lwgeom = lwgeom_from_wkb(wkb, hexsize/2, LW_PARSER_CHECK_NONE);
135 /* Parser should throw error, but if not, catch here. */
136 if ( !lwgeom ) PG_RETURN_NULL();
137 /* If we picked up an SRID at the head of the WKB set it manually */
138 if ( srid ) lwgeom_set_srid(lwgeom, srid);
139 /* Add a bbox if necessary */
140 if ( lwgeom_needs_bbox(lwgeom) ) lwgeom_add_bbox(lwgeom);
141 lwfree(wkb);
142 ret = geometry_serialize(lwgeom);
143 lwgeom_free(lwgeom);
144 }
145 /* WKT then. */
146 else
147 {
148 if ( lwgeom_parse_wkt(&lwg_parser_result, str, LW_PARSER_CHECK_ALL) == LW_FAILURE )
149 {
150 PG_PARSER_ERROR(lwg_parser_result);
151 PG_RETURN_NULL();
152 }
153 lwgeom = lwg_parser_result.geom;
154 if ( lwgeom_needs_bbox(lwgeom) )
155 lwgeom_add_bbox(lwgeom);
156 ret = geometry_serialize(lwgeom);
157 lwgeom_parser_result_free(&lwg_parser_result);
158 }
159
160 if ( geom_typmod >= 0 )
161 {
162 ret = postgis_valid_typmod(ret, geom_typmod);
163 POSTGIS_DEBUG(3, "typmod and geometry were consistent");
164 }
165 else
166 {
167 POSTGIS_DEBUG(3, "typmod was -1");
168 }
169
170 /* Don't free the parser result (and hence lwgeom) until we have done */
171 /* the typemod check with lwgeom */
172
173 PG_RETURN_POINTER(ret);
174
175}
GSERIALIZED * postgis_valid_typmod(GSERIALIZED *gser, int32_t typmod)
Check the consistency of the metadata we want to enforce in the typmod: srid, type and dimensionality...
#define LW_PARSER_CHECK_ALL
Definition liblwgeom.h:2061
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...
Definition lwgeom.c:1530
#define LW_FAILURE
Definition liblwgeom.h:110
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1138
#define LW_PARSER_CHECK_NONE
Definition liblwgeom.h:2060
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
Definition lwin_wkt.c:880
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
Definition lwgeom.c:1191
uint8_t * bytes_from_hexbytes(const char *hexbuf, size_t hexsize)
Definition lwin_wkb.c:92
void lwfree(void *mem)
Definition lwutil.c:242
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...
Definition lwin_wkb.c:833
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition lwgeom.c:677
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition lwin_wkt.c:886
#define str(s)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition shpopen.c:273
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.
Definition liblwgeom.h:2068

References bytes_from_hexbytes(), struct_lwgeom_parser_result::geom, geometry_serialize(), LW_FAILURE, LW_PARSER_CHECK_ALL, LW_PARSER_CHECK_NONE, lwfree(), lwgeom_add_bbox(), lwgeom_free(), lwgeom_from_wkb(), lwgeom_needs_bbox(), lwgeom_parse_wkt(), lwgeom_parser_result_free(), lwgeom_parser_result_init(), lwgeom_set_srid(), postgis_valid_typmod(), and str.

Referenced by parse_WKT_lwgeom().

Here is the call graph for this function:
Here is the caller graph for this function: