@@ -181,12 +181,12 @@ private static LatLngBounds parseBoundingBox(JSONArray coordinates) throws JSONE
181181 private static final int MAX_GEOMETRY_DEPTH = 20 ;
182182
183183 public static Geometry parseGeometry (JSONObject geoJsonGeometry ) {
184- return parseGeometry (geoJsonGeometry , 0 );
184+ return parseGeometry (geoJsonGeometry , MAX_GEOMETRY_DEPTH );
185185 }
186186
187- private static Geometry parseGeometry (JSONObject geoJsonGeometry , int depth ) {
188- if (depth > MAX_GEOMETRY_DEPTH ) {
189- Log .w (LOG_TAG , "GeoJSON geometry nesting depth exceeds maximum (" + MAX_GEOMETRY_DEPTH + ") , ignoring." );
187+ public static Geometry parseGeometry (JSONObject geoJsonGeometry , int maxDepth ) {
188+ if (maxDepth < 0 ) {
189+ Log .w (LOG_TAG , "GeoJSON geometry nesting depth limit exhausted , ignoring." );
190190 return null ;
191191 }
192192 try {
@@ -201,7 +201,7 @@ private static Geometry parseGeometry(JSONObject geoJsonGeometry, int depth) {
201201 // No geometries or coordinates array
202202 return null ;
203203 }
204- return createGeometry (geometryType , geometryArray , depth );
204+ return createGeometry (geometryType , geometryArray , maxDepth );
205205 } catch (JSONException e ) {
206206 return null ;
207207 }
@@ -250,7 +250,7 @@ private static HashMap<String, String> parseProperties(JSONObject properties)
250250 * @return Geometry object
251251 * @throws JSONException if the coordinates or geometries could be parsed
252252 */
253- private static Geometry createGeometry (String geometryType , JSONArray geometryArray , int depth )
253+ private static Geometry createGeometry (String geometryType , JSONArray geometryArray , int maxDepth )
254254 throws JSONException {
255255 switch (geometryType ) {
256256 case POINT :
@@ -266,7 +266,7 @@ private static Geometry createGeometry(String geometryType, JSONArray geometryAr
266266 case MULTIPOLYGON :
267267 return createMultiPolygon (geometryArray );
268268 case GEOMETRY_COLLECTION :
269- return createGeometryCollection (geometryArray , depth + 1 );
269+ return createGeometryCollection (geometryArray , maxDepth - 1 );
270270 }
271271 return null ;
272272 }
@@ -371,13 +371,13 @@ private static GeoJsonMultiPolygon createMultiPolygon(JSONArray coordinates)
371371 * @return GeoJsonGeometryCollection object
372372 * @throws JSONException if geometries cannot be parsed
373373 */
374- private static GeoJsonGeometryCollection createGeometryCollection (JSONArray geometries , int depth )
374+ private static GeoJsonGeometryCollection createGeometryCollection (JSONArray geometries , int maxDepth )
375375 throws JSONException {
376376 ArrayList <Geometry > geometryCollectionElements
377377 = new ArrayList <>();
378378 for (int i = 0 ; i < geometries .length (); i ++) {
379379 JSONObject geometryElement = geometries .getJSONObject (i );
380- Geometry geometry = parseGeometry (geometryElement , depth );
380+ Geometry geometry = parseGeometry (geometryElement , maxDepth );
381381 if (geometry != null ) {
382382 // Do not add geometries that could not be parsed
383383 geometryCollectionElements .add (geometry );
0 commit comments