# why doesn't Perimeter work with polygons?

**URL:** <https://forum.djangoproject.com/t/why-doesnt-perimeter-work-with-polygons/33670>\
**Category:** GeoDjango\
**Created:** [August 8, 2024, 12:42pm UTC](https://forum.djangoproject.com/t/why-doesnt-perimeter-work-with-polygons/33670 "2024-08-08T12:42:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaHryCT3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahryct3/32/23113_2.png) [@MaHryCT3](https://forum.djangoproject.com/u/MaHryCT3)\
**Post date:** [August 8, 2024, 12:42pm UTC](https://forum.djangoproject.com/t/why-doesnt-perimeter-work-with-polygons/33670/1 "2024-08-08T12:42:50Z")

</div>

I’ll be talking about django.contrib.gis.db.models.functions.Perimeter and the as\_postgresql method

First case with GeneratedField

```auto
from django.contrib.gis.db.models.functions import GeoFunc
from django.db import models
from django.contrib.gis.db.models import PolygonField

class Geography(GeoFunc):
    function = 'geography'

class Crosswalk(models.Model):
    polygon = PolygonField()
    perimeter = models.GeneratedField(
        expression=Perimeter(Geography('polygon')),
        output_field=models.FloatField(),
        db_persist=True,
    )

```

When i try to migrate i get this error

> django.db.utils.NotSupportedError: ST\_Perimeter cannot use a non-projected non-geography field.

Second case with annotate and without Geography

```python
from django.db import models
from django.contrib.gis.db.models import PolygonField

class Crosswalk(models.Model):
    polygon = PolygonField()

Crosswalk.objects.annotate(p=Perimeter('polygon'))

```

And on this case i too get this error

> django.db.utils.NotSupportedError: ST\_Perimeter cannot use a non-projected non-geography field.

But if I make a custom Perimeter function and remove this check, everything works correctly and I get the result.

```auto
class P(Perimeter):
    function = 'st_perimeter'

    def as_postgresql(self, compiler, connection, **extra_context):
        function = None
        dim = min(f.dim for f in self.get_source_fields())
        if dim > 2:
            function = connection.ops.perimeter3d
        return super().as_sql(compiler, connection, function=function, **extra_context)

```

Source: [django/django/contrib/gis/db/models/functions.py at main · django/django · GitHub](https://github.com/django/django/blob/main/django/contrib/gis/db/models/functions.py#L504)

Why does it work that way? Is it a bug?

---

<div class="post-metadata">

**Author:** ![claudep](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/claudep/32/5377_2.png) [@claudep](https://forum.djangoproject.com/u/claudep)\
**Post date:** [August 8, 2024, 1:25pm UTC](https://forum.djangoproject.com/t/why-doesnt-perimeter-work-with-polygons/33670/2 "2024-08-08T13:25:41Z")

</div>

Did you check the result of your Perimeter query? If I’m not mistaken, the result should be in degrees, which is not what is expected.

As per your first question, it is totally possible that Django is not able to determine that the expression final output is in fact `geography`. The check is probably too naive and could probably receive some love to make it smarter.

---

<div class="post-metadata">

**Author:** ![MaHryCT3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahryct3/32/23113_2.png) [@MaHryCT3](https://forum.djangoproject.com/u/MaHryCT3)\
**Post date:** [August 8, 2024, 2:18pm UTC](https://forum.djangoproject.com/t/why-doesnt-perimeter-work-with-polygons/33670/3 "2024-08-08T14:18:36Z")

</div>

> [@claudep](#):
>
> Did you check the result of your Perimeter query? If I’m not mistaken, the result should be in degrees, which is not what is expected.

You’re really right, I apologize. But it’s not really obvious that you can’t find a perimeter with the perimeter function.
