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.
-
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.Tyler Welsh– Tyler Welsh2021年09月03日 10:24:15 +00:00Commented Sep 3, 2021 at 10:24
1 Answer 1
When assigning a value to poly
, use this:
poly[regions - 1] = geometry.Polygon(....
Result of print(poly[dividend].intersection(poly[divisor]))
:
-
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.Tyler Welsh– Tyler Welsh2021年09月03日 11:30:50 +00:00Commented Sep 3, 2021 at 11:30
-
In
print
statement, usedumps
function and setrounding_precision
value.print(dumps(poly[dividend].intersection(poly[divisor]), rounding_precision=2))
ReferenceKadir Şahbaz– Kadir Şahbaz2021年09月03日 11:38:24 +00:00Commented 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'.Tyler Welsh– Tyler Welsh2021年09月03日 11:49:25 +00:00Commented Sep 3, 2021 at 11:49
-
Did you import
from shapely.wkt import dumps
?Kadir Şahbaz– Kadir Şahbaz2021年09月03日 11:52:55 +00:00Commented Sep 3, 2021 at 11:52 -
I did that and it says: "'rounding_precision' is an invalid keyword argument for print()".Tyler Welsh– Tyler Welsh2021年09月03日 12:03:18 +00:00Commented Sep 3, 2021 at 12:03