Skip to main content

Django GraphQL GeoJSON

Pypi Wheel Build Status Codecov Code Climate

GeoJSON support for Django GraphQL

Dependencies

  • Python ≥ 3.4

  • Django ≥ 1.11

Installation

Install last stable version from Pypi.

pip install django-graphql-geojson

GeoJSONType

GeoJSONType is a subclass of DjangoObjectType which provides GraphQL fields in GeoJSON format.

Just define a Meta.geojson_field to be represented as a Geometry type.

models.py

from django.contrib.gis.db import models


class Place(models.Model):
    name = models.CharField(max_length=255)
    location = models.PointField()

schema.py

import graphene
import graphql_geojson


class PlaceType(graphql_geojson.GeoJSONType):

    class Meta:
        model = models.Place
        geojson_field = 'location'


class Query(graphene.ObjectType):
    places = graphene.List(PlaceType)


schema = graphene.Schema(query=Query)

Query

query {
  places {
    id
    type
    geometry {
      type
      coordinates
    }
    bbox
    properties {
      name
    }
  }
}

Geometry Type

Geometry is a special GraphQL type that represents a GEOS geometry object.

schema.py

import graphene
import graphql_geojson


class CreatePlace(graphene.Mutation):
    place = graphene.Field(types.PlaceType)

    class Arguments:
        name = graphene.String(required=True)
        location = graphql_geojson.Geometry(required=True)

    @classmethod
    def mutate(cls, root, info, **args):
        place = models.Place.objects.create(**args)
        return cls(place=place)

Mutation

mutation CreatePlace($name: String!, $location: Geometry!) {
  createPlace(name: $name, location: $location) {
    place {
      id
    }
  }
}

Geometry type may be initialized in a few ways:

  • Well-known text (WKT):

"POINT(5 23)"
  • Hexadecimal (HEX):

"010100000000000000000014400000000000003740"
  • GeoJSON:

{
  "type": "Point",
  "coordinates": [5, 23]
}

GeometryFilterSet

Django GraphQL GeoJSON provides a custom FilterSet for spatial lookups.

The Meta.fields option is combined with model to automatically generate filters.

filters.py

from graphql_geojson.filters import GeometryFilterSet


class PlaceFilter(GeometryFilterSet):

    class Meta:
        model = models.Place
        fields = {
            'name': ['exact'],
            'location': ['exact', 'intersects', 'distance_lte'],
        }

schema.py

import graphene
import graphql_geojson
from graphene import relay
from graphene_django.filter import DjangoFilterConnectionField


class PlaceNode(graphql_geojson.GeoJSONType):

    class Meta:
        model = Place
        interfaces = [relay.Node]
        geojson_field = 'location'


class Query(graphene.ObjectType):
    places = DjangoFilterConnectionField(
        PlaceNode,
        filterset_class=PlaceFilter)

Query

query Places($geometry: Geometry!){
  places(location_Intersects: $geometry) {
    edges {
      node {
        id
      }
    }
  }
}

Distance lookups take a Distance parameter comprising:

  • The desired unit attribute name

  • Distance value

  • A geometry to base calculations from

query Places(
    $unit: DistanceUnitEnum!,
    $value: Float!,
    $geometry: Geometry!)
  {
  places(location_DistanceLte: {
      unit: $unit,
      value: $value,
      geometry: $geometry
    }) {
    edges {
      node {
        id
      }
    }
  }
}

Metadata

Release files for django-graphql-geojson 0.1.4

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

Source distribution (sdist)

Source distribution for django-graphql-geojson 0.1.4
File Size Uploaded
django-graphql-geojson-0.1.4.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-graphql-geojson 0.1.4
File Interpreter ABI Platform
django_graphql_geojson-0.1.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 18.4 kB

Release files / django-graphql-geojson-0.1.4.tar.gz

Download URL django-graphql-geojson-0.1.4.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
08acffff153316f8b0ab4ac7a621b9981a2f2bc4eda4539bf0997ba83d42c2ed
BLAKE2b-256 checksum
How to use checksums
62df87780966739beb216848e7d2c6892352b7ecf1d9720b06d05ad2b65d58b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_graphql_geojson-0.1.4-py2.py3-none-any.whl

Download URL django_graphql_geojson-0.1.4-py2.py3-none-any.whl
Size 10.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
dbd4b940d869cf5a0f7a969d64684e41bd60695b36328c33d52f3f1364de4ef8
BLAKE2b-256 checksum
How to use checksums
3ab33d52c2f6562abc637b4e44dc3ee503da407617753a5117c18f9ec51c651b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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