static LWGEOM* parse_geojson_polygon(json_object *geojson, bool *hasz, int *root_srid) { LWGEOM *geom; POINTARRAY **ppa; json_object* rings = NULL; int i = 0, j = 0; int ring = 0; rings = findMemberByName( geojson, "coordinates" ); if ( ! rings ) geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); ppa = (POINTARRAY**) lwalloc(sizeof(POINTARRAY*)); if( json_type_array == json_object_get_type( rings ) ) { int nPoints; json_object* points = NULL; ppa[0] = ptarray_construct_empty(1, 0, 1); ring = json_object_array_length( rings ); points = json_object_array_get_idx( rings, 0 ); nPoints = json_object_array_length( points ); for (i=0; i < nPoints; i++ ) { json_object* coords = NULL; coords = json_object_array_get_idx( points, i ); parse_geojson_coord(coords, hasz, ppa[0]); } for(i = 1; i < ring; ++i) { int nPoints; ppa = (POINTARRAY**) lwrealloc((POINTARRAY *) ppa, sizeof(POINTARRAY*) * (i + 1)); ppa[i] = ptarray_construct_empty(1, 0, 1); points = json_object_array_get_idx( rings, i ); nPoints = json_object_array_length( points ); for (j=0; j < nPoints; j++ ) { json_object* coords = NULL; coords = json_object_array_get_idx( points, j ); parse_geojson_coord(coords, hasz, ppa[i]); } } } geom = (LWGEOM *) lwpoly_construct(*root_srid, NULL, ring, ppa); return geom; }
static LWGEOM* parse_geojson_linestring(json_object *geojson, bool *hasz, int *root_srid) { LWGEOM *geom; POINTARRAY *pa; json_object* points = NULL; int i = 0; POSTGIS_DEBUG(2, "parse_geojson_linestring called."); points = findMemberByName( geojson, "coordinates" ); if ( ! points ) geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); pa = ptarray_construct_empty(1, 0, 1); if( json_type_array == json_object_get_type( points ) ) { const int nPoints = json_object_array_length( points ); for(i = 0; i < nPoints; ++i) { json_object* coords = NULL; coords = json_object_array_get_idx( points, i ); parse_geojson_coord(coords, hasz, pa); } } geom = (LWGEOM *) lwline_construct(*root_srid, NULL, pa); POSTGIS_DEBUG(2, "parse_geojson_linestring finished."); return geom; }
static LWGEOM* parse_geojson_multilinestring(json_object *geojson, int *hasz, int root_srid) { LWGEOM *geom = NULL; int i, j; json_object* poObjLines = NULL; if (!root_srid) { geom = (LWGEOM *)lwcollection_construct_empty(MULTILINETYPE, root_srid, 1, 0); } else { geom = (LWGEOM *)lwcollection_construct_empty(MULTILINETYPE, -1, 1, 0); } poObjLines = findMemberByName( geojson, "coordinates" ); if ( ! poObjLines ) { geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); return NULL; } if( json_type_array == json_object_get_type( poObjLines ) ) { const int nLines = json_object_array_length( poObjLines ); for( i = 0; i < nLines; ++i) { POINTARRAY *pa = NULL; json_object* poObjLine = NULL; poObjLine = json_object_array_get_idx( poObjLines, i ); pa = ptarray_construct_empty(1, 0, 1); if( json_type_array == json_object_get_type( poObjLine ) ) { const int nPoints = json_object_array_length( poObjLine ); for(j = 0; j < nPoints; ++j) { json_object* coords = NULL; coords = json_object_array_get_idx( poObjLine, j ); parse_geojson_coord(coords, hasz, pa); } geom = (LWGEOM*)lwmline_add_lwline((LWMLINE*)geom, (LWLINE*)lwline_construct(root_srid, NULL, pa)); } } } return geom; }
static LWGEOM* parse_geojson_multipoint(json_object *geojson, int *hasz, int root_srid) { LWGEOM *geom; int i = 0; json_object* poObjPoints = NULL; if (!root_srid) { geom = (LWGEOM *)lwcollection_construct_empty(MULTIPOINTTYPE, root_srid, 1, 0); } else { geom = (LWGEOM *)lwcollection_construct_empty(MULTIPOINTTYPE, -1, 1, 0); } poObjPoints = findMemberByName( geojson, "coordinates" ); if ( ! poObjPoints ) { geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); return NULL; } if( json_type_array == json_object_get_type( poObjPoints ) ) { const int nPoints = json_object_array_length( poObjPoints ); for( i = 0; i < nPoints; ++i) { POINTARRAY *pa; json_object* poObjCoords = NULL; poObjCoords = json_object_array_get_idx( poObjPoints, i ); pa = ptarray_construct_empty(1, 0, 1); parse_geojson_coord(poObjCoords, hasz, pa); geom = (LWGEOM*)lwmpoint_add_lwpoint((LWMPOINT*)geom, (LWPOINT*)lwpoint_construct(root_srid, NULL, pa)); } } return geom; }
static LWGEOM* parse_geojson_point(json_object *geojson, bool *hasz, int *root_srid) { LWGEOM *geom; POINTARRAY *pa; json_object* coords = NULL; POSTGIS_DEBUGF(3, "parse_geojson_point called with root_srid = %d.", *root_srid ); coords = findMemberByName( geojson, "coordinates" ); if ( ! coords ) geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); pa = ptarray_construct_empty(1, 0, 1); parse_geojson_coord(coords, hasz, pa); geom = (LWGEOM *) lwpoint_construct(*root_srid, NULL, pa); POSTGIS_DEBUG(2, "parse_geojson_point finished."); return geom; }
static LWGEOM* parse_geojson_multipolygon(json_object *geojson, bool *hasz, int *root_srid) { LWGEOM *geom = NULL; int i, j, k; json_object* poObjPolys = NULL; if (!*root_srid) { geom = (LWGEOM *)lwcollection_construct_empty(MULTIPOLYGONTYPE, *root_srid, 1, 0); } else { geom = (LWGEOM *)lwcollection_construct_empty(MULTIPOLYGONTYPE, -1, 1, 0); } poObjPolys = findMemberByName( geojson, "coordinates" ); if ( ! poObjPolys ) geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); if( json_type_array == json_object_get_type( poObjPolys ) ) { const int nPolys = json_object_array_length( poObjPolys ); for(i = 0; i < nPolys; ++i) { POINTARRAY **ppa; json_object* poObjPoly = NULL; poObjPoly = json_object_array_get_idx( poObjPolys, i ); ppa = (POINTARRAY**) lwalloc(sizeof(POINTARRAY*)); if( json_type_array == json_object_get_type( poObjPoly ) ) { int nPoints; json_object* points = NULL; int ring = json_object_array_length( poObjPoly ); ppa[0] = ptarray_construct_empty(1, 0, 1); points = json_object_array_get_idx( poObjPoly, 0 ); nPoints = json_object_array_length( points ); for (j=0; j < nPoints; j++ ) { json_object* coords = NULL; coords = json_object_array_get_idx( points, j ); parse_geojson_coord(coords, hasz, ppa[0]); } for(j = 1; j < ring; ++j) { int nPoints; ppa = (POINTARRAY**) lwrealloc((POINTARRAY *) ppa, sizeof(POINTARRAY*) * (j + 1)); ppa[i] = ptarray_construct_empty(1, 0, 1); points = json_object_array_get_idx( poObjPoly, j ); nPoints = json_object_array_length( points ); for (k=0; k < nPoints; k++ ) { json_object* coords = NULL; coords = json_object_array_get_idx( points, k ); parse_geojson_coord(coords, hasz, ppa[i]); } } geom = (LWGEOM*)lwmpoly_add_lwpoly((LWMPOLY*)geom, (LWPOLY*)lwpoly_construct(*root_srid, NULL, ring, ppa)); } } } return geom; }
static LWGEOM* parse_geojson_multipolygon(json_object *geojson, int *hasz, int root_srid) { LWGEOM *geom = NULL; int i, j, k; json_object* poObjPolys = NULL; if (!root_srid) { geom = (LWGEOM *)lwcollection_construct_empty(MULTIPOLYGONTYPE, root_srid, 1, 0); } else { geom = (LWGEOM *)lwcollection_construct_empty(MULTIPOLYGONTYPE, -1, 1, 0); } poObjPolys = findMemberByName( geojson, "coordinates" ); if ( ! poObjPolys ) { geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); return NULL; } if( json_type_array == json_object_get_type( poObjPolys ) ) { const int nPolys = json_object_array_length( poObjPolys ); for(i = 0; i < nPolys; ++i) { json_object* poObjPoly = json_object_array_get_idx( poObjPolys, i ); if( json_type_array == json_object_get_type( poObjPoly ) ) { LWPOLY *lwpoly = lwpoly_construct_empty(geom->srid, lwgeom_has_z(geom), lwgeom_has_m(geom)); int nRings = json_object_array_length( poObjPoly ); for(j = 0; j < nRings; ++j) { json_object* points = json_object_array_get_idx( poObjPoly, j ); if( json_type_array == json_object_get_type( points ) ) { POINTARRAY *pa = ptarray_construct_empty(1, 0, 1); int nPoints = json_object_array_length( points ); for ( k=0; k < nPoints; k++ ) { json_object* coords = json_object_array_get_idx( points, k ); parse_geojson_coord(coords, hasz, pa); } lwpoly_add_ring(lwpoly, pa); } } geom = (LWGEOM*)lwmpoly_add_lwpoly((LWMPOLY*)geom, lwpoly); } } } return geom; }
static LWGEOM* parse_geojson_polygon(json_object *geojson, int *hasz, int root_srid) { POINTARRAY **ppa = NULL; json_object* rings = NULL; json_object* points = NULL; int i = 0, j = 0; int nRings = 0, nPoints = 0; rings = findMemberByName( geojson, "coordinates" ); if ( ! rings ) { geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4); return NULL; } if ( json_type_array != json_object_get_type(rings) ) { geojson_lwerror("The 'coordinates' in GeoJSON are not an array", 4); return NULL; } nRings = json_object_array_length( rings ); /* No rings => POLYGON EMPTY */ if ( ! nRings ) { return (LWGEOM *)lwpoly_construct_empty(root_srid, 0, 0); } for ( i = 0; i < nRings; i++ ) { points = json_object_array_get_idx(rings, i); if ( ! points || json_object_get_type(points) != json_type_array ) { geojson_lwerror("The 'coordinates' in GeoJSON ring are not an array", 4); return NULL; } nPoints = json_object_array_length(points); /* Skip empty rings */ if ( nPoints == 0 ) continue; if ( ! ppa ) ppa = (POINTARRAY**)lwalloc(sizeof(POINTARRAY*) * nRings); ppa[i] = ptarray_construct_empty(1, 0, 1); for ( j = 0; j < nPoints; j++ ) { json_object* coords = NULL; coords = json_object_array_get_idx( points, j ); parse_geojson_coord(coords, hasz, ppa[i]); } } /* All the rings were empty! */ if ( ! ppa ) return (LWGEOM *)lwpoly_construct_empty(root_srid, 0, 0); return (LWGEOM *) lwpoly_construct(root_srid, NULL, nRings, ppa); }