@@ -554,15 +554,22 @@ def check_catalog_file_name(self) -> bool:
554
554
return True
555
555
556
556
def check_geometry_coordinates_order (self ) -> bool :
557
- """Checks if the coordinates in a geometry are in the correct order (longitude, latitude) .
557
+ """Checks if the coordinates in a geometry may be in the incorrect order.
558
558
559
- This function verifies that coordinates follow the GeoJSON specification where positions are in
560
- [longitude, latitude] order. It detects cases where coordinates might be accidentally reversed
561
- by checking if latitude values (which should be the second element in each coordinate pair)
562
- are within the valid range of -90 to 90 degrees.
559
+ This function attempts to detect cases where coordinates might not follow the GeoJSON
560
+ specification where positions should be in [longitude, latitude] order. It uses several
561
+ heuristics to identify potentially problematic coordinates:
562
+
563
+ 1. Checks if latitude values (second element) exceed ±90 degrees
564
+ 2. Checks if longitude values (first element) exceed ±180 degrees
565
+ 3. Uses a heuristic to detect when coordinates are likely reversed
566
+ (when first value > 90, second value < 90, and first value > second value*2)
567
+
568
+ Note that this check can never definitively determine if coordinates are reversed
569
+ or simply contain errors, it can only flag suspicious patterns.
563
570
564
571
Returns:
565
- bool: True if coordinates appear to be in the correct order, False if they seem reversed.
572
+ bool: True if coordinates appear to be in the expected order, False if they may be reversed.
566
573
"""
567
574
if "geometry" not in self .data or self .data ["geometry" ] is None :
568
575
return True
@@ -577,15 +584,19 @@ def is_valid_coordinate(coord):
577
584
lon , lat = coord [0 ], coord [1 ]
578
585
579
586
# Check if latitude (second value) is outside the valid range
580
- # This could indicate reversed coordinates
581
587
if abs (lat ) > 90 :
582
588
return False
583
589
584
590
# Check if longitude (first value) is outside the valid range
585
- # This is another indicator of possible coordinate reversal
586
591
if abs (lon ) > 180 :
587
592
return False
588
593
594
+ # Additional heuristic for likely reversed coordinates
595
+ # If the first value (supposed longitude) is > 90, second value (supposed latitude) is < 90,
596
+ # and first value is significantly larger than second value, they may be reversed
597
+ if abs (lon ) > 90 and abs (lat ) < 90 and abs (lon ) > abs (lat ) * 2 :
598
+ return False
599
+
589
600
return True
590
601
591
602
# Function to recursively check all coordinates in a geometry
@@ -706,7 +717,7 @@ def create_best_practices_dict(self) -> Dict:
706
717
not self .check_geometry_coordinates_order ()
707
718
and config ["geometry_coordinates_order" ] == True
708
719
):
709
- msg_1 = "Geometry coordinates should be in the correct order ( longitude, latitude)"
720
+ msg_1 = "Geometry coordinates may be reversed or contain errors (expected order: longitude, latitude)"
710
721
best_practices_dict ["geometry_coordinates_order" ] = [msg_1 ]
711
722
712
723
return best_practices_dict
0 commit comments