| import json | |
| from pathlib import Path | |
| import pandas as pd | |
| import plotly.express as px | |
| import plotly.io as pio | |
| input_path = Path('triangulated_lamps.csv') | |
| output_dir = Path('output') | |
| output_dir.mkdir(exist_ok=True) | |
| chart_path = output_dir / 'lamp_map_2d.png' | |
| meta_path = output_dir / 'lamp_map_2d.png.meta.json' | |
| csv_out = output_dir / 'lamp_map_2d_points.csv' | |
| if not input_path.exists(): | |
| raise FileNotFoundError(f'Không tìm thấy file input: {input_path.resolve()}') | |
| df = pd.read_csv(input_path) | |
| required_cols = ['lamp_id', 'x_m_local', 'y_m_local', 'mean_ray_distance_m'] | |
| for c in required_cols: | |
| if c not in df.columns: | |
| raise ValueError(f'Thiếu cột bắt buộc: {c}') | |
| df['lamp_id'] = df['lamp_id'].astype(str) | |
| df['x_m_local'] = pd.to_numeric(df['x_m_local'], errors='coerce') | |
| df['y_m_local'] = pd.to_numeric(df['y_m_local'], errors='coerce') | |
| df['mean_ray_distance_m'] = pd.to_numeric(df['mean_ray_distance_m'], errors='coerce') | |
| df = df.dropna(subset=['x_m_local', 'y_m_local']).copy() | |
| df = df.sort_values('lamp_id', key=lambda s: s.astype(int)) | |
| df['label'] = 'Lamp ' + df['lamp_id'] | |
| df['ray_err_m'] = df['mean_ray_distance_m'].round(2) | |
| df.to_csv(csv_out, index=False, encoding='utf-8-sig') | |
| fig = px.scatter( | |
| df, | |
| x='x_m_local', | |
| y='y_m_local', | |
| color='mean_ray_distance_m', | |
| text='lamp_id', | |
| hover_name='label', | |
| hover_data={ | |
| 'x_m_local': ':.2f', | |
| 'y_m_local': ':.2f', | |
| 'mean_ray_distance_m': ':.2f' | |
| }, | |
| title='2D Lamp Map (local XY)<br><span style="font-size: 18px; font-weight: normal;">Source: triangulated_lamps.csv | Color representation mean ray distance</span>' | |
| ) | |
| fig.update_traces( | |
| textposition='top center', | |
| marker=dict(size=14, line=dict(width=1, color='white')), | |
| cliponaxis=False | |
| ) | |
| fig.update_xaxes(title_text='X local (m)') | |
| fig.update_yaxes(title_text='Y local (m)', scaleanchor='x', scaleratio=1) | |
| fig.write_image(str(chart_path)) | |
| with open(meta_path, 'w', encoding='utf-8') as f: | |
| json.dump({ | |
| 'caption': '2D map of lamps in the local XY system', | |
| 'description': 'Scatter plot the triangulated lamps in the local coordinate system, coloring them according to mean ray distance to check spatial stability.' | |
| }, f, ensure_ascii=False) | |
| print(str(chart_path)) | |
| print(str(csv_out)) |