Sure! Here's a Python program that does what you described:
python
import argparse
import csv
from math import radians, sin, cos, sqrt, atan2
def calculate_distance(lat1, lng1, lat2, lng2):
R = 6371 # Radius of the Earth in kilometers
# Convert latitudes and longitudes from degrees to radians
lat1_rad = radians(lat1)
lng1_rad = radians(lng1)
lat2_rad = radians(lat2)
lng2_rad = radians(lng2)
# Calculate the differences between latitudes and longitudes
dlat = lat2_rad - lat1_rad
dlng = lng2_rad - lng1_rad
# Use the Haversine formula to calculate the distance
a = sin(dlat / 2) ** 2 + cos(lat1_rad) * cos(lat2_rad) * sin(dlng / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return distance
def calculate_polygon_area(coordinates):
area = 0
# Calculate the area using the Shoelace formula
for i in range(len(coordinates)):
j = (i + 1) % len(coordinates)
area += coordinates[i][0] * coordinates[j][1] - coordinates[j][0] * coordinates[i][1]
return abs(area / 2)
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Calculate geographic distances and polygon area.')
parser.add_argument('filename', help='CSV file containing lat,lng values')
parser.add_argument('--mode', choices=['distance', 'area'], default='distance', help='Mode of calculation (distance or area)')
args = parser.parse_args()
# Read lat,lng values from the CSV file
coordinates = []
with open(args.filename, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
lat = float(row['lat'])
lng = float(row['lng'])
coordinates.append((lat, lng))
# Calculate and print the result based on the mode
if args.mode == 'distance':
total_distance = 0
for i in range(len(coordinates) - 1):
lat1, lng1 = coordinates[i]
lat2, lng2 = coordinates[i +