LWDEBUGF(3, "parse_geojson_point called with root_srid = %d.", root_srid );
coords = findMemberByName( geojson, "coordinates" );
- if ( ! coords ) {
+ if ( ! coords )
+ {
geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4);
- return NULL;
- }
+ return NULL;
+ }
pa = ptarray_construct_empty(1, 0, 1);
parse_geojson_coord(coords, hasz, pa);
LWDEBUG(2, "parse_geojson_linestring called.");
points = findMemberByName( geojson, "coordinates" );
- if ( ! points ) {
+ if ( ! points )
+ {
geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4);
- return NULL;
- }
+ return NULL;
+ }
pa = ptarray_construct_empty(1, 0, 1);
}
poObjLines = findMemberByName( geojson, "coordinates" );
- if ( ! poObjLines ) {
+ if ( ! poObjLines )
+ {
geojson_lwerror("Unable to find 'coordinates' in GeoJSON string", 4);
- return NULL;
- }
+ return NULL;
+ }
if( json_type_array == json_object_get_type( poObjLines ) )
{
}
poObjGeoms = findMemberByName( geojson, "geometries" );
- if ( ! poObjGeoms ) {
+ if ( ! poObjGeoms )
+ {
geojson_lwerror("Unable to find 'geometries' in GeoJSON string", 4);
- return NULL;
- }
+ return NULL;
+ }
if( json_type_array == json_object_get_type( poObjGeoms ) )
{
json_object* type = NULL;
const char* name;
- if( NULL == geojson ) {
+ if( NULL == geojson )
+ {
geojson_lwerror("invalid GeoJSON representation", 2);
- return NULL;
- }
+ return NULL;
+ }
type = findMemberByName( geojson, "type" );
- if( NULL == type ) {
+ if( NULL == type )
+ {
geojson_lwerror("unknown GeoJSON type", 3);
- return NULL;
- }
+ return NULL;
+ }
name = json_object_get_string( type );
if (poObjSrsType != NULL)
{
json_object* poObjSrsProps = findMemberByName( poObjSrs, "properties" );
- json_object* poNameURL = findMemberByName( poObjSrsProps, "name" );
- const char* pszName = json_object_get_string( poNameURL );
- *srs = lwalloc(strlen(pszName) + 1);
- strcpy(*srs, pszName);
+ if ( poObjSrsProps )
+ {
+ json_object* poNameURL = findMemberByName( poObjSrsProps, "name" );
+ if ( poNameURL )
+ {
+ const char* pszName = json_object_get_string( poNameURL );
+ if ( pszName )
+ {
+ *srs = lwalloc(strlen(pszName) + 1);
+ strcpy(*srs, pszName);
+ }
+ }
+ }
}
}
lwgeom = parse_geojson(poObj, &hasz, 0);
- json_object_put(poObj);
+ json_object_put(poObj);
lwgeom_add_bbox(lwgeom);
SPI_finish();
return 0;
}
- sprintf(query, "SELECT srid \
- FROM spatial_ref_sys WHERE auth_name||':'||auth_srid = '%s'", srs);
+ sprintf(query,
+ "SELECT srid "
+ "FROM spatial_ref_sys, "
+ "regexp_matches('%s', E'([a-z]+):([0-9]+)', 'gi') AS re "
+ "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid", srs);
err = SPI_exec(query, 1);
if ( err < 0 )
/* no entry in spatial_ref_sys */
if (SPI_processed <= 0)
{
- sprintf(query, "SELECT srid \
- FROM spatial_ref_sys WHERE \
- 'urn:ogc:def:crs:'||auth_name||'::'||auth_srid = '%s'", srs);
+ sprintf(query,
+ "SELECT srid "
+ "FROM spatial_ref_sys, "
+ "regexp_matches('%s', E'urn:ogc:def:crs:([a-z]+):.*:([0-9]+)', 'gi') AS re "
+ "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid", srs);
err = SPI_exec(query, 1);
if ( err < 0 )
PG_RETURN_NULL();
#else /* HAVE_LIBJSON */
# ifdef JSON_C_VERSION
- const char *ver = json_c_version();
+ const char *ver = json_c_version();
# else
const char *ver = "UNKNOWN";
# endif
LWGEOM *lwgeom;
text *geojson_input;
char *geojson;
- char *srs = NULL;
+ char *srs = NULL;
/* Get the geojson stream */
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+
geojson_input = PG_GETARG_TEXT_P(0);
geojson = text2cstring(geojson_input);
- lwgeom = lwgeom_from_geojson(geojson, &srs);
- if ( ! lwgeom ) {
- /* Shouldn't get here */
- elog(ERROR, "lwgeom_from_geojson returned NULL");
- PG_RETURN_NULL();
- }
+ lwgeom = lwgeom_from_geojson(geojson, &srs);
+ if ( ! lwgeom )
+ {
+ /* Shouldn't get here */
+ elog(ERROR, "lwgeom_from_geojson returned NULL");
+ PG_RETURN_NULL();
+ }
- if ( srs ) {
- lwgeom_set_srid(lwgeom, getSRIDbySRS(srs));
- lwfree(srs);
- }
+ if ( srs )
+ {
+ lwgeom_set_srid(lwgeom, getSRIDbySRS(srs));
+ lwfree(srs);
+ }
geom = geometry_serialize(lwgeom);
lwgeom_free(lwgeom);
SELECT 'geomfromgeojson_zm_01', ST_AsEWKT(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[1,2,3,4]}'));
SELECT 'geomfromgeojson_zm_02', ST_AsEWKT(ST_GeomFromGeoJSON('{"type":"LineString","coordinates":[[1,2,3,4],[2,3,4,5]]}'));
+SELECT 'geomfromgeojson_srs_1', ST_AsEWKT(ST_GeomFromGeoJSON('{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}],"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}}}'));
+SELECT 'geomfromgeojson_srs_2', ST_AsEWKT(ST_GeomFromGeoJSON('{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'));
+SELECT 'geomfromgeojson_srs_3', ST_AsEWKT(ST_GeomFromGeoJSON('{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}],"crs":{"type":"name","props":{"name":"urn:ogc:def:crs:EPSG::4326"}}}'));
+SELECT 'geomfromgeojson_srs_4', ST_AsEWKT(ST_GeomFromGeoJSON('{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}],"crs":{"type":"name","properties":{"nm":"EPSG:4326"}}}'));
geomfromgeojson_z_02|LINESTRING(1 2 3,2 3 4)
geomfromgeojson_zm_01|POINT(1 2 3)
geomfromgeojson_zm_02|LINESTRING(1 2 3,2 3 4)
+geomfromgeojson_srs_1|GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101 0,102 1))
+geomfromgeojson_srs_2|GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101 0,102 1))
+geomfromgeojson_srs_3|GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101 0,102 1))
+geomfromgeojson_srs_4|GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101 0,102 1))