# postgresql-geometry
Types for PostgreSQL geometry datatypes, including Django integration.
## Supported types
### Point
```python
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).
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
import factory
from postgresql_geometry.faker import GeometryProvider
factory.Faker.add_provider(GeometryProvider)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/kozlek/postgresql-geometry",
"name": "postgresql-geometry",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": "django, geometry, postgresql",
"author": "Thomas Berdy",
"author_email": "thomas.berdy@outlook.com",
"download_url": "https://files.pythonhosted.org/packages/1a/eb/8f44a9e67a2173822e6720493ff9b11aaeed78bc1567b4eaeb77b4d65035/postgresql_geometry-0.1.1.tar.gz",
"platform": null,
"description": "# postgresql-geometry\n\nTypes for PostgreSQL geometry datatypes, including Django integration.\n\n## Supported types\n\n### Point\n\n```python\nfrom postgresql_geometry import Point\n\npoint = Point(2.3463463, 48.881885)\nprint(point.longitude, point.latitude)\n```\n\n## Django integration\n\n### Point type\n\nDeclare field mapped to a `point` datatype on PostgreSQL side (support migrations).\n\n```python\nfrom django.db import models\n\nfrom postgresql_geometry import Point\nfrom postgresql_geometry.django.models import PointField\n\n\nclass User(models.Model):\n location: Point = PointField()\n```\n\n\n### Point distance\n\nIncluded `PointDistance` function allows approximate distance calculation (in kilometers)\nbetween two points.\n\n```python\nfrom django.db import models\n\nfrom postgresql_geometry import Point\nfrom postgresql_geometry.django.models import PointDistance, PointField\n\n\nclass User(models.Model):\n location: Point = PointField()\n\n\nclass Store(models.Model):\n location: Point = PointField()\n\n\nstore = Store(location=Point(2.3463463, 48.881885))\nuser = User(location=Point(3.3463463, 42.881885))\n\nqs = User.objects.annotate(\n store_distance=PointDistance(\n models.F(\"location\"),\n models.Value(store.location, output_field=PointField()),\n ),\n)\nprint(qs.first().store_distance)\n```\n\n\u26a0\ufe0f This function requires `cube` and `earthdistance` built-in extensions to be created first !\nIf you manage PG's extensions using Django migrations, you can add the provided operations to your migration file.\n\n```python\nfrom django.db import migrations\n\nfrom postgresql_geometry.django.models.operations import CubeExtension, EarthDistanceExtension\n\n\nclass Migration(migrations.Migration):\n ...\n\n operations = [\n CubeExtension(),\n EarthDistanceExtension(),\n ]\n```\n\n## DRF integration\n\nTo enable `ModelSerializer` field auto-detection for extra fields, you can add them to\nthe default mapping.\n\n```python\nfrom postgresql_geometry.django.models import PointField\nfrom postgresql_geometry.rest_framework.fields import PointField as PointSerializerField\nfrom rest_framework import serializers\n\nserializers.ModelSerializer.serializer_field_mapping |= {\n PointField: PointSerializerField,\n}\n```\n\n### PointField (serializers)\n\nProvide a serializer field for Django's `PointField`.\nPoint will be serialized into a list of 2 float.\n\n```python\nfrom postgresql_geometry.rest_framework.fields import PointField\nfrom rest_framework import serializers\n\n\nclass MySerializer(serializers.Serializer):\n coordinates = PointField()\n```\n\n## Faker integration\n\nIf you want to quickly generate random `Point` instance(s), you can use the included\n`Faker` provider.\n\n```python\nfrom faker import Faker\nfrom postgresql_geometry.faker import GeometryProvider\n\nfake = Faker()\nfake.add_provider(GeometryProvider)\n\npoint = fake.point()\nprint(point)\n```\n\nIf you use `factory-boy`, it's even easier to integrate with the inner `Faker` instance.\n\n```python\nimport factory\nfrom postgresql_geometry.faker import GeometryProvider\n\nfactory.Faker.add_provider(GeometryProvider)\n```",
"bugtrack_url": null,
"license": "MIT",
"summary": "PostgreSQL geometry types for python",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/kozlek/postgresql-geometry",
"Repository": "https://github.com/kozlek/postgresql-geometry"
},
"split_keywords": [
"django",
" geometry",
" postgresql"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d4edff1ecd04dd975fdb04f40f96ec5b94e913e6d945e6835df1c364d9dbfec8",
"md5": "4b3b369e339403d0a4e18abb7b15f630",
"sha256": "6fc31c340e62b719541f931580d4c0781d0791a8d92617fe92841dbaa21a373f"
},
"downloads": -1,
"filename": "postgresql_geometry-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4b3b369e339403d0a4e18abb7b15f630",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 6378,
"upload_time": "2024-05-03T13:11:44",
"upload_time_iso_8601": "2024-05-03T13:11:44.963588Z",
"url": "https://files.pythonhosted.org/packages/d4/ed/ff1ecd04dd975fdb04f40f96ec5b94e913e6d945e6835df1c364d9dbfec8/postgresql_geometry-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1aeb8f44a9e67a2173822e6720493ff9b11aaeed78bc1567b4eaeb77b4d65035",
"md5": "48fa55e7300fb3241debe708032b7d53",
"sha256": "8b0730b4805cc0c71b68841c3888a7ce225e88b7b4360657b4b94d6b230533dd"
},
"downloads": -1,
"filename": "postgresql_geometry-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "48fa55e7300fb3241debe708032b7d53",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 5043,
"upload_time": "2024-05-03T13:11:46",
"upload_time_iso_8601": "2024-05-03T13:11:46.293495Z",
"url": "https://files.pythonhosted.org/packages/1a/eb/8f44a9e67a2173822e6720493ff9b11aaeed78bc1567b4eaeb77b4d65035/postgresql_geometry-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-03 13:11:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kozlek",
"github_project": "postgresql-geometry",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "postgresql-geometry"
}