PostgreSQL geometry types for python
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file postgresql_geometry-0.1.1.tar.gz
.
File metadata
- Download URL: postgresql_geometry-0.1.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b0730b4805cc0c71b68841c3888a7ce225e88b7b4360657b4b94d6b230533dd |
|
MD5 | 48fa55e7300fb3241debe708032b7d53 |
|
BLAKE2b-256 | 1aeb8f44a9e67a2173822e6720493ff9b11aaeed78bc1567b4eaeb77b4d65035 |
File details
Details for the file postgresql_geometry-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: postgresql_geometry-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fc31c340e62b719541f931580d4c0781d0791a8d92617fe92841dbaa21a373f |
|
MD5 | 4b3b369e339403d0a4e18abb7b15f630 |
|
BLAKE2b-256 | d4edff1ecd04dd975fdb04f40f96ec5b94e913e6d945e6835df1c364d9dbfec8 |