Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Startup script to ensure all required files are available before running the main app | |
| """ | |
| import os | |
| import sys | |
| import subprocess | |
| import urllib.request | |
| import zipfile | |
| import geopandas as gpd | |
| from pathlib import Path | |
| def ensure_shapefile_exists(): | |
| """Ensure the India State Boundary shapefile exists""" | |
| shapefile_path = "shapefiles/India_State_Boundary.shp" | |
| if os.path.exists(shapefile_path): | |
| print(f"β Shapefile found at {shapefile_path}") | |
| return True | |
| print(f"β Shapefile not found at {shapefile_path}") | |
| # Try to pull from git lfs if available | |
| try: | |
| if os.path.exists(".git"): | |
| print("Attempting to pull LFS files...") | |
| result = subprocess.run(["git", "lfs", "pull"], capture_output=True, text=True) | |
| if result.returncode == 0 and os.path.exists(shapefile_path): | |
| print("β Successfully pulled LFS files") | |
| return True | |
| else: | |
| print("β Git LFS pull failed or file still missing") | |
| except Exception as e: | |
| print(f"β Git LFS not available: {e}") | |
| # Download alternative shapefile | |
| print("Downloading alternative India boundary data...") | |
| try: | |
| # Create shapefiles directory | |
| os.makedirs("shapefiles", exist_ok=True) | |
| # Download from Natural Earth (reliable source) | |
| url = "https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip" | |
| zip_path = "temp_countries.zip" | |
| print("Downloading Natural Earth country boundaries...") | |
| urllib.request.urlretrieve(url, zip_path) | |
| # Extract | |
| with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
| zip_ref.extractall("temp_extract") | |
| # Load and filter for India | |
| world_data = gpd.read_file("temp_extract/ne_50m_admin_0_countries.shp") | |
| india_data = world_data[world_data['NAME'] == 'India'] | |
| if len(india_data) == 0: | |
| # Try alternative name matching | |
| india_data = world_data[world_data['NAME_EN'] == 'India'] | |
| if len(india_data) > 0: | |
| # Save as our expected shapefile | |
| india_data.to_file(shapefile_path) | |
| print(f"β Successfully created {shapefile_path}") | |
| else: | |
| raise Exception("Could not find India in the world data") | |
| # Cleanup | |
| os.remove(zip_path) | |
| import shutil | |
| shutil.rmtree("temp_extract", ignore_errors=True) | |
| return True | |
| except Exception as e: | |
| print(f"β Failed to download alternative shapefile: {e}") | |
| # Create a simple fallback | |
| print("Creating fallback boundary...") | |
| try: | |
| from shapely.geometry import Polygon | |
| # Simple India bounding box | |
| india_bounds = [68.0, 6.0, 97.5, 37.0] # [min_lon, min_lat, max_lon, max_lat] | |
| polygon = Polygon([ | |
| (india_bounds[0], india_bounds[1]), # SW | |
| (india_bounds[2], india_bounds[1]), # SE | |
| (india_bounds[2], india_bounds[3]), # NE | |
| (india_bounds[0], india_bounds[3]), # NW | |
| (india_bounds[0], india_bounds[1]) # Close | |
| ]) | |
| gdf = gpd.GeoDataFrame([1], geometry=[polygon], crs="EPSG:4326") | |
| gdf['NAME'] = 'India' | |
| gdf.to_file(shapefile_path) | |
| print(f"β Created fallback boundary at {shapefile_path}") | |
| return True | |
| except Exception as e2: | |
| print(f"β Failed to create fallback: {e2}") | |
| return False | |
| if __name__ == "__main__": | |
| print("π Starting CAMS Pollution Dashboard...") | |
| print("Checking required files...") | |
| if ensure_shapefile_exists(): | |
| print("β All required files are ready") | |
| print("Starting Flask application...") | |
| # Import and run the main app | |
| from app import app | |
| app.run(host='0.0.0.0', port=7860, debug=False) | |
| else: | |
| print("β Failed to ensure required files exist") | |
| sys.exit(1) |