PostGIS  2.5.7dev-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.

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  if ( ! ppa )
266  ppa = (POINTARRAY**)lwalloc(sizeof(POINTARRAY*) * nRings);
267 
268  ppa[i] = ptarray_construct_empty(1, 0, 1);
269  for ( j = 0; j < nPoints; j++ )
270  {
271  json_object* coords = NULL;
272  coords = json_object_array_get_idx( points, j );
273  if (LW_FAILURE == parse_geojson_coord(coords, hasz, ppa[i]))
274  {
275  int k;
276  for (k = 0; k <= i; k++)
277  {
278  ptarray_free(ppa[k]);
279  }
280  lwfree(ppa);
281  geojson_lwerror("The 'coordinates' in GeoJSON polygon are not sufficiently nested", 4);
282  return NULL;
283  }
284  }
285  }
286 
287  /* All the rings were empty! */
288  if ( ! ppa )
289  return (LWGEOM *)lwpoly_construct_empty(root_srid, 0, 0);
290 
291  return (LWGEOM *) lwpoly_construct(root_srid, NULL, nRings, ppa);
292 }
#define LW_FAILURE
Definition: liblwgeom.h:79
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
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
void * lwalloc(size_t size)
Definition: lwutil.c:229
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:161
static int parse_geojson_coord(json_object *poObj, int *hasz, POINTARRAY *pa)
Definition: lwin_geojson.c:103
static void geojson_lwerror(char *msg, __attribute__((__unused__)) int error_code)
Definition: lwin_geojson.c:55
static json_object * findMemberByName(json_object *poObj, const char *pszName)
Definition: lwin_geojson.c:65

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().

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