Skip to content

Commit

Permalink
Changed way how extra area is calculated
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Rusakov authored and lanseg committed Dec 24, 2024
1 parent 9775283 commit 48ba4bc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.3 on 2024-12-20 15:20
# Generated by Django 5.1.3 on 2024-12-23 11:39

import django.contrib.gis.db.models.fields
import django.contrib.gis.geos.collections
Expand All @@ -17,8 +17,8 @@ class Migration(migrations.Migration):
operations = [
migrations.AddField(
model_name='order',
name='actualGeom',
field=django.contrib.gis.db.models.fields.PolygonField(null=True, srid=2056, verbose_name='actualGeom'),
name='excludedGeom',
field=django.contrib.gis.db.models.fields.PolygonField(null=True, srid=2056, verbose_name='excludedGeom'),
),
migrations.AlterField(
model_name='orderitem',
Expand Down
2 changes: 1 addition & 1 deletion api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ class OrderStatus(models.TextChoices):
null=True,
)
geom = models.PolygonField(_("geom"), srid=settings.DEFAULT_SRID)
actualGeom = models.PolygonField(_("actualGeom"), srid=settings.DEFAULT_SRID, null=True)
excludedGeom = models.PolygonField(_("excludedGeom"), srid=settings.DEFAULT_SRID, null=True)

client = models.ForeignKey(
UserModel, models.PROTECT, verbose_name=_("client"), blank=True
Expand Down
11 changes: 5 additions & 6 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,14 @@ def validate(self, attrs):
ownedAreas = MultiPolygon(srid=settings.DEFAULT_SRID)
for area in relevantOwnedAreas:
ownedAreas = ownedAreas.union(area.geom)
ownedRequestedGeom = requestedGeom.intersection(ownedAreas)
attrs['actualGeom'] = ownedRequestedGeom

if (round(ownedRequestedGeom.area) == 0 and
requestedGeom.area > settings.MAX_ORDER_AREA):
unownedAreas = requestedGeom.difference(ownedAreas)
attrs['excludedGeom'] = unownedAreas
if (unownedAreas.area > settings.MAX_ORDER_AREA):
raise ValidationError({
'message': _(f'Order area is too large'),
'expected': settings.MAX_ORDER_AREA,
'actual': requestedGeom.area
'actual': requestedGeom.area,
'excluded': unownedAreas.area
})

return attrs
Expand Down
30 changes: 18 additions & 12 deletions api/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,32 @@ def __init__(self, webclient=None):
user_group=Group.objects.create(name="zurich_data_owner"),
product=self.products['free'],
geom=MultiPolygon([Polygon([
[8.472347, 47.364641], [8.472347, 47.404275],
[8.576202, 47.404275], [8.576202, 47.364641],
[8.472347, 47.364641]
])], srid=4326))
[2678084.641714959, 1246491.459194262],
[2678026.5975109423, 1250897.6804795086],
[2685865.173103665, 1251006.1324528102],
[2685929.053569935, 1246599.9914154143],
[2678084.641714959, 1246491.459194262]
])]))
self.lausanneDataOwner = ProductOwnership.objects.create(
user_group=Group.objects.create(name="lausanne_data_owner"),
product=self.products['free'],
geom=MultiPolygon([Polygon([
[6.59008, 46.500283], [6.59008, 46.551542],
[6.694794, 46.551542], [6.694794, 46.500283],
[6.59008, 46.500283]
])], srid=4326))
[2534861.402730483, 1150239.0030801909],
[2534923.142683635, 1155936.9007802252],
[2542953.5027929996, 1155855.250616442],
[2542899.372803648, 1150157.2750742848],
[2534861.402730483, 1150239.0030801909]
])]))
self.switzerlandDataOwner = ProductOwnership.objects.create(
user_group=Group.objects.create(name="switzerland_data_owner"),
product=self.products['free'],
geom=MultiPolygon([Polygon([
[5.50415, 45.713851], [5.50415, 47.857403],
[10.667725, 47.857403], [10.667725, 45.713851],
[5.50415, 45.713851]
])], srid=4326))
[2449355.7225977806, 1064320.9468696574],
[2455237.7729970617, 1302556.4191441573],
[2841601.3432626957, 1305741.504752999],
[2851408.7686695675, 1067635.8085796747],
[2449355.7225977806, 1064320.9468696574]
])]))

zurichOwners = self.zurichDataOwner.user_group
zurichOwners.user_set.add(self.user_private)
Expand Down
25 changes: 15 additions & 10 deletions api/tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def test_order_geom_is_fine(self):
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)

@override_settings(MAX_ORDER_AREA = 100)
def test_order_owned_contains(self):
def test_order_owned_noExcluded(self):
url = reverse('order-list')
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.config.client_token)
self.order_data['items'] = [{
Expand All @@ -507,11 +507,11 @@ def test_order_owned_contains(self):
response = self.client.post(url, self.order_data, format='json')
order = json.loads(response.content)

self.assertTrue(areasEqual(order["geom"], order["actualGeom"]))
self.assertEqual(len(order["excludedGeom"]["coordinates"]), 0)
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)

@override_settings(MAX_ORDER_AREA = 1000)
def test_order_owned_intersects(self):
@override_settings(MAX_ORDER_AREA = 1001)
def test_order_owned_intersects_toobig(self):
url = reverse('order-list')
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.config.client_token)
self.order_data['items'] = [{
Expand All @@ -528,11 +528,12 @@ def test_order_owned_intersects(self):
]}
response = self.client.post(url, self.order_data, format='json')

order = json.loads(response.content)
self.assertFalse(areasEqual(order["geom"], order["actualGeom"]))
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)
errorDetails = json.loads(response.content)
self.assertEqual(errorDetails['message'], ['Order area is too large'])
self.assertEqual(errorDetails['expected'], ['1001'])
self.assertTrue(errorDetails['actual'][0].startswith('203800502.0'))

@override_settings(MAX_ORDER_AREA = 100)
@override_settings(MAX_ORDER_AREA = 10000)
def test_order_unowned_limited(self):
url = reverse('order-list')
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.config.client_token)
Expand All @@ -542,9 +543,13 @@ def test_order_unowned_limited(self):
self.order_data['geom'] = {
'type': 'Polygon',
'coordinates': [
[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]
[[2682058.9416315095, 1246529.024343783],
[2682052.9914918, 1246958.8119991398],
[2682208.5772218467, 1246960.9680303528],
[2682214.538653401, 1246531.1805305541],
[2682058.9416315095, 1246529.024343783]]
]}
response = self.client.post(url, self.order_data, format='json')
order = json.loads(response.content)
self.assertEqual(len(order["actualGeom"]["coordinates"]), 0)
self.assertEqual(len(order["excludedGeom"]["coordinates"]), 1)
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)

0 comments on commit 48ba4bc

Please sign in to comment.