Skip to main content

postgresql-geometry

Types for PostgreSQL geometry datatypes, including Django integration.

Supported types

Point

from postgresql_geometry import Point

point = Point(2.3463463, 48.881885)
print(point.longitude, point.latitude)

Django integration

Point type

Declare field mapped to a point datatype on PostgreSQL side (support migrations).

from django.db import models

from postgresql_geometry import Point
from postgresql_geometry.django.models import PointField


class User(models.Model):
    location: Point = PointField()

Point distance

Included PointDistance function allows approximate distance calculation (in kilometers) between two points.

from django.db import models

from postgresql_geometry import Point
from postgresql_geometry.django.models import PointDistance, PointField


class User(models.Model):
    location: Point = PointField()


class Store(models.Model):
    location: Point = PointField()


store = Store(location=Point(2.3463463, 48.881885))
user = User(location=Point(3.3463463, 42.881885))

qs = User.objects.annotate(
    store_distance=PointDistance(
        models.F("location"),
        models.Value(store.location, output_field=PointField()),
    ),
)
print(qs.first().store_distance)

⚠️ This function requires cube and earthdistance built-in extensions to be created first ! If you manage PG's extensions using Django migrations, you can add the provided operations to your migration file.

from django.db import migrations

from postgresql_geometry.django.models.operations import CubeExtension, EarthDistanceExtension


class Migration(migrations.Migration):
    ...

    operations = [
        CubeExtension(),
        EarthDistanceExtension(),
    ]

DRF integration

To enable ModelSerializer field auto-detection for extra fields, you can add them to the default mapping.

from postgresql_geometry.django.models import PointField
from postgresql_geometry.rest_framework.fields import PointField as PointSerializerField
from rest_framework import serializers

serializers.ModelSerializer.serializer_field_mapping |= {
    PointField: PointSerializerField,
}

PointField (serializers)

Provide a serializer field for Django's PointField. Point will be serialized into a list of 2 float.

from postgresql_geometry.rest_framework.fields import PointField
from rest_framework import serializers


class MySerializer(serializers.Serializer):
    coordinates = PointField()

Faker integration

If you want to quickly generate random Point instance(s), you can use the included Faker provider.

from faker import Faker
from postgresql_geometry.faker import GeometryProvider

fake = Faker()
fake.add_provider(GeometryProvider)

point = fake.point()
print(point)

If you use factory-boy, it's even easier to integrate with the inner Faker instance.

import factory
from postgresql_geometry.faker import GeometryProvider

factory.Faker.add_provider(GeometryProvider)

Release files for postgresql-geometry 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for postgresql-geometry 0.1.1
File Size Uploaded
postgresql_geometry-0.1.1.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for postgresql-geometry 0.1.1
File Interpreter ABI Platform
postgresql_geometry-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.4 kB

Release files / postgresql_geometry-0.1.1.tar.gz

Download URL postgresql_geometry-0.1.1.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8b0730b4805cc0c71b68841c3888a7ce225e88b7b4360657b4b94d6b230533dd
BLAKE2b-256 checksum
How to use checksums
1aeb8f44a9e67a2173822e6720493ff9b11aaeed78bc1567b4eaeb77b4d65035
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/21.6.0

Release files / postgresql_geometry-0.1.1-py3-none-any.whl

Download URL postgresql_geometry-0.1.1-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6fc31c340e62b719541f931580d4c0781d0791a8d92617fe92841dbaa21a373f
BLAKE2b-256 checksum
How to use checksums
d4edff1ecd04dd975fdb04f40f96ec5b94e913e6d945e6835df1c364d9dbfec8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page