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

75 Views Asked by At

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!

2

There are 2 best solutions below

0
AndreVale69 On BEST ANSWER

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")
0
Brendan Mitchell On

I can make your code work with three small modifications:

  1. Use the distance function from geopy (which you have already imported). You attempted to call p1.distance(p2), but p1 is a tuple and has no function called distance. You'll get an error informing you of that. Instead, call distance.distance(p1, p2).
  2. Eliminate duplicate names. You use the name distance in multiple places in your code to mean different things. The first is here:
    import geopy.distance as distance
    And the second is here:
    distance = p1.distance(p2)
    This effectively gets rid of the distance module you imported and replaces it with whatever the distance function returned (a geodesic, I believe). This causes an error the next time the loop goes around and tries to use the module: AttributeError: 'geodesic' object has no attribute 'distance'.
    Rename your valiable to pair_distance or something like that.
  3. Your coordinates are switched. Latitude then Longitude. Your distances will turn out funky if you have it backwards.