Download core/survey_processor.py from Surajnitp/flowmarg: direct link, hf CLI and curl.
- Browser
- Download file 15.4 kB
-
https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/core/survey_processor.py
- Command line
-
hf download hf://spaces/Surajnitp/flowmarg/core/survey_processor.py
-
curl -L -o survey_processor.py https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/core/survey_processor.py
15.4 kB
| """ | |
| Survey Processor - Complete DXF Survey Importer | |
| Network Rail Standards - Professional Edition | |
| """ | |
| import re | |
| import pandas as pd | |
| from typing import Dict, List, Optional, Tuple, Any | |
| from dataclasses import dataclass, field | |
| from datetime import datetime | |
| class SurveyFeature: | |
| layer: str | |
| name: str | |
| chainage: Optional[float] = None | |
| category: str = "Other" | |
| properties: Dict = field(default_factory=dict) | |
| geometry: Dict = field(default_factory=dict) | |
| def to_dict(self) -> Dict: | |
| return { | |
| "layer": self.layer, | |
| "name": self.name, | |
| "chainage": self.chainage, | |
| "category": self.category, | |
| "properties": self.properties, | |
| "geometry": self.geometry | |
| } | |
| class SurveyConstraint: | |
| type: str | |
| location: float | |
| clearance: Optional[float] = None | |
| height: Optional[float] = None | |
| width: Optional[float] = None | |
| description: str = "" | |
| properties: Dict = field(default_factory=dict) | |
| class SurveyImporter: | |
| def __init__(self): | |
| self.features: List[SurveyFeature] = [] | |
| self.constraints: List[SurveyConstraint] = [] | |
| self.coverage: Dict = {} | |
| self.layers: Dict = {} | |
| self.chainage_range: Tuple[float, float] = (None, None) | |
| self.total_features = 0 | |
| def import_dxf(self, dxf_content: bytes) -> Dict: | |
| try: | |
| import ezdxf | |
| import io | |
| doc = ezdxf.read(io.BytesIO(dxf_content)) | |
| msp = doc.modelspace() | |
| self._process_entities(msp) | |
| self._analyze_constraints() | |
| self._analyze_coverage() | |
| return self.get_summary() | |
| except Exception as e: | |
| return { | |
| "status": "error", | |
| "message": f"DXF import failed: {e}", | |
| "error_type": str(type(e).__name__) | |
| } | |
| def _process_entities(self, msp): | |
| self.features = [] | |
| for entity in msp: | |
| try: | |
| layer = entity.dxf.layer | |
| if not self._is_track_layer(layer): | |
| continue | |
| feature = self._extract_feature(entity, layer) | |
| if feature: | |
| self.features.append(feature) | |
| except Exception: | |
| continue | |
| self.total_features = len(self.features) | |
| def _is_track_layer(self, layer: str) -> bool: | |
| track_keywords = [ | |
| "SU-TR", "TRACK", "RAIL", "CHAINAGE", "BRIDGE", | |
| "PLATFORM", "OLE", "OVERHEAD", "SIGNAL", "SWITCH", | |
| "CROSSING", "TURNOUT", "GIRDER", "STRUCTURE" | |
| ] | |
| layer_upper = layer.upper() | |
| return any(kw in layer_upper for kw in track_keywords) | |
| def _extract_feature(self, entity, layer: str) -> Optional[SurveyFeature]: | |
| try: | |
| text = "" | |
| if hasattr(entity, 'text'): | |
| text = entity.text | |
| elif hasattr(entity, 'plain_text'): | |
| text = entity.plain_text | |
| elif hasattr(entity, 'get_text'): | |
| text = entity.get_text() | |
| geometry = self._extract_geometry(entity) | |
| chainage = self._extract_chainage(text, entity) | |
| name = self._extract_name(text, entity) | |
| category = self._classify_feature(layer, text) | |
| properties = self._extract_properties(text, entity) | |
| return SurveyFeature( | |
| layer=layer, | |
| name=name, | |
| chainage=chainage, | |
| category=category, | |
| properties=properties, | |
| geometry=geometry | |
| ) | |
| except Exception: | |
| return None | |
| def _extract_geometry(self, entity) -> Dict: | |
| geometry = {} | |
| if hasattr(entity, 'location'): | |
| loc = entity.location | |
| geometry["x"] = loc.x if hasattr(loc, 'x') else None | |
| geometry["y"] = loc.y if hasattr(loc, 'y') else None | |
| geometry["z"] = loc.z if hasattr(loc, 'z') else None | |
| if hasattr(entity, 'start') and hasattr(entity, 'end'): | |
| geometry["start_x"] = entity.start.x if hasattr(entity.start, 'x') else None | |
| geometry["start_y"] = entity.start.y if hasattr(entity.start, 'y') else None | |
| geometry["end_x"] = entity.end.x if hasattr(entity.end, 'x') else None | |
| geometry["end_y"] = entity.end.y if hasattr(entity.end, 'y') else None | |
| return geometry | |
| def _extract_chainage(self, text: str, entity) -> Optional[float]: | |
| if text: | |
| patterns = [ | |
| r"CH\s*(\d+\.?\d*)", | |
| r"Chainage\s*[:=]\s*(\d+\.?\d*)", | |
| r"Ch\s*[:=]\s*(\d+\.?\d*)", | |
| r"(\d{3,4}\.\d{3})" | |
| ] | |
| for pattern in patterns: | |
| match = re.search(pattern, text, re.I) | |
| if match: | |
| return float(match.group(1)) | |
| if hasattr(entity, 'attributes'): | |
| for attr in entity.attributes: | |
| if hasattr(attr, 'text'): | |
| attr_text = attr.text | |
| match = re.search(r"CH\s*(\d+\.?\d*)", attr_text, re.I) | |
| if match: | |
| return float(match.group(1)) | |
| return None | |
| def _extract_name(self, text: str, entity) -> str: | |
| if text: | |
| match = re.search(r"NAME\s*[:=]\s*([^\n]+)", text, re.I) | |
| if match: | |
| return match.group(1).strip() | |
| if len(text.strip()) < 50: | |
| return text.strip() | |
| if hasattr(entity, 'dxftype'): | |
| return entity.dxftype() | |
| return "Unknown" | |
| def _classify_feature(self, layer: str, text: str) -> str: | |
| layer_upper = layer.upper() | |
| text_upper = text.upper() if text else "" | |
| if "BRIDGE" in layer_upper or "GIRDER" in layer_upper: | |
| return "Bridge" | |
| if "PLATFORM" in layer_upper: | |
| return "Platform" | |
| if "OLE" in layer_upper or "OVERHEAD" in layer_upper: | |
| return "OLE" | |
| if "SIGNAL" in layer_upper: | |
| return "Signal" | |
| if "SWITCH" in layer_upper or "CROSSING" in layer_upper or "TURNOUT" in layer_upper: | |
| return "S&C" | |
| if "TRACK" in layer_upper or "RAIL" in layer_upper: | |
| return "Track" | |
| if "CHAINAGE" in layer_upper: | |
| return "Chainage" | |
| return "Other" | |
| def _extract_properties(self, text: str, entity) -> Dict: | |
| properties = {} | |
| if text: | |
| measurements = re.findall(r"(\d+\.?\d*)\s*(?:m|mm|M|MM)", text) | |
| if measurements: | |
| properties["measurements"] = [float(m) for m in measurements] | |
| clearance_match = re.search(r"(?:Clearance|Clr)\s*[:=]?\s*([\d.]+)", text, re.I) | |
| if clearance_match: | |
| properties["clearance"] = float(clearance_match.group(1)) | |
| height_match = re.search(r"(?:Height|Ht)\s*[:=]?\s*([\d.]+)", text, re.I) | |
| if height_match: | |
| properties["height"] = float(height_match.group(1)) | |
| if hasattr(entity, 'dxf'): | |
| if hasattr(entity.dxf, 'color'): | |
| properties["color"] = entity.dxf.color | |
| if hasattr(entity.dxf, 'linetype'): | |
| properties["linetype"] = entity.dxf.linetype | |
| return properties | |
| def _analyze_constraints(self): | |
| self.constraints = [] | |
| for feature in self.features: | |
| if feature.category == "Bridge": | |
| constraint = SurveyConstraint( | |
| type="Bridge", | |
| location=feature.chainage if feature.chainage else 0, | |
| clearance=feature.properties.get("clearance"), | |
| height=feature.properties.get("height"), | |
| description=f"Bridge at CH {feature.chainage}" if feature.chainage else "Bridge", | |
| properties=feature.properties | |
| ) | |
| self.constraints.append(constraint) | |
| elif feature.category == "Platform": | |
| constraint = SurveyConstraint( | |
| type="Platform", | |
| location=feature.chainage if feature.chainage else 0, | |
| clearance=feature.properties.get("clearance"), | |
| description=f"Platform at CH {feature.chainage}" if feature.chainage else "Platform", | |
| properties=feature.properties | |
| ) | |
| self.constraints.append(constraint) | |
| elif feature.category == "OLE": | |
| constraint = SurveyConstraint( | |
| type="OLE", | |
| location=feature.chainage if feature.chainage else 0, | |
| clearance=feature.properties.get("clearance"), | |
| height=feature.properties.get("height"), | |
| description=f"OLE at CH {feature.chainage}" if feature.chainage else "OLE", | |
| properties=feature.properties | |
| ) | |
| self.constraints.append(constraint) | |
| elif feature.category == "Signal": | |
| constraint = SurveyConstraint( | |
| type="Signal", | |
| location=feature.chainage if feature.chainage else 0, | |
| description=f"Signal at CH {feature.chainage}" if feature.chainage else "Signal", | |
| properties=feature.properties | |
| ) | |
| self.constraints.append(constraint) | |
| elif feature.category == "S&C": | |
| constraint = SurveyConstraint( | |
| type="S&C", | |
| location=feature.chainage if feature.chainage else 0, | |
| description=f"S&C at CH {feature.chainage}" if feature.chainage else "S&C", | |
| properties=feature.properties | |
| ) | |
| self.constraints.append(constraint) | |
| def _analyze_coverage(self): | |
| if not self.features: | |
| self.coverage = { | |
| "total_features": 0, | |
| "chainage_range": [None, None], | |
| "layer_count": 0, | |
| "layers": [], | |
| "categories": {} | |
| } | |
| return | |
| chainages = [f.chainage for f in self.features if f.chainage is not None] | |
| if chainages: | |
| self.chainage_range = (min(chainages), max(chainages)) | |
| layers = {} | |
| categories = {} | |
| for feature in self.features: | |
| layers[feature.layer] = layers.get(feature.layer, 0) + 1 | |
| categories[feature.category] = categories.get(feature.category, 0) + 1 | |
| self.coverage = { | |
| "total_features": len(self.features), | |
| "chainage_range": [self.chainage_range[0], self.chainage_range[1]], | |
| "layer_count": len(layers), | |
| "layers": sorted(layers.keys()), | |
| "categories": categories, | |
| "layer_counts": layers | |
| } | |
| def get_summary(self) -> Dict: | |
| return { | |
| "status": "success", | |
| "coverage": self.coverage, | |
| "constraints": [c.__dict__ for c in self.constraints], | |
| "features": [f.to_dict() for f in self.features], | |
| "summary": { | |
| "total_features": self.total_features, | |
| "total_constraints": len(self.constraints), | |
| "constraints_by_type": { | |
| "bridges": sum(1 for c in self.constraints if c.type == "Bridge"), | |
| "platforms": sum(1 for c in self.constraints if c.type == "Platform"), | |
| "ole": sum(1 for c in self.constraints if c.type == "OLE"), | |
| "signals": sum(1 for c in self.constraints if c.type == "Signal"), | |
| "sandc": sum(1 for c in self.constraints if c.type == "S&C") | |
| }, | |
| "chainage_range": self.coverage["chainage_range"], | |
| "layers": len(self.coverage.get("layers", [])) | |
| }, | |
| "warnings": [ | |
| "Survey data is for REFERENCE only", | |
| "All measurements must be verified from source drawing", | |
| "Chainages should be confirmed with site survey" | |
| ], | |
| "imported_at": datetime.now().isoformat() | |
| } | |
| _TYPE_TO_LOOKUP_KEY = { | |
| "bridge": "bridges", | |
| "platform": "platforms", | |
| "ole": "ole", | |
| "signal": "signals", | |
| "s&c": "sandc", | |
| } | |
| def get_constraints_for_chainage(self, chainage: float, buffer: float = 50.0) -> Dict: | |
| nearby = { | |
| "bridges": [], | |
| "platforms": [], | |
| "ole": [], | |
| "signals": [], | |
| "sandc": [] | |
| } | |
| for constraint in self.constraints: | |
| if abs(constraint.location - chainage) <= buffer: | |
| type_key = self._TYPE_TO_LOOKUP_KEY.get(constraint.type.lower()) | |
| if type_key and type_key in nearby: | |
| nearby[type_key].append({ | |
| "chainage": constraint.location, | |
| "clearance": constraint.clearance, | |
| "height": constraint.height, | |
| "description": constraint.description, | |
| "properties": constraint.properties | |
| }) | |
| return nearby | |
| _REQUIRED_CLEARANCE_M = { | |
| "Bridge": 4.5, | |
| } | |
| def check_clearance(self, track_level: float, chainage: float, constraint_type: str = "Bridge") -> Dict: | |
| constraints = self.get_constraints_for_chainage(chainage) | |
| type_key = self._TYPE_TO_LOOKUP_KEY.get(constraint_type.lower()) | |
| if not type_key or type_key not in constraints: | |
| return { | |
| "status": "NOT FOUND", | |
| "message": f"No {constraint_type} found near CH {chainage}" | |
| } | |
| for c in constraints[type_key]: | |
| clearance = c.get("clearance") | |
| if clearance is not None: | |
| actual_clearance = clearance - track_level | |
| required = self._REQUIRED_CLEARANCE_M.get(constraint_type) | |
| if required is None: | |
| return { | |
| "status": "REQUIRES MANUAL VERIFICATION", | |
| "message": f"No verified clearance requirement configured for '{constraint_type}'. " | |
| f"Measured clearance is {actual_clearance:.3f}m - confirm the required " | |
| f"value against the applicable standard before treating this as PASS/FAIL.", | |
| "clearance": actual_clearance, | |
| "required": None, | |
| "location": c.get("description", f"{constraint_type} at CH {chainage}") | |
| } | |
| return { | |
| "status": "PASS" if actual_clearance >= required else "FAIL", | |
| "clearance": actual_clearance, | |
| "required": required, | |
| "margin": actual_clearance - required, | |
| "location": c.get("description", f"{constraint_type} at CH {chainage}") | |
| } | |
| return { | |
| "status": "MISSING DATA", | |
| "message": f"Clearance data not available for {constraint_type} at CH {chainage}" | |
| } | |