PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum LWGEOM_in ( PG_FUNCTION_ARGS  )

Definition at line 60 of file lwgeom_inout.c.

References bytes_from_hexbytes(), struct_lwgeom_parser_result::geom, geometry_serialize(), LW_FAILURE, LW_PARSER_CHECK_ALL, LW_PARSER_CHECK_NONE, 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(), and postgis_valid_typmod().

Referenced by parse_WKT_lwgeom().

61 {
62  char *input = PG_GETARG_CSTRING(0);
63  int32 geom_typmod = -1;
64  char *str = input;
65  LWGEOM_PARSER_RESULT lwg_parser_result;
66  LWGEOM *lwgeom;
67  GSERIALIZED *ret;
68  int srid = 0;
69 
70  if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
71  geom_typmod = PG_GETARG_INT32(2);
72  }
73 
74  lwgeom_parser_result_init(&lwg_parser_result);
75 
76  /* Empty string. */
77  if ( str[0] == '\0' ) {
78  ereport(ERROR,(errmsg("parse error - invalid geometry")));
79  PG_RETURN_NULL();
80  }
81 
82  /* Starts with "SRID=" */
83  if( strncasecmp(str,"SRID=",5) == 0 )
84  {
85  /* Roll forward to semi-colon */
86  char *tmp = str;
87  while ( tmp && *tmp != ';' )
88  tmp++;
89 
90  /* Check next character to see if we have WKB */
91  if ( tmp && *(tmp+1) == '0' )
92  {
93  /* Null terminate the SRID= string */
94  *tmp = '\0';
95  /* Set str to the start of the real WKB */
96  str = tmp + 1;
97  /* Move tmp to the start of the numeric part */
98  tmp = input + 5;
99  /* Parse out the SRID number */
100  srid = atoi(tmp);
101  }
102  }
103 
104  /* WKB? Let's find out. */
105  if ( str[0] == '0' )
106  {
107  size_t hexsize = strlen(str);
108  unsigned char *wkb = bytes_from_hexbytes(str, hexsize);
109  /* TODO: 20101206: No parser checks! This is inline with current 1.5 behavior, but needs discussion */
110  lwgeom = lwgeom_from_wkb(wkb, hexsize/2, LW_PARSER_CHECK_NONE);
111  /* If we picked up an SRID at the head of the WKB set it manually */
112  if ( srid ) lwgeom_set_srid(lwgeom, srid);
113  /* Add a bbox if necessary */
114  if ( lwgeom_needs_bbox(lwgeom) ) lwgeom_add_bbox(lwgeom);
115  pfree(wkb);
116  ret = geometry_serialize(lwgeom);
117  lwgeom_free(lwgeom);
118  }
119  /* WKT then. */
120  else
121  {
122  if ( lwgeom_parse_wkt(&lwg_parser_result, str, LW_PARSER_CHECK_ALL) == LW_FAILURE )
123  {
124  PG_PARSER_ERROR(lwg_parser_result);
125  PG_RETURN_NULL();
126  }
127  lwgeom = lwg_parser_result.geom;
128  if ( lwgeom_needs_bbox(lwgeom) )
129  lwgeom_add_bbox(lwgeom);
130  ret = geometry_serialize(lwgeom);
131  lwgeom_parser_result_free(&lwg_parser_result);
132  }
133 
134  if ( geom_typmod >= 0 )
135  {
136  ret = postgis_valid_typmod(ret, geom_typmod);
137  POSTGIS_DEBUG(3, "typmod and geometry were consistent");
138  }
139  else
140  {
141  POSTGIS_DEBUG(3, "typmod was -1");
142  }
143 
144  /* Don't free the parser result (and hence lwgeom) until we have done */
145  /* the typemod check with lwgeom */
146 
147  PG_RETURN_POINTER(ret);
148 
149 }
unsigned int int32
Definition: shpopen.c:273
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...
uint8_t * bytes_from_hexbytes(const char *hexbuf, size_t hexsize)
Definition: lwin_wkb.c:72
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:871
#define LW_FAILURE
Definition: liblwgeom.h:64
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1869
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM...
Definition: liblwgeom.h:1876
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:865
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:599
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...
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:1870
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:754
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
Definition: lwgeom.c:1103

Here is the call graph for this function:

Here is the caller graph for this function: