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

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 graphql_geojson


class PlaceType(graphql_geojson.GeoJSONType):

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

Query

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

Geometry Type

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.000000,
    23.000000
  ]
}

GeometryFilterSet

filters.py

from graphql_geojson.filters import GeometryFilterSet


class PlaceFilter(GeometryFilterSet):

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

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

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.0.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

django_graphql_geojson-0.0.3-py2.py3-none-any.whl (7.5 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

File hashes

Hashes for django-graphql-geojson-0.0.3.tar.gz
Algorithm Hash digest
SHA256 7516d65bb6f70188567e85977ac8f2201903b4778cded6499816c5fe448ab465
MD5 fc9cd80108efc03d5e57536ad45e0cfb
BLAKE2b-256 77f499b63cae68a6fd28efa0bb7ae8eebc4c6ad9f0cd1081b0114bbdf2171d61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_graphql_geojson-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1b1e8bc66c02ab50f4c7e0d3d0eed5d1714863fb62c091f310340eb5060bf7ba
MD5 a95a63bde5370bdb1ee019bdf5243b1b
BLAKE2b-256 3b09e19f9b77e65a4bca4421552ff8f21990e9716a551c505b53749461642d9f

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