forked from simonw/codespaces-datasette
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpre-processing.py
More file actions
180 lines (148 loc) · 5.07 KB
/
pre-processing.py
File metadata and controls
180 lines (148 loc) · 5.07 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import argparse
import sys
from typing import Tuple, Optional
import pandas as pd
from pyproj import Transformer
def parse_coord_pair(value: object) -> Tuple[Optional[float], Optional[float]]:
"""
Parse a single string containing both coordinates into (lon, lat).
Supported forms (examples):
- "7.1234,47.5678"
- "7.1234 47.5678"
- "POINT(7.1234 47.5678)"
- "7.1234;47.5678"
Assumes order: lon, lat.
Returns (None, None) if parsing fails.
"""
if value is None:
return None, None
s = str(value).strip()
if not s:
return None, None
# Handle WKT POINT syntax
if s.upper().startswith("POINT"):
s = s[s.find("(") + 1 : s.rfind(")")].strip()
# Try common separators: comma, semicolon, space
for sep in [",", ";", " "]:
if sep in s:
parts = [p for p in s.split(sep) if p.strip() != ""]
if len(parts) == 2:
try:
lon = float(parts[0])
lat = float(parts[1])
return lon, lat
except ValueError:
return None, None
# Fallback: if nothing matched
return None, None
def transform_coords(
df: pd.DataFrame,
epsg_in: int,
coord_cols: list,
) -> pd.DataFrame:
"""
Transform coordinates from epsg_in to EPSG:4326.
coord_cols:
- length == 1: one column with both coordinates -> create 'longitude', 'latitude'
- length == 2: two separate columns -> overwrite them with lon/lat in EPSG:4326
"""
transformer = Transformer.from_crs(epsg_in, 4326, always_xy=True)
if len(coord_cols) == 1:
col = coord_cols[0]
if col not in df.columns:
raise ValueError(f"Coordinate column '{col}' not found in CSV.")
# Parse the single column into lon/lat
lon_lat = df[col].apply(parse_coord_pair)
df["longitude"] = lon_lat.apply(lambda t: t[0])
df["latitude"] = lon_lat.apply(lambda t: t[1])
# Transform from epsg_in to 4326
# (If epsg_in == 4326, this is effectively an identity, but still safe.)
lon_vals, lat_vals = transformer.transform(
df["longitude"].values,
df["latitude"].values,
)
df["longitude"] = lon_vals
df["latitude"] = lat_vals
elif len(coord_cols) == 2:
x_col, y_col = coord_cols
if x_col not in df.columns:
raise ValueError(f"Coordinate column '{x_col}' not found in CSV.")
if y_col not in df.columns:
raise ValueError(f"Coordinate column '{y_col}' not found in CSV.")
# Transform and overwrite same columns
lon_vals, lat_vals = transformer.transform(
df[x_col].values,
df[y_col].values,
)
df[x_col] = lon_vals
df[y_col] = lat_vals
else:
raise ValueError("coord-cols must be either 1 or 2 column names.")
return df
def main():
parser = argparse.ArgumentParser(
description=(
"Pre-process a CSV with coordinates: split/transform to EPSG:4326 "
"and save as comma-separated file."
)
)
parser.add_argument(
"csv_path",
help="Path to the input CSV file.",
)
parser.add_argument(
"--sep",
default=",",
help=(
"Field separator used in the input CSV "
"(default: ','). For example: ';' or '\\t'."
),
)
parser.add_argument(
"--coord-cols",
nargs="+",
required=True,
help=(
"Name(s) of coordinate column(s). "
"Provide ONE column if both coordinates are in a single column, "
"or TWO columns for separate X/Y (or lon/lat) columns."
),
)
parser.add_argument(
"--epsg-in",
type=int,
required=True,
help="EPSG code of the input coordinates (e.g. 2056, 3857, 4326).",
)
parser.add_argument(
"--output",
default=None,
help=(
"Path to the output CSV file (comma-separated). "
"If not provided, the input file will be overwritten."
),
)
args = parser.parse_args()
input_path = args.csv_path
sep = args.sep
coord_cols = args.coord_cols
epsg_in = args.epsg_in
output_path = args.output or input_path
try:
df = pd.read_csv(input_path, sep=sep)
except Exception as e:
print(f"Error reading CSV '{input_path}': {e}", file=sys.stderr)
sys.exit(1)
try:
df = transform_coords(df, epsg_in, coord_cols)
except Exception as e:
print(f"Error transforming coordinates: {e}", file=sys.stderr)
sys.exit(1)
# Always save as comma-separated CSV
try:
df.to_csv(output_path, index=False)
except Exception as e:
print(f"Error writing CSV '{output_path}': {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()