Download core/xml_parser.py from Surajnitp/flowmarg: direct link, hf CLI and curl.
- Browser
- Download file 14.5 kB
-
https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/core/xml_parser.py
- Command line
-
hf download hf://spaces/Surajnitp/flowmarg/core/xml_parser.py
-
curl -L -o xml_parser.py https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/core/xml_parser.py
14.5 kB
| """ | |
| 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 | |