Created
April 27, 2023 15:07
-
-
Save deangrant/e859301f8f97068a6a9d1f4793028c30 to your computer and use it in GitHub Desktop.
Check if the polygons in the data are valid and closed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_valid_polygon( | |
data: list | |
) -> list: | |
"""" | |
Check if the polygons in the data are valid and closed. | |
Args: | |
data: A list containing polygon data in the format: | |
{'polygons': [{'coordinates': [{'latitude': float, | |
'longitude': float}, | |
{...}, | |
{...}], | |
'id': str}, | |
{...}, | |
{...}], | |
'other_data': ...} | |
Returns: | |
The modified data with any closed polygons having their duplicate | |
coordinate removed. | |
""" | |
# Check if there any any polygons in the data. | |
if len(data['polygons']) > 0: | |
# Loop through each polygon. | |
for polygon in data['polygons']: | |
# Check if the polygon has at least 2 coordinates and the last 2 | |
# coordinates are the same (which means it's a closed polygon). | |
if (len(polygon["coordinates"]) >= 2 and | |
polygon["coordinates"][-1]["latitude"] == | |
polygon["coordinates"][-2]["latitude"] and | |
polygon["coordinates"][-1]["longitude"] == | |
polygon["coordinates"][-2]["longitude"]): | |
# If the polygon is closed, remove the last coordinate, which | |
# is a duplicate of the first coordinate | |
polygon["coordinates"].pop() | |
# Return the data (with any modifications made to the polygons). | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment