PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ parse_geojson_polygon()

static LWGEOM* parse_geojson_polygon ( json_object *  geojson,
int *  hasz,
int  root_srid 
)
static

Definition at line 226 of file lwin_geojson.c.

References findMemberByName(), geojson_lwerror(), LW_FAILURE, lwalloc(), lwfree(), lwpoly_construct(), lwpoly_construct_empty(), parse_geojson_coord(), ptarray_construct_empty(), and ptarray_free().

Referenced by parse_geojson().

227 {
228  POINTARRAY **ppa = NULL;
229  json_object* rings = NULL;
230  json_object* points = NULL;
231  int i = 0, j = 0;
232  int nRings = 0, nPoints = 0;
233 
234  rings = findMemberByName( geojson, "coordinates" );
235  if ( ! rings )
236  {
237  geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4);
238  return NULL;
239  }
240 
241  if ( json_type_array != json_object_get_type(rings) )
242  {
243  geojson_lwerror("The 'coordinates' in GeoJSON are not an array", 4);
244  return NULL;
245  }
246 
247  nRings = json_object_array_length( rings );
248 
249  /* No rings => POLYGON EMPTY */
250  if ( ! nRings )
251  {
252  return (LWGEOM *)lwpoly_construct_empty(root_srid, 0, 0);
253  }
254 
255  for ( i = 0; i < nRings; i++ )
256  {
257  points = json_object_array_get_idx(rings, i);
258  if ( ! points || json_object_get_type(points) != json_type_array )
259  {
260  geojson_lwerror("The 'coordinates' in GeoJSON ring are not an array", 4);
261  return NULL;
262  }
263  nPoints = json_object_array_length(points);
264 
265  /* Skip empty rings */
266  if ( nPoints == 0 ) continue;
267 
268  if ( ! ppa )
269  ppa = (POINTARRAY**)lwalloc(sizeof(POINTARRAY*) * nRings);
270 
271  ppa[i] = ptarray_construct_empty(1, 0, 1);
272  for ( j = 0; j < nPoints; j++ )
273  {
274  json_object* coords = NULL;
275  coords = json_object_array_get_idx( points, j );
276  if (LW_FAILURE == parse_geojson_coord(coords, hasz, ppa[i]))
277  {
278  int k;
279  for (k = 0; k <= i; k++)
280  {
281  ptarray_free(ppa[k]);
282  }
283  lwfree(ppa);
284  geojson_lwerror("The 'coordinates' in GeoJSON polygon are not sufficiently nested", 4);
285  return NULL;
286  }
287  }
288  }
289 
290  /* All the rings were empty! */
291  if ( ! ppa )
292  return (LWGEOM *)lwpoly_construct_empty(root_srid, 0, 0);
293 
294  return (LWGEOM *) lwpoly_construct(root_srid, NULL, nRings, ppa);
295 }
static void geojson_lwerror(char *msg, __attribute__((__unused__)) int error_code)
Definition: lwin_geojson.c:55
void lwfree(void *mem)
Definition: lwutil.c:244
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:328
static int parse_geojson_coord(json_object *poObj, int *hasz, POINTARRAY *pa)
Definition: lwin_geojson.c:103
#define LW_FAILURE
Definition: liblwgeom.h:78
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
static json_object * findMemberByName(json_object *poObj, const char *pszName)
Definition: lwin_geojson.c:65
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:161
void * lwalloc(size_t size)
Definition: lwutil.c:229
Here is the call graph for this function:
Here is the caller graph for this function: