File size: 11,490 Bytes
30be4c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Script to import speed camera and speed limit data from JSON files into PostgreSQL.
Run this after creating the database schema.
"""

import asyncio
import json
import os
import sys
from pathlib import Path
from typing import List, Tuple
from urllib.parse import urlparse, urlunparse

# Add parent directory to path to import database modules
sys.path.insert(0, str(Path(__file__).parent.parent))

# Fix: Sanitize DATABASE_URL before importing modules that use it
if not os.getenv("DATABASE_URL"):
    # Default to user provided credentials
    os.environ["DATABASE_URL"] = "postgresql+asyncpg://postgres:1634@localhost:5432/navigation_app"
else:
    # Strip whitespace from DATABASE_URL and ensure /navigation_app path
    url = os.getenv("DATABASE_URL").strip()
    try:
        parsed = urlparse(url)
        if not parsed.path or parsed.path == '/':
            parsed = parsed._replace(path="/navigation_app")
            url = urlunparse(parsed)
    except:
        pass
    os.environ["DATABASE_URL"] = url

from sqlalchemy import func, select, text
from sqlalchemy.ext.asyncio import AsyncSession

from database.database import AsyncSessionLocal, init_db
from database.models import RoadSpeedLimit, SpeedCamera


async def import_speed_cameras(db: AsyncSession, json_file_path: str) -> int:
    """
    Import speed cameras from JSON file.
    
    Args:
        db: Database session
        json_file_path: Path to the JSON file
    
    Returns:
        Number of cameras imported
    """
    print(f"Reading speed cameras from {json_file_path}...")
    
    with open(json_file_path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    cameras_data = data.get('cameras', [])
    print(f"Found {len(cameras_data)} cameras in JSON file")
    
    imported_count = 0
    skipped_count = 0
    
    for cam_data in cameras_data:
        try:
            # Extract data
            latitude = float(cam_data.get('latitude', 0))
            longitude = float(cam_data.get('longitude', 0))
            
            # Skip if coordinates are invalid
            if not (-90 <= latitude <= 90) or not (-180 <= longitude <= 180):
                skipped_count += 1
                continue
            
            # Parse speed limit (can be string like "80" or "/")
            speed_limit_str = str(cam_data.get('speed_limit', '0'))
            try:
                speed_limit_kmh = int(speed_limit_str) if speed_limit_str != '/' else 60  # Default to 60 if not specified
            except (ValueError, TypeError):
                speed_limit_kmh = 60
            
            # Map camera type
            camera_type_map = {
                'G': 'fixed',
                'M': 'mobile',
                'A': 'average_speed'
            }
            camera_type_char = cam_data.get('camera_type', 'G')
            camera_type = camera_type_map.get(camera_type_char, 'fixed')
            
            # Parse direction (can be string like "90" or None)
            direction_str = cam_data.get('direction')
            direction_degrees = None
            if direction_str:
                try:
                    direction_degrees = int(direction_str)
                    if not (0 <= direction_degrees <= 360):
                        direction_degrees = None
                except (ValueError, TypeError):
                    direction_degrees = None
            
            # Create point geometry using PostGIS function
            point_sql = text(f"ST_SetSRID(ST_MakePoint({longitude}, {latitude}), 4326)")
            
            # Check if camera already exists (by location, within 10 meters)
            # SpeedCamera.location is Geography. We must cast our point to Geography too
            # to ensure ST_DWithin uses meters, not degrees.
            existing = await db.execute(
                select(SpeedCamera).where(
                    func.ST_DWithin(
                        SpeedCamera.location,
                        text(f"ST_SetSRID(ST_MakePoint({longitude}, {latitude}), 4326)::geography"),
                        10.0  # 10 meters tolerance
                    )
                ).limit(1)  # Ensure we only fetch one
            )
            if existing.scalar():
                skipped_count += 1
                continue
            
            # Create camera record
            camera = SpeedCamera(
                location=point_sql,
                speed_limit_kmh=speed_limit_kmh,
                camera_type=camera_type,
                direction_degrees=direction_degrees,
                confidence_score=0.80,  # Default confidence for imported data
                verified=True,  # Mark imported data as verified
                verification_count=1,
                notes=f"Imported from {cam_data.get('street', 'Unknown')}, {cam_data.get('city', 'Unknown')}"
            )
            
            db.add(camera)
            imported_count += 1
            
            # Commit in batches of 100
            if imported_count % 100 == 0:
                await db.commit()
                print(f"  Imported {imported_count} cameras...")
        
        except Exception as e:
            print(f"  Error importing camera {cam_data.get('id', 'unknown')}: {e}")
            skipped_count += 1
            continue
    
    # Final commit
    await db.commit()
    print(f"  Imported {imported_count} speed cameras (skipped {skipped_count})")
    return imported_count


async def import_speed_limits(db: AsyncSession, json_file_path: str) -> int:
    """
    Import road speed limits from JSON file (OSM format).
    
    Args:
        db: Database session
        json_file_path: Path to the JSON file
    
    Returns:
        Number of speed limits imported
    """
    print(f"Reading speed limits from {json_file_path}...")
    
    with open(json_file_path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    elements = data.get('elements', [])
    print(f"Found {len(elements)} elements in JSON file")
    
    imported_count = 0
    skipped_count = 0
    
    for element in elements:
        try:
            # Only process ways (not nodes)
            if element.get('type') != 'way':
                continue
            
            # Get geometry
            geometry = element.get('geometry', [])
            if len(geometry) < 2:  # Need at least 2 points for a line
                skipped_count += 1
                continue
            
            # Get tags
            tags = element.get('tags', {})
            maxspeed_str = tags.get('maxspeed')
            
            # Skip if no speed limit specified
            if not maxspeed_str:
                skipped_count += 1
                continue
            
            # Parse speed limit (can be "40", "40 km/h", etc.)
            try:
                # Extract number from string
                speed_limit_kmh = int(''.join(filter(str.isdigit, str(maxspeed_str))))
                if speed_limit_kmh <= 0 or speed_limit_kmh > 200:
                    skipped_count += 1
                    continue
            except (ValueError, TypeError):
                skipped_count += 1
                continue
            
            # Extract coordinates (lat, lon) and convert to (lon, lat) for PostGIS
            coordinates = [(float(pt['lat']), float(pt['lon'])) for pt in geometry]
            
            # Build linestring SQL
            points_array = [f"ST_MakePoint({lon}, {lat})" for lat, lon in coordinates]
            linestring_sql = text(f"ST_SetSRID(ST_MakeLine(ARRAY[{', '.join(points_array)}]), 4326)")
            
            # Get road name and type
            road_name = tags.get('name') or tags.get('ref') or None
            road_type = tags.get('highway', 'unknown')
            
            # Determine direction
            direction = 'both'
            if tags.get('oneway') == 'yes':
                direction = 'forward'
            
            # Create speed limit record
            speed_limit = RoadSpeedLimit(
                road_segment=linestring_sql,
                speed_limit_kmh=speed_limit_kmh,
                road_name=road_name,
                road_type=road_type,
                direction=direction,
                confidence_score=0.85,  # Default confidence for OSM data
                verified=True,  # Mark OSM data as verified
                verification_count=1,
                notes=f"Imported from OpenStreetMap (way {element.get('id', 'unknown')})"
            )
            
            db.add(speed_limit)
            imported_count += 1
            
            # Commit in batches of 50 (speed limits are larger)
            if imported_count % 50 == 0:
                await db.commit()
                print(f"  Imported {imported_count} speed limits...")
        
        except Exception as e:
            print(f"  Error importing speed limit {element.get('id', 'unknown')}: {e}")
            skipped_count += 1
            continue
    
    # Final commit
    await db.commit()
    print(f"  Imported {imported_count} speed limits (skipped {skipped_count})")
    return imported_count


async def main():
    print("=" * 60)
    print("Navigation App - Data Import Script")
    print("=" * 60)

    script_dir = Path(__file__).parent

    camera_files = [
        "tn_complete_cameras_detailed.json",
        "Bengaluru_complete_cameras_detailed.json",
        "delhi_complete_cameras_detailed.json",
        "Hyderabad_complete_cameras_detailed.json",
        "Kolkata_complete_cameras_detailed.json",
        "MUMBAI_complete_cameras_detailed.json",
        "Dehradun_complete_cameras_detailed.json",
        "coimbatore_complete_cameras_detailed.json",
        "puducherry_complete_cameras_detailed.json",
        "nh44_madurai_to_tirunelveli_complete_cameras_detailed.json",
        "KERALA_1_complete_cameras_detailed.json",
        "KERALA_2_complete_cameras_detailed.json",
        "KERALA_3_complete_cameras_detailed.json",
        "KERALA_4_complete_cameras_detailed.json",
        "KERALA_5_complete_cameras_detailed.json",
        "KERALA_6_complete_cameras_detailed.json",
        "KERALA_7_complete_cameras_detailed.json",
        "Chandigarh_complete_cameras_detailed.json",
    ]

    camera_files = [script_dir / f for f in camera_files]

    speed_limits_file = script_dir / "chennai_speed_limit.json"

    missing_files = [f for f in camera_files if not f.exists()]
    if missing_files:
        print("\nERROR: Missing camera files:")
        for f in missing_files:
            print(f"  - {f}")
        return

    if not speed_limits_file.exists():
        print(f"\nERROR: Speed limits file not found: {speed_limits_file}")
        return

    print("\n1. Initializing database...")
    await init_db()
    print("[OK] Database initialized")

    total_cameras = 0

    async with AsyncSessionLocal() as db:
        print("\n2. Importing speed cameras...")
        for cam_file in camera_files:
            print(f"\nImporting {cam_file.name}")
            count = await import_speed_cameras(db, str(cam_file))
            total_cameras += count

        print("\n3. Importing speed limits...")
        speed_limits_count = await import_speed_limits(db, str(speed_limits_file))

    print("\n" + "=" * 60)
    print("Import Complete!")
    print(f"  Speed Cameras (total): {total_cameras}")
    print(f"  Speed Limits: {speed_limits_count}")
    print("=" * 60)

if __name__ == "__main__":
    asyncio.run(main())