File size: 15,364 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | """
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
@dataclass
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
}
@dataclass
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}"
}
|