PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ parse_geojson_polygon()

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

Definition at line 206 of file lwin_geojson.c.

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

Referenced by parse_geojson().

207 {
208  POINTARRAY **ppa = NULL;
209  json_object* rings = NULL;
210  json_object* points = NULL;
211  int i = 0, j = 0;
212  int nRings = 0, nPoints = 0;
213 
214  rings = findMemberByName( geojson, "coordinates" );
215  if ( ! rings )
216  {
217  geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4);
218  return NULL;
219  }
220 
221  if ( json_type_array != json_object_get_type(rings) )
222  {
223  geojson_lwerror("The 'coordinates' in GeoJSON are not an array", 4);
224  return NULL;
225  }
226 
227  nRings = json_object_array_length( rings );
228 
229  /* No rings => POLYGON EMPTY */
230  if ( ! nRings )
231  {
232  return (LWGEOM *)lwpoly_construct_empty(root_srid, 0, 0);
233  }
234 
235  for ( i = 0; i < nRings; i++ )
236  {
237  points = json_object_array_get_idx(rings, i);
238  if ( ! points || json_object_get_type(points) != json_type_array )
239  {
240  geojson_lwerror("The 'coordinates' in GeoJSON ring are not an array", 4);
241  return NULL;
242  }
243  nPoints = json_object_array_length(points);
244 
245  /* Skip empty rings */
246  if ( nPoints == 0 ) continue;
247 
248  if ( ! ppa )
249  ppa = (POINTARRAY**)lwalloc(sizeof(POINTARRAY*) * nRings);
250 
251  ppa[i] = ptarray_construct_empty(1, 0, 1);
252  for ( j = 0; j < nPoints; j++ )
253  {
254  json_object* coords = NULL;
255  coords = json_object_array_get_idx( points, j );
256  parse_geojson_coord(coords, hasz, ppa[i]);
257  }
258  }
259 
260  /* All the rings were empty! */
261  if ( ! ppa )
262  return (LWGEOM *)lwpoly_construct_empty(root_srid, 0, 0);
263 
264  return (LWGEOM *) lwpoly_construct(root_srid, NULL, nRings, ppa);
265 }
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
static void geojson_lwerror(char *msg, int error_code)
Definition: lwin_geojson.c:35
static int parse_geojson_coord(json_object *poObj, int *hasz, POINTARRAY *pa)
Definition: lwin_geojson.c:83
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
static json_object * findMemberByName(json_object *poObj, const char *pszName)
Definition: lwin_geojson.c:45
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:66
void * lwalloc(size_t size)
Definition: lwutil.c:199
Here is the call graph for this function:
Here is the caller graph for this function: