Skip to content

Commit

Permalink
Check file existence and readability before opening files
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishq-sharma-mapup committed Aug 7, 2024
1 parent dd5dc1c commit c0d5a93
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
from multiprocessing import Pool, cpu_count
import os

import pyproj
from shapely.geometry import LineString, Point
Expand All @@ -22,10 +23,24 @@ def setup_logging():
)

def load_geojson(file_path):
# Check if the file exists
if not os.path.exists(file_path):
logging.error(f"File not found: {file_path}")
return None

# Check if the file is readable
if not os.access(file_path, os.R_OK):
logging.error(f"File not readable: {file_path}")
return None

# Try to open and load the file
try:
with open(file_path, "r") as f:
data = json.load(f)
return data
except json.JSONDecodeError as e:
logging.error(f"Error decoding JSON from the file: {e}")
return None
except Exception as e:
logging.error(f"Error loading GeoJSON file: {e}")
return None
Expand Down

0 comments on commit c0d5a93

Please sign in to comment.