Skip to content

return geometry in CRS84 by default #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ src/django_oapif/__version__.py
static
media
_test_outputs
unit_tests_outputs
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ psycopg2
transifex-client
djangorestframework
djangorestframework-gis
django-postgres-extra
pyyaml
uritemplate
pyproj
49 changes: 36 additions & 13 deletions src/django_oapif/decorators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from os import getenv
from typing import Any, Callable, Dict, Optional

from django.contrib.gis.db.models import Model
from django.contrib.gis.db.models.functions import Transform
from django.contrib.gis.geos import Polygon
from django.db.models import Model
from django.http import HttpResponseBadRequest
from psqlextra.models import PostgresModel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we remove this dependency.

from pyproj import CRS, Transformer
from rest_framework import viewsets
from rest_framework_gis.serializers import GeoFeatureModelSerializer
Expand Down Expand Up @@ -66,12 +68,12 @@ class Meta:

# Create the viewset
class Viewset(OAPIFDescribeModelViewSetMixin, viewsets.ModelViewSet):
queryset = Model.objects.all()
queryset = PostgresModel.objects.all()
serializer_class = viewset_serializer_class

# TODO: these should probably be moved to the mixin
oapif_title = Model._meta.verbose_name
oapif_description = Model.__doc__
oapif_title = PostgresModel._meta.verbose_name
oapif_description = PostgresModel.__doc__

# (one day this will be retrieved automatically from the serializer)
oapif_geom_lookup = viewset_oapif_geom_lookup
Expand All @@ -84,19 +86,26 @@ def get_queryset(self):
# Override get_queryset to catch bbox-crs
queryset = super().get_queryset()

if self.request.GET.get("bbox"):
api_crs = CRS.from_epsg(int(getenv("GEOMETRY_SRID", "2056")))

crs = self.request.GET.get(
"crs", "http://www.opengis.net/def/crs/OGC/1.3/CRS84"
)
bbox = self.request.GET.get("bbox")
do_transform = crs != api_crs

if bbox:
coords = self.request.GET["bbox"].split(",")
user_crs = self.request.GET.get("bbox-crs")
bbox_crs = self.request.GET.get("bbox-crs")

if user_crs:
if bbox_crs:
try:
user_crs = get_crs_from_uri(user_crs)
bbox_crs = get_crs_from_uri(bbox_crs)
except:
return HttpResponseBadRequest(
"This API supports only EPSG-specified CRS. Make sure to use the appropriate value for the `bbox-crs`query parameter."
)
api_crs = CRS.from_epsg(int(getenv("GEOMETRY_SRID", "2056")))
transformer = Transformer.from_crs(user_crs, api_crs)
transformer = Transformer.from_crs(bbox_crs, api_crs)
LL = transformer.transform(coords[0], coords[1])
UR = transformer.transform(coords[2], coords[3])
my_bbox_polygon = Polygon.from_bbox(
Expand All @@ -106,9 +115,23 @@ def get_queryset(self):
else:
my_bbox_polygon = Polygon.from_bbox(coords)

return queryset.filter(geom__intersects=my_bbox_polygon)

return queryset.all()
if do_transform:
return (
queryset.filter(geom__intersects=my_bbox_polygon)
.annotate(geom2=Transform("geom", 4326))
Comment on lines +120 to +121
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
queryset.filter(geom__intersects=my_bbox_polygon)
.annotate(geom2=Transform("geom", 4326))
queryset.filter(geom__intersects=my_bbox_polygon)
.defer("geom")
.annotate(geom=Transform("geom", 4326))

.rename_annotation(geom="geom2")
)
else:
return queryset.filter(geom__intersects=my_bbox_polygon)

if do_transform:
return (
queryset.all()
.annotate(geom2=Transform("geom", 4326))
.rename_annotation(geom="geom2")
)
else:
return queryset.all()

# Apply custom serializer attributes
# if viewset_serializer_class.__name__ == "AutoNoGeomSerializer":
Expand Down
7 changes: 6 additions & 1 deletion src/signalo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.postgres",
"psqlextra",
"django.contrib.gis",
"rest_framework",
"rest_framework_gis",
"computedfields",
"debug_toolbar",
]


MIDDLEWARE = [
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.security.SecurityMiddleware",
Expand Down Expand Up @@ -90,7 +93,7 @@

DATABASES = {
"default": {
"ENGINE": "django.contrib.gis.db.backends.postgis",
"ENGINE": "psqlextra.backend",
"NAME": "postgres",
"HOST": "postgres",
"PORT": 5432,
Expand All @@ -99,6 +102,8 @@
}
}

POSTGRES_EXTRA_DB_BACKEND_BASE = "django.contrib.gis.db.backends.postgis"


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
Expand Down