# Site Connectivity Feature Documentation

## Overview
The Site Connectivity feature allows users to check if all sites (homes) are connected to the network infrastructure. This feature uses spatial analysis to determine connectivity based on distance thresholds and updates the database with connectivity status for QGIS analysis.

## Features

### 1. **Automated Connectivity Check**
- Calculates the distance from each site to the nearest network segment
- Configurable distance threshold (default: 50 meters)
- Uses PostGIS spatial functions for accurate distance calculations

### 2. **Database Integration** 
- Adds `connectivity_status` field to sites table (`connected`/`disconnected`)
- Adds `connectivity_distance` field with distance to nearest segment in meters
- Creates database index for performance optimization

### 3. **Visual Feedback**
- Highlights disconnected sites on the map with red markers
- Displays connectivity statistics (total, connected, disconnected, rate)
- Popup information showing site details and distance to network

### 4. **QGIS Integration**
- Updated site attributes can be viewed in QGIS
- Filter and symbolize sites by connectivity status
- Use connectivity_distance field for further analysis

## Implementation

### Backend Changes
- **`qc/site_connectivity.go`**: New QC module with connectivity analysis
- **`models/models.go`**: Updated Sites struct with connectivity fields
- **`main.go`**: Registered new route for site connectivity endpoint
- **`migrations/add_site_connectivity_fields.sql`**: Database migration script

### Frontend Changes
- **`index.html`**: Added "Check Site Connectivity" button
- **`main.js`**: Added JavaScript functionality for connectivity checking

### API Endpoint
```
GET /api/qc/site-connectivity?map_id={market_id}&zone={zone}&max_distance={meters}
```

**Parameters:**
- `map_id` (required): Market/project ID
- `zone` (optional): Zone filter 
- `max_distance` (optional): Distance threshold in meters (default: 50)

**Response:**
```json
{
  "total_sites": 150,
  "connected_sites": 145, 
  "disconnected_sites": 5,
  "connectivity_rate": 96.7,
  "max_distance_meters": 50,
  "results": [
    {
      "site_id": 123,
      "site_name": "Site Name",
      "is_connected": false,
      "nearest_distance": 75.5,
      "connectivity_status": "disconnected",
      "geometry": {...}
    }
  ]
}
```

## Usage Instructions

### 1. Database Setup
Run the migration script to add required columns:
```sql
-- Execute this in your database
\i Backend/migrations/add_site_connectivity_fields.sql
```

### 2. Running the Check
1. Start the backend server: `cd Backend && go run main.go`
2. Open the web application
3. Select a market and optionally a zone
4. Click "Check Site Connectivity" button
5. View results on the map and in the status display

### 3. QGIS Analysis
1. Connect to your PostGIS database in QGIS
2. Load the sites layer
3. View the attribute table to see connectivity fields:
   - `connectivity_status`: "connected" or "disconnected"  
   - `connectivity_distance`: Distance in meters to nearest segment
4. Use these fields for filtering, symbology, or further analysis

## Configuration

### Distance Threshold
The default 50-meter threshold can be adjusted by:
- Modifying the frontend JavaScript (`max_distance=50`)
- Passing different values via the API parameter
- Consider local regulations and technical requirements

### Performance Considerations
- Uses PostGIS spatial indexes for efficient distance calculations
- Database index created on connectivity_status for fast filtering
- Suitable for datasets with thousands of sites

## Workflow Integration

This feature integrates seamlessly with existing QC workflows:
1. Load market and zone data
2. Run connectivity analysis alongside other QC checks
3. Export results to QGIS for detailed analysis and remediation planning
4. Update network design based on connectivity gaps

## Benefits

1. **Automated Analysis**: Eliminates manual site-by-site connectivity checking
2. **Database Persistence**: Results stored for historical analysis and reporting
3. **QGIS Integration**: Seamless workflow for GIS analysts
4. **Configurable**: Adjustable distance thresholds for different scenarios
5. **Visual Feedback**: Clear identification of problem areas on the map

## Future Enhancements

Potential improvements that could be added:
- Multiple distance thresholds for different site types
- Batch connectivity updates for multiple markets
- Export functionality for disconnected sites
- Integration with network planning tools
- Automated report generation