4

I have made a program that calculates area and perimeter based on 2D coordinates (latitude and latitude) given by the user, asking if they want to include more coordinates from the third pair, and evaluating the area and perimeter each time a pair of coordinates is added.

Besides, it compares the values of the area and perimeter from different regions, choosing how many regions the user wants to evaluate, and I have added a zip that "joins" each latitude to its longitude value, and I have converted that zip in a list, so I can print it each time because it is in a loop (that calculates the number of areas the user wants):

from pyproj import Geod
geod = Geod(ellps='WGS84')
regions = 0
area_list = []
perimeter_list = []
pair_coordinates_list = {}
while True:
 regions += 1
 lats = []
 lons = []
 count = 0
 while True:
 count += 1
 lat = float(input(f"Enter latitude #{count}: "))
 lon = float(input(f"Enter longitude #{count}: "))
 lats.append(lat)
 lons.append(lon)
 
 if count < 3:
 print("Not enough information to calculate the area.")
 continue
 
 poly_area, poly_perimeter = geod.polygon_area_perimeter(lons, lats)
 print(f"\nLatitude coordinates #{regions}: {lats}")
 print(f"Longitude coordinates #{regions}: {lons}")
 
 pair_coordinates = zip(lats,lons)
 type(pair_coordinates)
 pair_coordinates_list[regions - 1] = list(pair_coordinates)
 print(f"Pair of coordinates #{regions}: ", pair_coordinates_list[regions - 1])
 
 for info in zip(lats,lons):
 print(info)
 print(f"Area #{regions}:", (abs(float(poly_area))) / (1000000), "km^2")
 print(f"Perimeter #{regions}:", (abs(float(poly_perimeter))) / (1000), "km")
 if input('Enter more coordinates? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
 area_list.append(poly_area)
 perimeter_list.append(poly_perimeter)
 break 
 if input('More regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
 break 
while True:
 dividend = int(input(f"Write what region is the dividend:")) - 1
 area_km_dividend = (abs(area_list[dividend])/1000000)
 perimeter_km_dividend = (abs(perimeter_list[dividend])/1000)
 print(f"Area: {area_km_dividend}")
 print(f"Perimeter: {perimeter_km_dividend}")
 print(f"Pair of coordinates of region #{dividend + 1}: ", list(pair_coordinates_list[dividend]))
 
 divisor = int(input(f"Which region is the divisor?:")) - 1
 area_km_divisor = (abs(area_list[divisor])/1000000)
 perimeter_km_divisor = (abs(perimeter_list[divisor])/1000)
 print(f"Area: {area_km_divisor}")
 print(f"Perimeter: {perimeter_km_divisor}")
 print(f"Pair of coordinates of region #{divisor + 1}: ", list(pair_coordinates_list[divisor]))
 
 
 area_divid_div = area_km_dividend / area_km_divisor
 area_div_divid = area_km_divisor / area_km_dividend
 
 perimeter_divid_div = perimeter_km_dividend / perimeter_km_divisor
 perimeter_div_divid = perimeter_km_divisor / perimeter_km_dividend
 
 if area_divid_div > 1:
 print(f"\nArea of region #{dividend + 1} is", area_divid_div*100 - 100, f"% larger than #{divisor + 1}")
 if perimeter_divid_div > 1:
 print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
 else:
 print(f"Perimeter of region #{divisor + 1} es un", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
 else:
 print(f"\nArea of region #{divisor + 1} is", (area_div_divid*100) - 100, f"% larger than #{dividend + 1}")
 if perimeter_divid_div > 1:
 print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
 else:
 print(f"Perimeter of region #{divisor + 1} is", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
 
 if input('Do you want to evaluate more regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
 break

And it writes on the terminal the following lines:

Enter latitude #1: 8
Enter longitude #1: -4
Not enough information to calculate the area.
Enter latitude #2: 6
Enter longitude #2: 1
Not enough information to calculate the area.
Enter latitude #3: 9
Enter longitude #3: 0
Latitude coordinates #1: [8.0, 6.0, 9.0]
Longitude coordinates #1: [-4.0, 1.0, 0.0]
Pair of coordinates #1: [(8.0, -4.0), (6.0, 1.0), (9.0, 0.0)]
(8.0, -4.0)
(6.0, 1.0)
(9.0, 0.0)
Area #1: 79182.40432720438 km^2
Perimeter #1: 1398.8161417294746 km
Enter more coordinates? [Y/N] y
Enter latitude #4: 7
Enter longitude #4: 3
Latitude coordinates #1: [8.0, 6.0, 9.0, 7.0]
Longitude coordinates #1: [-4.0, 1.0, 0.0, 3.0]
Pair of coordinates #1: [(8.0, -4.0), (6.0, 1.0), (9.0, 0.0), (7.0, 3.0)]
(8.0, -4.0)
(6.0, 1.0)
(9.0, 0.0)
(7.0, 3.0)
Area #1: 12638.320661535065 km^2
Perimeter #1: 2123.065070961561 km
Enter more coordinates? [Y/N] n
More regions? [Y/N] y
Enter latitude #1: 1
Enter longitude #1: -6
Not enough information to calculate the area.
Enter latitude #2: 4
Enter longitude #2: 7
Not enough information to calculate the area.
Enter latitude #3: 2
Enter longitude #3: 5
Latitude coordinates #2: [1.0, 4.0, 2.0]
Longitude coordinates #2: [-6.0, 7.0, 5.0]
Pair of coordinates #2: [(1.0, -6.0), (4.0, 7.0), (2.0, 5.0)]
(1.0, -6.0)
(4.0, 7.0)
(2.0, 5.0)
Area #2: 124238.38575956487 km^2
Perimeter #2: 3025.8407986864295 km
Enter more coordinates? [Y/N] n
More regions? [Y/N] n
Write what region is the dividend:2
Area: 124238.38575956487
Perimeter: 3025.8407986864295
Pair of coordinates of region #2: [(1.0, -6.0), (4.0, 7.0), (2.0, 5.0)]
Which region is the divisor?:1
Area: 12638.320661535065
Perimeter: 2123.065070961561
Pair of coordinates of region #1: [(8.0, -4.0), (6.0, 1.0), (9.0, 0.0), (7.0, 3.0)]
Area of region #2 is 883.0292258502858 % larger than #1
Perimeter of region #2 is 42.5222825278733 % larger than #1
Do you want to evaluate more regions? [Y/N] n

To know if the regions evaluated intercept, I have to create polygons from the pair_coordinates_list = {}. With the additions I have made, the code is like this:

from pyproj import Geod
from shapely import geometry
geod = Geod(ellps='WGS84')
regions = 0
area_list = []
perimeter_list = []
pair_coordinates_list = {}
poly = {}
while True:
 regions += 1
 lats = []
 lons = []
 count = 0
 while True:
 count += 1
 lat = float(input(f"Enter latitude #{count}: "))
 lon = float(input(f"Enter longitude #{count}: "))
 lats.append(lat)
 lons.append(lon)
 if count < 3:
 print("Not enough information to calculate the area.")
 continue
 poly_area, poly_perimeter = geod.polygon_area_perimeter(lons, lats)
 print(f"\nLatitude coordinates #{regions}: {lats}")
 print(f"Longitude coordinates #{regions}: {lons}")
 pair_coordinates = zip(lats,lons)
 type(pair_coordinates)
 pair_coordinates_list[regions - 1] = list(pair_coordinates)
 print(f"Pair of coordinates #{regions}: ", pair_coordinates_list[regions - 1])
 
 poly = geometry.Polygon (pares_coordenadas_list[regions - 1]) #NEW
 print(poly) #NEW
 for info in zip(lats,lons):
 print(info)
 print(f"Area #{regions}:", (abs(float(poly_area))) / (1000000), "km^2")
 print(f"Perimeter #{regions}:", (abs(float(poly_perimeter))) / (1000), "km")
 if input('Enter more coordinates? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
 area_list.append(poly_area)
 perimeter_list.append(poly_perimeter)
 break 
 if input('More regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
 break 
while True:
 dividend = int(input("Write what region is the dividend:")) - 1
 area_km_dividend = (abs(area_list[dividend])/1000000)
 perimeter_km_dividend = (abs(perimeter_list[dividend])/1000)
 print(f"Area: {area_km_dividend}")
 print(f"Perimeter: {perimeter_km_dividend}")
 print(f"Pair of coordinates of region #{dividend + 1}: ", list(pair_coordinates_list[dividend]))
 divisor = int(input("Which region is the divisor?:")) - 1
 area_km_divisor = (abs(area_list[divisor])/1000000)
 perimeter_km_divisor = (abs(perimeter_list[divisor])/1000)
 print(f"Area: {area_km_divisor}")
 print(f"Perimeter: {perimeter_km_divisor}")
 print(f"Pair of coordinates of region #{divisor + 1}: ", list(pair_coordinates_list[divisor]))
 area_divid_div = area_km_dividend / area_km_divisor
 area_div_divid = area_km_divisor / area_km_dividend
 perimeter_divid_div = perimeter_km_dividend / perimeter_km_divisor
 perimeter_div_divid = perimeter_km_divisor / perimeter_km_dividend
 
 print(poly[dividend].intersection(poly[divisor])) #NEW
 if area_divid_div > 1:
 print(f"\nArea of region #{dividend + 1} is", area_divid_div*100 - 100, f"% larger than #{divisor + 1}")
 if perimeter_divid_div > 1:
 print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
 else:
 print(f"Perimeter of region #{divisor + 1} es un", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
 else:
 print(f"\nArea of region #{divisor + 1} is", (area_div_divid*100) - 100, f"% larger than #{dividend + 1}")
 if perimeter_divid_div > 1:
 print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
 else:
 print(f"Perimeter of region #{divisor + 1} is", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
 if input('Do you want to evaluate more regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
 break

Line 74:

 print(poly[dividend].intersection(poly[divisor])) #NEW

Causes and error:

'Polygon' object is not subscriptable

I don't know how solve this problem.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Sep 3, 2021 at 9:55
1
  • I would like to evaluate if the polygons created by input from the user, intersect. That is why I made a zip out of longitude and latitude coordinates, later I made a list of that zip, and now I want to compare data from that list, that is in a while loop. Commented Sep 3, 2021 at 10:24

1 Answer 1

5

When assigning a value to poly, use this:

poly[regions - 1] = geometry.Polygon(....

Result of print(poly[dividend].intersection(poly[divisor])):

enter image description here

answered Sep 3, 2021 at 10:42
9
  • Thank you so much! By the way, do you know how can I limit the number of decimals? I want to print it to show the area that overlaps, and it is kinda "ugly" sometimes. Commented Sep 3, 2021 at 11:30
  • In print statement, use dumps function and set rounding_precision value. print(dumps(poly[dividend].intersection(poly[divisor]), rounding_precision=2)) Reference Commented Sep 3, 2021 at 11:38
  • I don't know why, but it does not work. I tried writing "dump" and "json.dumps" instead of "dumps", importing json, and says 'Object of type Polygon is not JSON serializable'. Commented Sep 3, 2021 at 11:49
  • Did you import from shapely.wkt import dumps? Commented Sep 3, 2021 at 11:52
  • I did that and it says: "'rounding_precision' is an invalid keyword argument for print()". Commented Sep 3, 2021 at 12:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.