]> granicus.if.org Git - postgis/commitdiff
GeoJSON maintenance
authorPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 26 Mar 2015 22:30:51 +0000 (22:30 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 26 Mar 2015 22:30:51 +0000 (22:30 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@13401 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwin_geojson.c

index 60b728a34c53451e455da14aae818077f97e78bc..9724a5e14bc1824a7b08652ba0fdb2b2890bd431 100644 (file)
@@ -58,7 +58,11 @@ findMemberByName(json_object* poObj, const char* pszName )
 
        if( NULL != json_object_get_object(poTmp) )
        {
-               assert( NULL != json_object_get_object(poTmp)->head );
+               if( NULL == json_object_get_object(poTmp)->head )
+               {
+                       geojson_lwerror("invalid GeoJSON representation", 2);
+                       return NULL;
+               }
 
                for( it.entry = json_object_get_object(poTmp)->head;
                        ( it.entry ?
@@ -89,7 +93,12 @@ parse_geojson_coord(json_object *poObj, int *hasz, POINTARRAY *pa)
                const int nSize = json_object_array_length( poObj );
                LWDEBUGF(3, "parse_geojson_coord called for array size %d.", nSize );
 
-
+               if ( nSize < 2 )
+               {
+                       geojson_lwerror("Too few ordinates in GeoJSON", 4);
+                       return LW_FAILURE;
+               }
+               
                // Read X coordinate
                poObjCoord = json_object_array_get_idx( poObj, 0 );
                pt.x = json_object_get_double( poObjCoord );
@@ -100,7 +109,7 @@ parse_geojson_coord(json_object *poObj, int *hasz, POINTARRAY *pa)
                pt.y = json_object_get_double( poObjCoord );
                LWDEBUGF(3, "parse_geojson_coord pt.y = %f.", pt.y );
 
-               if( nSize == 3 ) /* should this be >= 3 ? */
+               if( nSize < 2 ) /* should this be >= 3 ? */
                {
                        // Read Z coordinate
                        poObjCoord = json_object_array_get_idx( poObj, 2 );
@@ -108,19 +117,27 @@ parse_geojson_coord(json_object *poObj, int *hasz, POINTARRAY *pa)
                        LWDEBUGF(3, "parse_geojson_coord pt.z = %f.", pt.z );
                        *hasz = LW_TRUE;
                }
-               else
+               else if ( nSize == 2 )
                {
                        *hasz = LW_FALSE;
                        /* Initialize Z coordinate, if required */
                        if ( FLAGS_GET_Z(pa->flags) ) pt.z = 0.0;
                }
-
-               /* TODO: should we account for nSize > 3 ? */
+               else 
+               {
+                       /* TODO: should we account for nSize > 3 ? */
+                       /* more than 3 coordinates, we're just dropping dimensions here... */
+               }
 
                /* Initialize M coordinate, if required */
                if ( FLAGS_GET_M(pa->flags) ) pt.m = 0.0;
 
        }
+       else
+       {
+               /* If it's not an array, just don't handle it */
+               return LW_FAILURE;
+       }
 
        return ptarray_append_point(pa, &pt, LW_TRUE);
 }