aditya-me13 commited on
Commit
c110c9c
Β·
1 Parent(s): 8d4f01a

Fix shapefile loading issue - add startup script with fallback downloads

Browse files
Files changed (4) hide show
  1. Dockerfile +4 -1
  2. plot_generator.py +1 -1
  3. requirements.txt +2 -1
  4. startup.py +118 -0
Dockerfile CHANGED
@@ -16,6 +16,9 @@ RUN apt-get update && apt-get install -y \
16
  libspatialindex-dev \
17
  libffi-dev \
18
  git \
 
 
 
19
  && rm -rf /var/lib/apt/lists/*
20
 
21
  # Set environment variables for GDAL
@@ -49,4 +52,4 @@ USER user
49
 
50
  # Command to run the application
51
  # Note: Hugging Face Spaces expects the app to run on port 7860
52
- CMD ["python", "-c", "import sys; sys.path.append('.'); from app import app; app.run(host='0.0.0.0', port=7860, debug=False)"]
 
16
  libspatialindex-dev \
17
  libffi-dev \
18
  git \
19
+ git-lfs \
20
+ curl \
21
+ wget \
22
  && rm -rf /var/lib/apt/lists/*
23
 
24
  # Set environment variables for GDAL
 
52
 
53
  # Command to run the application
54
  # Note: Hugging Face Spaces expects the app to run on port 7860
55
+ CMD ["python", "startup.py"]
plot_generator.py CHANGED
@@ -20,7 +20,7 @@ class IndiaMapPlotter:
20
 
21
  Parameters:
22
  plots_dir (str): Directory to save plots
23
- shapefile_path (str): Path to the India districts shapefile
24
  """
25
  self.plots_dir = Path(plots_dir)
26
  self.plots_dir.mkdir(exist_ok=True)
 
20
 
21
  Parameters:
22
  plots_dir (str): Directory to save plots
23
+ shapefile_path (str): Path to India boundary shapefile
24
  """
25
  self.plots_dir = Path(plots_dir)
26
  self.plots_dir.mkdir(exist_ok=True)
requirements.txt CHANGED
@@ -11,4 +11,5 @@ jinja2==3.1.2
11
  python-dateutil==2.8.2
12
  plotly==6.3.0
13
  kaleido
14
- geopandas
 
 
11
  python-dateutil==2.8.2
12
  plotly==6.3.0
13
  kaleido
14
+ geopandas
15
+ shapely
startup.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Startup script to ensure all required files are available before running the main app
4
+ """
5
+ import os
6
+ import sys
7
+ import subprocess
8
+ import urllib.request
9
+ import zipfile
10
+ import geopandas as gpd
11
+ from pathlib import Path
12
+
13
+ def ensure_shapefile_exists():
14
+ """Ensure the India State Boundary shapefile exists"""
15
+ shapefile_path = "shapefiles/India_State_Boundary.shp"
16
+
17
+ if os.path.exists(shapefile_path):
18
+ print(f"βœ“ Shapefile found at {shapefile_path}")
19
+ return True
20
+
21
+ print(f"βœ— Shapefile not found at {shapefile_path}")
22
+
23
+ # Try to pull from git lfs if available
24
+ try:
25
+ if os.path.exists(".git"):
26
+ print("Attempting to pull LFS files...")
27
+ result = subprocess.run(["git", "lfs", "pull"], capture_output=True, text=True)
28
+ if result.returncode == 0 and os.path.exists(shapefile_path):
29
+ print("βœ“ Successfully pulled LFS files")
30
+ return True
31
+ else:
32
+ print("βœ— Git LFS pull failed or file still missing")
33
+ except Exception as e:
34
+ print(f"βœ— Git LFS not available: {e}")
35
+
36
+ # Download alternative shapefile
37
+ print("Downloading alternative India boundary data...")
38
+ try:
39
+ # Create shapefiles directory
40
+ os.makedirs("shapefiles", exist_ok=True)
41
+
42
+ # Download from Natural Earth (reliable source)
43
+ url = "https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"
44
+ zip_path = "temp_countries.zip"
45
+
46
+ print("Downloading Natural Earth country boundaries...")
47
+ urllib.request.urlretrieve(url, zip_path)
48
+
49
+ # Extract
50
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
51
+ zip_ref.extractall("temp_extract")
52
+
53
+ # Load and filter for India
54
+ world_data = gpd.read_file("temp_extract/ne_50m_admin_0_countries.shp")
55
+ india_data = world_data[world_data['NAME'] == 'India']
56
+
57
+ if len(india_data) == 0:
58
+ # Try alternative name matching
59
+ india_data = world_data[world_data['NAME_EN'] == 'India']
60
+
61
+ if len(india_data) > 0:
62
+ # Save as our expected shapefile
63
+ india_data.to_file(shapefile_path)
64
+ print(f"βœ“ Successfully created {shapefile_path}")
65
+ else:
66
+ raise Exception("Could not find India in the world data")
67
+
68
+ # Cleanup
69
+ os.remove(zip_path)
70
+ import shutil
71
+ shutil.rmtree("temp_extract", ignore_errors=True)
72
+
73
+ return True
74
+
75
+ except Exception as e:
76
+ print(f"βœ— Failed to download alternative shapefile: {e}")
77
+
78
+ # Create a simple fallback
79
+ print("Creating fallback boundary...")
80
+ try:
81
+ from shapely.geometry import Polygon
82
+
83
+ # Simple India bounding box
84
+ india_bounds = [68.0, 6.0, 97.5, 37.0] # [min_lon, min_lat, max_lon, max_lat]
85
+
86
+ polygon = Polygon([
87
+ (india_bounds[0], india_bounds[1]), # SW
88
+ (india_bounds[2], india_bounds[1]), # SE
89
+ (india_bounds[2], india_bounds[3]), # NE
90
+ (india_bounds[0], india_bounds[3]), # NW
91
+ (india_bounds[0], india_bounds[1]) # Close
92
+ ])
93
+
94
+ gdf = gpd.GeoDataFrame([1], geometry=[polygon], crs="EPSG:4326")
95
+ gdf['NAME'] = 'India'
96
+ gdf.to_file(shapefile_path)
97
+
98
+ print(f"βœ“ Created fallback boundary at {shapefile_path}")
99
+ return True
100
+
101
+ except Exception as e2:
102
+ print(f"βœ— Failed to create fallback: {e2}")
103
+ return False
104
+
105
+ if __name__ == "__main__":
106
+ print("πŸš€ Starting CAMS Pollution Dashboard...")
107
+ print("Checking required files...")
108
+
109
+ if ensure_shapefile_exists():
110
+ print("βœ“ All required files are ready")
111
+ print("Starting Flask application...")
112
+
113
+ # Import and run the main app
114
+ from app import app
115
+ app.run(host='0.0.0.0', port=7860, debug=False)
116
+ else:
117
+ print("βœ— Failed to ensure required files exist")
118
+ sys.exit(1)