Validate a single lat/lon coordinate pair and convert to a list Validate that location: * is a sized variable * with size 2 * allows indexing (i.e. has an ordering) * where both values are floats (or convertible to float) * and both values are not NaN
(location: Sequence[float])
| 67 | |
| 68 | |
| 69 | def validate_location(location: Sequence[float]) -> List[float]: |
| 70 | """Validate a single lat/lon coordinate pair and convert to a list |
| 71 | |
| 72 | Validate that location: |
| 73 | * is a sized variable |
| 74 | * with size 2 |
| 75 | * allows indexing (i.e. has an ordering) |
| 76 | * where both values are floats (or convertible to float) |
| 77 | * and both values are not NaN |
| 78 | """ |
| 79 | if isinstance(location, np.ndarray) or ( |
| 80 | pd is not None and isinstance(location, pd.DataFrame) |
| 81 | ): |
| 82 | location = np.squeeze(location).tolist() |
| 83 | if not hasattr(location, "__len__"): |
| 84 | raise TypeError( |
| 85 | "Location should be a sized variable, " |
| 86 | "for example a list or a tuple, instead got " |
| 87 | f"{location!r} of type {type(location)}." |
| 88 | ) |
| 89 | if len(location) != 2: |
| 90 | raise ValueError( |
| 91 | "Expected two (lat, lon) values for location, " |
| 92 | f"instead got: {location!r}." |
| 93 | ) |
| 94 | try: |
| 95 | coords = (location[0], location[1]) |
| 96 | except (TypeError, KeyError): |
| 97 | raise TypeError( |
| 98 | "Location should support indexing, like a list or " |
| 99 | f"a tuple does, instead got {location!r} of type {type(location)}." |
| 100 | ) |
| 101 | for coord in coords: |
| 102 | try: |
| 103 | float(coord) |
| 104 | except (TypeError, ValueError): |
| 105 | raise ValueError( |
| 106 | "Location should consist of two numerical values, " |
| 107 | f"but {coord!r} of type {type(coord)} is not convertible to float." |
| 108 | ) |
| 109 | if math.isnan(float(coord)): |
| 110 | raise ValueError("Location values cannot contain NaNs.") |
| 111 | return [float(x) for x in coords] |
| 112 | |
| 113 | |
| 114 | def _validate_locations_basics(locations: TypeMultiLine) -> None: |
no outgoing calls