Skip to main content

GeoJSON support for Django GraphQL

Project description

Pypi Wheel Build Status Codecov Code Climate

GeoJSON support for Django GraphQL

Dependencies

  • Python ≥ 3.4

  • Django ≥ 1.11

Installation

Update Graphene Django package.

pip install -U git+https://github.com/graphql-python/graphene-django.git@master

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
      }
    }
  }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-graphql-geojson-0.1.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

django_graphql_geojson-0.1.2-py2.py3-none-any.whl (10.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-graphql-geojson-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django-graphql-geojson-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ffc6bef2ffc5e7796f2f4f10690ae07eaa10f5f5e5796976a7010feb3c8ba4c8
MD5 7f082e7f9de93174ae1d0c3691111c48
BLAKE2b-256 125de872a809a9ebc16398729df20ea0416c5382a2ed4d79eae172c6c06b7624

See more details on using hashes here.

File details

Details for the file django_graphql_geojson-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_graphql_geojson-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5ab92bdd2ac0b82b84a7c33709410b5f5a22ba6587b6b9e33dd7df231343b867
MD5 735332950bc8583f5bd13c8ff999ea5a
BLAKE2b-256 69e55ccd2e936a3f299fba03c20d49dd8b32ba924e885a700bc10b855d82a302

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page