Skip to content

Commit

Permalink
dropping wkb
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer committed Aug 2, 2023
1 parent b1fd34b commit 0179eb1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 62 deletions.
15 changes: 0 additions & 15 deletions src/django_oapif/db.py

This file was deleted.

49 changes: 18 additions & 31 deletions src/django_oapif/decorators.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import json
from typing import Any, Callable, Dict, Optional

from django.contrib.gis.db.models.functions import AsWKB
from django.db import connection
from django.db.models import Model
from django.http import StreamingHttpResponse
from psycopg2 import sql
from rest_framework import viewsets
from rest_framework_gis.serializers import GeoFeatureModelSerializer

from django_oapif.db import mk_gen_items
from django_oapif.pagination import OapifPagination

from .filters import BboxFilterBackend
from .mixins import OAPIFDescribeModelViewSetMixin
from .urls import oapif_router
Expand Down Expand Up @@ -82,43 +77,35 @@ class Viewset(OAPIFDescribeModelViewSetMixin, viewsets.ModelViewSet):
# Handling BBOX requirements
filter_backends = [BboxFilterBackend]

# Pagination
pagination_class = OapifPagination

# Allowing '.' and '-' in urls
lookup_value_regex = r"[\w.-]+"

def list(self, request):
# Override list to support downloading items with their geometric
# field as WKB, or alternatively, download just the geometries as FlatGeoBuf
# Override list to support downloading geometry
# in items as FlatGeoBuf
format = self.request.GET.get("format")

if not format or format == "json":
if format != "fgb":
return super().list(request)

queryset = self.get_queryset()

if format == "wkb":
queryset = queryset.annotate(wkb=AsWKB("geom"))
get_geom = lambda v: bytes(v.wkb)
gen_items = mk_gen_items(queryset, get_geom)
iterable = (json.dumps(item) for item in gen_items)

elif format == "fgb":
table_name = Model._meta.db_table
pks = tuple(queryset.values_list("id", flat=True))
query = sql.SQL(
"""
WITH rows AS (SELECT geom FROM {table} WHERE id IN %s)
SELECT encode(ST_AsFlatGeobuf(rows), 'base64') FROM rows
queryset = self.filter_queryset(self.get_queryset())
table_name = Model._meta.db_table
pks = set(queryset.values_list("id", flat=True))
query = sql.SQL(
"""
).format(table=sql.Identifier(table_name))
WITH rows AS (SELECT geom FROM {table} WHERE id IN %s)
SELECT encode(ST_AsFlatGeobuf(rows), 'base64') FROM rows
"""
).format(table=sql.Identifier(table_name))

with connection.cursor() as cur:
cur.execute(query, (pks,))

with connection.cursor() as cur:
cur.execute(query, (pks,))
iterable = cur.fetchall()
def stream_from_cursor():
while not cur.closed:
yield cur.fetchmany()

return StreamingHttpResponse(iterable)
return StreamingHttpResponse(stream_from_cursor())

# Apply custom serializer attributes
for k, v in custom_serializer_attrs.items():
Expand Down
16 changes: 0 additions & 16 deletions src/signalo/roads/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.gis.db.models.functions import AsWKB
from django.core.management import call_command
from django.db import connection
from psycopg2 import sql
Expand All @@ -17,21 +16,6 @@ def test_valid(self):
with connection.cursor() as cur:
cur.execute(query)

def test_geom_wkb_with_geodjango(self):
wkbs = Road.objects.annotate(wkb=AsWKB("geom")).values_list("wkb", flat=True)
self.assertGreater(len(wkbs), 1000)

def test_geom_wkb(self):
instances = Road.objects.all()[:1000]
ids = tuple([instance.id for instance in instances])
with connection.cursor() as cur:
query = sql.SQL(
"SELECT ST_AsBinary(geom) FROM {table} WHERE id IN %s"
).format(table=sql.Identifier(Road._meta.db_table))
cur.execute(query, (ids,))
entries = cur.fetchall()
self.assertEqual(len(entries), 1000)

def test_geom_fgb(self):
instances = Road.objects.all()[:1000]
pks = tuple([instance.id for instance in instances])
Expand Down

0 comments on commit 0179eb1

Please sign in to comment.