Skip to content

Commit

Permalink
Filter out reports in the ocean or in the polar circle
Browse files Browse the repository at this point in the history
  • Loading branch information
epou committed Dec 20, 2024
1 parent c9edf7e commit 9f336b2
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 8 deletions.
63 changes: 63 additions & 0 deletions tigaserver_app/migrations/0074_add_report_location_is_masked.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 3.2.25 on 2024-12-18 16:05

from django.conf import settings
from django.db import migrations, models
from django.db.models.expressions import Func
from django.db.models.functions import Abs


def populate_report_location_is_masked(apps, schema_editor):

Report = apps.get_model('tigaserver_app', 'Report')

Report.objects.alias(
latitude=Func(
models.F('point'),
function='ST_Y',
output_field=models.FloatField()
)
).annotate(
latitude_abs=Abs('latitude')
).filter(
models.Q(point__isnull=False)
& (
models.Q(latitude_abs__gt=settings.POLAR_CIRCLE_LATITUDE)
| models.Q(point__within=settings.OCEAN_GEOM)
)
).update(
location_is_masked=True
)


def populate_report_history_location_is_masked(apps, schema_editor):
Report = apps.get_model('tigaserver_app', 'Report')
HistoricalReport = apps.get_model("tigaserver_app", "HistoricalReport")

HistoricalReport.objects.filter(
version_UUID__in=models.Subquery(
Report.objects.filter(location_is_masked=True).values('pk')
)
).update(
location_is_masked=True
)

class Migration(migrations.Migration):

dependencies = [
('tigaserver_app', '0073_tigauser_lastlocation'),
]

operations = [
migrations.AddField(
model_name='report',
name='location_is_masked',
field=models.BooleanField(default=False),
),
migrations.RunPython(populate_report_location_is_masked, migrations.RunPython.noop),
migrations.AddField(
model_name='historicalreport',
name='location_is_masked',
field=models.BooleanField(default=False),
),
migrations.RunPython(populate_report_history_location_is_masked, migrations.RunPython.noop),
]
19 changes: 12 additions & 7 deletions tigaserver_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ class Report(TimeZoneModelMixin, models.Model):
null=True,
help_text="The timezone corresponding to the GPS of the report."
)
location_is_masked = models.BooleanField(default=False)

deleted_at = models.DateTimeField(null=True, blank=True, editable=False, default=None)

Expand Down Expand Up @@ -1964,14 +1965,18 @@ def save(self, *args, **kwargs):
)
)

if self.point and self.country:
nuts3 = self._get_nuts_is_in(levl_code=3)
if nuts3:
self.nuts_3 = nuts3.nuts_id
if self.point:
if self.country:
nuts3 = self._get_nuts_is_in(levl_code=3)
if nuts3:
self.nuts_3 = nuts3.nuts_id

nuts2 = self._get_nuts_is_in(levl_code=2)
if nuts2:
self.nuts_2 = nuts2.nuts_id
nuts2 = self._get_nuts_is_in(levl_code=2)
if nuts2:
self.nuts_2 = nuts2.nuts_id
else:
# Check if masked because of is in the ocean of out of the artic/antartic circle.
self.location_is_masked = settings.OCEAN_GEOM.contains(self.point) or abs(self.point.y) > settings.POLAR_CIRCLE_LATITUDE

bite_fieldnames = [
'head_bite_count',
Expand Down
66 changes: 65 additions & 1 deletion tigaserver_app/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,4 +1556,68 @@ def test_tags_from_note_are_unique(self):

# Check if tags are correctly set by comparing tag names
tag_names = list(report.tags.values_list('name', flat=True))
self.assertEqual(sorted(tag_names), ['tag1', 'tag2'])
self.assertEqual(sorted(tag_names), ['tag1', 'tag2'])

def test_report_above_artic_circle_should_be_marked_as_masked(self):
report = Report.objects.create(
user=TigaUser.objects.create(),
report_id='1234',
phone_upload_time=timezone.now(),
creation_time=timezone.now(),
version_time=timezone.now(),
type=Report.TYPE_ADULT,
location_choice=Report.LOCATION_CURRENT,
current_location_lon=0,
current_location_lat=67,
)
report.refresh_from_db()

self.assertTrue(report.location_is_masked)

def test_report_below_antartic_circle_should_be_marked_as_masked(self):
report = Report.objects.create(
user=TigaUser.objects.create(),
report_id='1234',
phone_upload_time=timezone.now(),
creation_time=timezone.now(),
version_time=timezone.now(),
type=Report.TYPE_ADULT,
location_choice=Report.LOCATION_CURRENT,
current_location_lon=0,
current_location_lat=-67,
)
report.refresh_from_db()

self.assertTrue(report.location_is_masked)

def test_report_in_the_ocean_should_be_marked_as_masked(self):
report = Report.objects.create(
user=TigaUser.objects.create(),
report_id='1234',
phone_upload_time=timezone.now(),
creation_time=timezone.now(),
version_time=timezone.now(),
type=Report.TYPE_ADULT,
location_choice=Report.LOCATION_CURRENT,
current_location_lon=0,
current_location_lat=0,
)
report.refresh_from_db()

self.assertTrue(report.location_is_masked)

def test_report_in_land_should_not_be_marked_as_masked(self):
report = Report.objects.create(
user=TigaUser.objects.create(),
report_id='1234',
phone_upload_time=timezone.now(),
creation_time=timezone.now(),
version_time=timezone.now(),
type=Report.TYPE_ADULT,
location_choice=Report.LOCATION_CURRENT,
current_location_lon=41,
current_location_lat=2,
)
report.refresh_from_db()

self.assertFalse(report.location_is_masked)
Binary file added tigaserver_project/ne_10m_ocean_b8km.gpkg
Binary file not shown.
7 changes: 7 additions & 0 deletions tigaserver_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import django.conf.global_settings as DEFAULT_SETTINGS
import pytz
from datetime import datetime
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.gis.gdal import DataSource

import firebase_admin
from firebase_admin.credentials import Certificate
Expand Down Expand Up @@ -37,6 +39,11 @@

ALLOWED_HOSTS = []

OCEAN_GEOM = GEOSGeometry.from_ewkt(
DataSource(PROJECT_DIR + '/ne_10m_ocean_b8km.gpkg')[0][1].geom.ewkt
)
POLAR_CIRCLE_LATITUDE = 66.5

# Application definition


Expand Down

0 comments on commit 9f336b2

Please sign in to comment.