-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertcoordinates.py
More file actions
36 lines (30 loc) · 1.19 KB
/
convertcoordinates.py
File metadata and controls
36 lines (30 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
""""
This script takes a CSV file containing DMS coordinates (Latitude and Longitude columns)
into DD format, returning a CSV file with two columns with the converted values.
"""
import pandas as pd
# Function to convert DMS to DD
def dms_to_dd(dms, direction):
"""Convert DMS (Degrees, Minutes, Seconds) to Decimal Degrees."""
# Split DMS string into components
dms = dms.replace('°', '').replace('’', '').replace('’’', '').replace('"', '').strip()
parts = dms.split()
degrees, minutes, seconds = map(float, parts)
decimal = degrees + (minutes / 60) + (seconds / 3600)
if direction in ['S', 'W']:
decimal *= -1
return decimal
# Load the CSV file
input_file = "input.csv" # Replace with your file path
output_file = "output.csv"
df = pd.read_csv(input_file)
# Apply the conversion for Latitude and Longitude
df['Latitude_DD'] = df['Latitude'].apply(
lambda x: dms_to_dd(x[:-1], x[-1]) # Separate value and direction
)
df['Longitude_DD'] = df['Longitude'].apply(
lambda x: dms_to_dd(x[:-1], x[-1]) # Separate value and direction
)
# Save the updated data to a new CSV file
df.to_csv(output_file, index=False)
print(f"Converted file saved as {output_file}")