File size: 14,472 Bytes
2379054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
XML Parser for Station Base and Geometry Reports
Network Rail Standards - Professional Edition
Complete Implementation - Your Exact Logic
"""

import pandas as pd
from lxml import etree
from typing import Optional, Tuple, List
from .engineering import num, station_of_point, valid, classify

# ============================================
# ALIGNMENT HELPERS
# ============================================

def alignment_names(df: pd.DataFrame) -> List[str]:
    """
    Get unique alignment names from DataFrame.
    YOUR EXACT LOGIC.
    """
    if df is None or df.empty:
        return []
    if "offset_alignment" not in df.columns:
        return []
    return sorted([x for x in df["offset_alignment"].dropna().unique().tolist() if x])

def suggest_alignment(arr: List[str], kind: str, track_no: int) -> str:
    """
    Suggest alignment name based on classification.
    YOUR EXACT LOGIC.
    """
    vals = [x for x in arr if classify(x) == kind]
    return vals[min(max(track_no - 1, 0), len(vals) - 1)] if vals else (arr[0] if arr else "")

# ============================================
# STATION BASE PARSER (YOUR EXACT LOGIC)
# ============================================

def parse_station_base(xml_bytes: bytes) -> pd.DataFrame:
    """
    Parse Station Base XML file.
    YOUR EXACT LOGIC from parse_station_base().
    
    Args:
        xml_bytes: XML file content as bytes
    
    Returns:
        DataFrame with station data
    """
    try:
        root = etree.fromstring(xml_bytes, parser=etree.XMLParser(recover=True, huge_tree=True))
    except Exception:
        return pd.DataFrame()
    
    rows = []
    
    # Find all HorizontalAlignments
    for ha in root.xpath(".//*[local-name()='HorizontalAlignment']"):
        datum = ha.get("name", "")
        
        # Find all StationOffsetPoints
        for sop in ha.xpath(".//*[local-name()='StationOffsetPoint']"):
            cl = sop.find("centerLinePoint")
            ol = sop.find("offsetLinePoint")
            cp = cl.find("point") if cl is not None else None
            op = ol.find("point") if ol is not None else None
            off = sop.find("offsetPoint")
            
            # Get chainages
            dch = station_of_point(cp)
            och = station_of_point(op)
            
            row = {
                "datum_alignment": datum,
                "offset_alignment": sop.get("offsetAlignmentName", ""),
                "datum_chainage": dch,
                "offset_own_chainage": och,
                "chainage_creep_m": (och - dch) if dch is not None and och is not None else None,
                "first_offset_m": num(sop.get("firstOffset")),
                "center_radius_m": num(cl.get("instantaneousRadius")) if cl is not None else None,
                "center_grade": num(cl.get("instantaneousGrade")) if cl is not None else None,
                "center_versine_m": num(cl.get("versine")) if cl is not None else None,
                "center_cant_m": num(cl.get("cant")) if cl is not None else None,
                "offset_radius_m": num(ol.get("instantaneousRadius")) if ol is not None else None,
                "offset_smoothing_radius_m": num(ol.get("smoothingRadius")) if ol is not None else None,
                "offset_cant_m": num(ol.get("cant")) if ol is not None else None,
            }
            
            # Add point coordinates
            for pt, prefix in [(cp, "center"), (op, "offset_line"), (off, "offset_point")]:
                if pt is not None:
                    row[f"{prefix}_n"] = num(pt.get("northing"))
                    row[f"{prefix}_e"] = num(pt.get("easting"))
                    row[f"{prefix}_z"] = num(pt.get("elevation"))
            
            # Get level - YOUR EXACT LOGIC
            level_candidates = [
                row.get("offset_line_z"),
                row.get("offset_point_z"),
                row.get("center_z")
            ]
            row["level_m"] = next((x for x in level_candidates if x is not None and abs(x) > 1e-9), None)
            if row["level_m"] is None:
                row["level_m"] = row.get("center_z")
            
            rows.append(row)
    
    df = pd.DataFrame(rows)
    
    # Sort - YOUR EXACT LOGIC
    if not df.empty and "datum_chainage" in df.columns:
        df = df.sort_values(["datum_alignment", "offset_alignment", "datum_chainage"])
        df = df.reset_index(drop=True)
    
    return df

def detected_summary(df: pd.DataFrame) -> pd.DataFrame:
    """
    Generate summary of detected alignments.
    YOUR EXACT LOGIC.
    """
    if df is None or df.empty:
        return pd.DataFrame()

    # Ensure chainage_creep_m column exists to avoid KeyError in groupby
    if "chainage_creep_m" not in df.columns:
        df = df.copy()
        df["chainage_creep_m"] = None

    out = df.groupby(["datum_alignment", "offset_alignment"], dropna=False).agg(
        Rows=("datum_chainage", "count"),
        From_Ch=("datum_chainage", "min"),
        To_Ch=("datum_chainage", "max"),
        Min_Level=("level_m", "min"),
        Max_Level=("level_m", "max"),
        Min_Creep=("chainage_creep_m", "min"),
        Max_Creep=("chainage_creep_m", "max")
    ).reset_index().rename(columns={
        "datum_alignment": "Datum Alignment",
        "offset_alignment": "Alignment"
    })

    out["Max Abs Creep (m)"] = out[["Min_Creep", "Max_Creep"]].abs().max(axis=1)
    return out

def own_station_key(row) -> Optional[float]:
    """
    Get own station key from a row.
    YOUR EXACT LOGIC.
    """
    if row is None:
        return None
    if valid(row.get("offset_own_chainage")) and abs(float(row.get("offset_own_chainage"))) > 1e-6:
        return float(row.get("offset_own_chainage"))
    return float(row.get("datum_chainage")) if valid(row.get("datum_chainage")) else None

def row_by_chainage(df: pd.DataFrame, alg: str, chainage: float, tol: float) -> Optional[pd.Series]:
    """
    Find row by chainage.
    YOUR EXACT LOGIC.
    """
    w = rows_for_alignment(df, alg)
    if w.empty or chainage is None:
        return None
    idx = (w.datum_chainage - float(chainage)).abs().idxmin()
    return w.loc[idx] if abs(float(w.loc[idx, "datum_chainage"]) - float(chainage)) <= tol else None

def rows_for_alignment(df: pd.DataFrame, alg: str) -> pd.DataFrame:
    """
    Get rows for a specific alignment.
    YOUR EXACT LOGIC.
    """
    if df is None or df.empty:
        return pd.DataFrame()
    if alg in df.get("offset_alignment", pd.Series(dtype=str)).dropna().unique().tolist():
        return df[df.offset_alignment == alg].copy()
    kind = classify(alg)
    candidates = [x for x in alignment_names(df) if classify(x) == kind]
    return df[df.offset_alignment == candidates[0]].copy() if candidates else pd.DataFrame()

# ============================================
# GEOMETRY REPORT PARSER (YOUR EXACT LOGIC)
# ============================================

def parse_geometry_report(xml_bytes: bytes) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
    """
    Parse Geometry Report XML file.
    YOUR EXACT LOGIC from parse_geometry_report().
    
    Args:
        xml_bytes: XML file content as bytes
    
    Returns:
        Tuple of (H_df, V_df, E_df) - Horizontal, Vertical, Event Points
    """
    try:
        root = etree.fromstring(xml_bytes, parser=etree.XMLParser(recover=True, huge_tree=True))
    except Exception:
        return pd.DataFrame(), pd.DataFrame(), pd.DataFrame()
    
    H, V, E = [], [], []
    
    for ha in root.xpath(".//*[local-name()='HorizontalAlignment']"):
        alg = ha.get("name", "")
        
        # ===== Horizontal Elements =====
        he = ha.find("HorizontalElements")
        if he is not None:
            for el in he:
                if not isinstance(el.tag, str):
                    continue
                tag = etree.QName(el).localname
                if tag not in ["HorizontalLine", "HorizontalCircle", "HorizontalSpiral"]:
                    continue
                
                s, e, pi = el.find("Start"), el.find("End"), el.find("PI")
                
                typ = {
                    "HorizontalLine": "Line",
                    "HorizontalCircle": "Curve",
                    "HorizontalSpiral": "Transition"
                }.get(tag, "Unknown")
                
                H.append({
                    "Alignment": alg,
                    "Element No": num(el.get("elementNumber")),
                    "Type": typ,
                    "Start Ch": station_of_point(s),
                    "End Ch": station_of_point(e),
                    "Length (m)": num(el.get("length")),
                    "Chainage Length (m)": (station_of_point(e) - station_of_point(s)) if station_of_point(s) is not None and station_of_point(e) is not None else None,
                    "Radius (m)": num(el.get("radius")),
                    "Start Radius (m)": num(el.get("startRadius")),
                    "End Radius (m)": num(el.get("endRadius")),
                    "Direction": el.get("rotationDirection", el.get("direction", "")),
                    "Design Speed": num(el.get("designSpeed")),
                    "Applied Cant (mm)": num(el.get("appliedCant")) or 0,
                    "A Constant": num(el.get("aConstant")),
                    "Start Level (m)": num(s.get("elevation")) if s is not None else None,
                    "End Level (m)": num(e.get("elevation")) if e is not None else None,
                    "PI Ch": station_of_point(pi),
                    "PI Level (m)": num(pi.get("elevation")) if pi is not None else None,
                })
        
        # ===== Vertical Elements =====
        va = ha.find("VerticalAlignment")
        if va is not None and va.find("VerticalElements") is not None:
            for el in va.find("VerticalElements"):
                if not isinstance(el.tag, str):
                    continue
                tag = etree.QName(el).localname
                if tag not in ["VerticalLine", "VerticalSymmetricalParabola"]:
                    continue
                
                s, e, pvi = el.find("VerticalStart"), el.find("VerticalEnd"), el.find("PVI")
                tp, sd = el.find("TurningPoint"), el.find("SightDistance")
                
                if tag == "VerticalLine":
                    g = num(el.get("grade"))
                    V.append({
                        "Alignment": alg,
                        "Element Type": "Vertical Line",
                        "Start Ch": station_of_point(s),
                        "End Ch": station_of_point(e),
                        "Length (m)": num(el.get("length")),
                        "Start Level (m)": num(s.get("elevation")) if s is not None else None,
                        "End Level (m)": num(e.get("elevation")) if e is not None else None,
                        "Grade (%)": g * 100 if g is not None else None,
                        "Start Grade (%)": g * 100 if g is not None else None,
                        "End Grade (%)": g * 100 if g is not None else None,
                        "K Value": None,
                        "Vertical Curve Length (m)": None,
                        "PVI Ch": None,
                        "PVI Level (m)": None,
                        "Turning Point Type": "",
                        "Turning Point Ch": None,
                        "Turning Point Level (m)": None,
                        "Design Speed": num(sd.get("designSpeed")) if sd is not None else None
                    })
                else:
                    sg, eg = num(el.get("startGrade")), num(el.get("endGrade"))
                    V.append({
                        "Alignment": alg,
                        "Element Type": "Vertical Curve",
                        "Start Ch": station_of_point(s),
                        "End Ch": station_of_point(e),
                        "Length (m)": num(el.get("verticalCurveLength")),
                        "Start Level (m)": num(s.get("elevation")) if s is not None else None,
                        "End Level (m)": num(e.get("elevation")) if e is not None else None,
                        "Start Grade (%)": sg * 100 if sg is not None else None,
                        "End Grade (%)": eg * 100 if eg is not None else None,
                        "K Value": num(el.get("kValue")),
                        "Vertical Curve Length (m)": num(el.get("verticalCurveLength")),
                        "PVI Ch": station_of_point(pvi),
                        "PVI Level (m)": num(pvi.get("elevation")) if pvi is not None else None,
                        "Turning Point Type": tp.get("pointType", "") if tp is not None else "",
                        "Turning Point Ch": station_of_point(tp),
                        "Turning Point Level (m)": num(tp.get("elevation")) if tp is not None else None,
                        "Design Speed": num(sd.get("designSpeed")) if sd is not None else None
                    })
        
        # ===== Event Points =====
        for ep in ha.xpath(".//*[local-name()='HorizontalComputedEventPoints']/*[local-name()='HorizontalEventPoint']"):
            gp = ep.find("GeometryPoint")
            g = num(ep.get("grade"))
            E.append({
                "Alignment": alg,
                "Chainage": station_of_point(gp),
                "Northing": num(gp.get("northing")) if gp is not None else None,
                "Easting": num(gp.get("easting")) if gp is not None else None,
                "Level (m)": num(gp.get("elevation")) if gp is not None else None,
                "Radius (m)": num(ep.get("radius")),
                "Grade (%)": g * 100 if g is not None else None,
                "Point Type": gp.get("pointType", "") if gp is not None else ""
            })
    
    H_df = pd.DataFrame(H)
    V_df = pd.DataFrame(V)
    E_df = pd.DataFrame(E)
    
    # Sort (fixed: previous version reassigned the loop variable `df`
    # instead of H_df/V_df/E_df, so nothing was actually sorted)
    if not H_df.empty and "Start Ch" in H_df.columns:
        H_df = H_df.sort_values(["Alignment", "Start Ch"], ignore_index=True)
    if not V_df.empty and "Start Ch" in V_df.columns:
        V_df = V_df.sort_values(["Alignment", "Start Ch"], ignore_index=True)
    if not E_df.empty and "Chainage" in E_df.columns:
        E_df = E_df.sort_values(["Alignment", "Chainage"], ignore_index=True)

    return H_df, V_df, E_df