Skip to content

Commit

Permalink
geojson fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegerrits committed Feb 4, 2025
1 parent cb69f36 commit 9e6ebb1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
91 changes: 50 additions & 41 deletions vespadb/observations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def retrieve_list(self, request: Request, *args: Any, **kwargs: Any) -> Response
},
)
@method_decorator(ratelimit(key="ip", rate="60/m", method="GET", block=True))
@action(detail=False, methods=["get"], url_path="dynamic-geojson")
@action(detail=False, methods=["get"], url_path="dynamic-geojson", permission_classes=[AllowAny])
def geojson(self, request: Request) -> HttpResponse:
"""Generate GeoJSON data for the observations."""
try:
Expand All @@ -382,50 +382,59 @@ def geojson(self, request: Request) -> HttpResponse:
cached_data = cache.get(cache_key)
if cached_data:
logger.info("Cache hit - Returning cached response")
return JsonResponse(cached_data, safe=False)

bbox_str = request.GET.get("bbox")
if bbox_str:
try:
bbox_coords = list(map(float, bbox_str.split(",")))
if len(bbox_coords) == BBOX_LENGTH:
xmin, ymin, xmax, ymax = bbox_coords
bbox_wkt = (
f"POLYGON(({xmin} {ymin}, {xmin} {ymax}, {xmax} {ymax}, {xmax} {ymin}, {xmin} {ymin}))"
)
bbox = GEOSGeometry(bbox_wkt, srid=4326)
else:
return HttpResponse("Invalid bbox format", status=status.HTTP_400_BAD_REQUEST)
except ValueError:
return HttpResponse("Invalid bbox values", status=status.HTTP_400_BAD_REQUEST)
response = JsonResponse(cached_data, safe=False)
else:
bbox = None

queryset = self.filter_queryset(self.get_queryset())
bbox_str = request.GET.get("bbox")
if bbox_str:
try:
bbox_coords = list(map(float, bbox_str.split(",")))
if len(bbox_coords) == BBOX_LENGTH:
xmin, ymin, xmax, ymax = bbox_coords
bbox_wkt = (
f"POLYGON(({xmin} {ymin}, {xmin} {ymax}, {xmax} {ymax}, {xmax} {ymin}, {xmin} {ymin}))"
)
bbox = GEOSGeometry(bbox_wkt, srid=4326)
else:
return HttpResponse("Invalid bbox format", status=status.HTTP_400_BAD_REQUEST)
except ValueError:
return HttpResponse("Invalid bbox values", status=status.HTTP_400_BAD_REQUEST)
else:
bbox = None

queryset = self.filter_queryset(self.get_queryset())

if bbox:
queryset = queryset.filter(location__within=bbox)

queryset = queryset.order_by("id").annotate(point=Transform("location", 4326))

features = [
{
"type": "Feature",
"properties": {
"id": obs.id,
"status": "eradicated"
if obs.eradication_result is not None
else "reserved"
if obs.reserved_by
else "default",
},
"geometry": json.loads(obs.location.geojson) if obs.location else None,
}
for obs in queryset
]
geojson_response = {"type": "FeatureCollection", "features": features}
cache.set(cache_key, geojson_response, GEOJSON_REDIS_CACHE_EXPIRATION)
response = JsonResponse(geojson_response)

if bbox:
queryset = queryset.filter(location__within=bbox)
# Add CORS headers manually
response["Access-Control-Allow-Origin"] = request.META.get('HTTP_ORIGIN', '*')
response["Access-Control-Allow-Credentials"] = "true"
response["Access-Control-Allow-Methods"] = "GET, OPTIONS"
response["Access-Control-Allow-Headers"] = "Content-Type, Authorization"

queryset = queryset.order_by("id").annotate(point=Transform("location", 4326))
return response

features = [
{
"type": "Feature",
"properties": {
"id": obs.id,
"status": "eradicated"
if obs.eradication_result is not None
else "reserved"
if obs.reserved_by
else "default",
},
"geometry": json.loads(obs.location.geojson) if obs.location else None,
}
for obs in queryset
]
geojson_response = {"type": "FeatureCollection", "features": features}
cache.set(cache_key, geojson_response, GEOJSON_REDIS_CACHE_EXPIRATION)
return JsonResponse(geojson_response)
except Exception:
logger.exception("An error occurred while generating GeoJSON data")
return HttpResponse(
Expand Down
4 changes: 2 additions & 2 deletions vespadb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

secrets = {
"DJANGO_SECRET_KEY": os.getenv("SECRET_KEY"),
"CORS_ALLOWED_ORIGINS": os.getenv("CORS_ALLOWED_ORIGINS", "http://localhost:3000").split(","),
"CSRF_TRUSTED_ORIGINS": os.getenv("CSRF_TRUSTED_ORIGINS", "http://localhost:3000").split(","),
"CORS_ALLOWED_ORIGINS": os.getenv("CORS_ALLOWED_ORIGINS", "http://localhost:3000").split(",") + ["https://nesten.vespawatch.be", "https://uat-nesten.vespawatch.be"],
"CSRF_TRUSTED_ORIGINS": os.getenv("CSRF_TRUSTED_ORIGINS", "http://localhost:3000").split(",") + ["https://nesten.vespawatch.be", "https://uat-nesten.vespawatch.be"],
"CSRF_COOKIE_DOMAIN": os.getenv("CSRF_COOKIE_DOMAIN", ".vespawatch.be"),
"SESSION_COOKIE_DOMAIN": os.getenv("SESSION_COOKIE_DOMAIN", ".vespawatch.be"),
"POSTGRES_DB": os.getenv("POSTGRES_DB"),
Expand Down

0 comments on commit 9e6ebb1

Please sign in to comment.