pythongeopandasshapelygeopyobspy

Find the furthest coordinates from Polygon and Calculate distance in Nautical Miles


I am looking to solve a problem for any given polygons, how to find 2 furthest coordinates pair (this is also known as Maximum Linear Extent) and compute the distance between those 2 furthest coordinates for given polygon in Nautical Miles.

Altough my attempt was not successful below is the code so far what I have tried:

import shapely.geometry as sg
import math
from shapely import Polygon, length
import geopy.distance as distance

# Create the polygon object
t1 = sg.Polygon([(-74.418225663382, 39.36239030236737), # Further Point 1: Atlantic City, NJ
                 (-74.27880733397238, 39.71055595453288),
                 (-74.75681303480502, 40.219387193292164),
                 (-75.4705021020208, 40.60356289498688),
                 (-76.88460230031765, 40.264996135212186), # Further Point 2: Harrisburg, PA
                 (-74.418225663382, 39.36239030236737)])

# Initialize variables to store the furthest coordinates
furthest_pair = None
max_distance = 0

# Iterate through all pairs of coordinates in the polygon
for i, p1 in enumerate(t1.exterior.coords):
    for j, p2 in enumerate(t1.exterior.coords[i+1:]):
        distance = p1.distance(p2)  # Calculate the distance between the points
        if distance > max_distance:
            max_distance = distance
            furthest_pair = (p1, p2)

# Print the furthest coordinates and their distance
print("Furthest coordinates:", furthest_pair)  # This should print 2 furthest coordinates pair 
print("Distance between them:", max_distance) # Distance in Nautical Miles
print("Should be: ", 126.0, "NM")

Appreciate any feedback and support with this. Thank you!


Solution

  • As the intepreter says:

    AttributeError: 'tuple' object has no attribute 'distance'
    

    The reason why is that you write:

    import geopy.distance as distance
    

    But this line means: import geopy.distance aka distance. This is wrong because I think you would want to import the distance method. This is waht the code should be:

    from geopy.distance import distance
    

    Also the distance method has the following syntax (official doc): distance(tuple1, tuple2). If you want a distance in nautical miles, use .nm at the end of the method.

    In summary:

    import shapely.geometry as sg
    from geopy.distance import distance
    
    # Create the polygon object
    t1 = sg.Polygon([(-74.418225663382, 39.36239030236737), # Further Point 1: Atlantic City, NJ
                     (-74.27880733397238, 39.71055595453288),
                     (-74.75681303480502, 40.219387193292164),
                     (-75.4705021020208, 40.60356289498688),
                     (-76.88460230031765, 40.264996135212186), # Further Point 2: Harrisburg, PA
                     (-74.418225663382, 39.36239030236737)])
    
    # Initialize variables to store the furthest coordinates
    furthest_pair = None
    max_distance = 0
    
    # Iterate through all pairs of coordinates in the polygon
    for i, p1 in enumerate(t1.exterior.coords):
        for j, p2 in enumerate(t1.exterior.coords[i+1:]):
            dist = distance(p1, p2).nm  # Calculate the distance between the points
            if dist > max_distance:
                max_distance = dist
                furthest_pair = (p1, p2)
    
    # Print the furthest coordinates and their distance
    print("Furthest coordinates:", furthest_pair)  # This should print 2 furthest coordinates pair 
    print("Distance between them:", max_distance) # Distance in Nautical Miles
    print("Should be: ", 126.0, "NM")