GithubHelp home page GithubHelp logo

everwinter23 / graphene-gis Goto Github PK

View Code? Open in Web Editor NEW
37.0 4.0 9.0 81 KB

A geojson serializer for graphene-- python's graphql library.

License: MIT License

Makefile 1.12% Python 98.88%
graphene graphql django gis postgis python

graphene-gis's Introduction

graphene-gis

CircleCI Monthly Downloads

INSTALLATION

Django support depends on graphene-django. Install the graphene-gis with pip:

$ pip install graphene-gis

Make sure that you have appropriate driver to interact with postgis-- psycopg2 or psycopg2-binary. The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources. More info here.

Add it to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    'graphene_gis',
]

USAGE

Hi, check this-> geoql project out, it demonstrates usage-- such as querying, mutations using WKT and geojson. I will be adding more stuff soon such as containerization, interactive UI etc, and more examples that showcases the library. This project provides an insight into real-world usage of the library, do check it out.

This extension can works out of the box with WKT, but if you want to use GeoJSON for input while mutations, install rest_framework_gis alongside it-- or check out geoql sample project.

QUERY

models.py

from django.contrib.gis.db import models


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

schema.py

from graphene_django import DjangoObjectType
from graphene_gis.converter import gis_converter  # noqa

class PlaceType(DjangoObjectType):
    class Meta:
        model = Place

class Query(graphene.ObjectType):
    place = graphene.Field(Place)

    def resolve_place(self, info):
        return Place(name="San Andreas", location="POINT(34.2 54.3)")

schema = graphene.Schema(query=Query)

Query

query {
    place {
        name
        location
    }
}

Query Output

"place": {
    "name": "San Andreas",
    "location": {
        "type": "Point",
        "coordinates": [34.2, 54.3]
    }
}

MUTATION

schema.py

class PointModelType(graphene.ObjectType):
    location = graphene.Field(graphene.String, to=scalars.PointScalar())

class CreatePointModelType(graphene.Mutation):
    point = graphene.Field(PointModelType)

    class Arguments:
        location = graphene.Argument(scalars.PointScalar)

    def mutate(root, info, location):
        point = PointModelType(location=location)
        return CreatePointModelType(point=point)

Mutation

mutation {
    createPoint (location: "POINT(3 5)") {
        point {
            location
        }
    }
}

Mutation Output

"createPoint": {
    "point": {
        "location": "{'type': 'Point', 'coordinates': [3.0, 5.0]}"
    }
}

EXTRA STUFF

A JSON Converter, so if you're familiar with graphene, you know that it sends JSONField as stringified JSON, but with a lot of data, you dont want to parse it in the frontend, I know it goes against having a static type, but if you're not modifying the data on the frontend, plus you're using typescript which enforces types anyway, it works like a charm.

And geojson contains JSONField like properties section, and parsing every node in the frontend is cumbersome if you have ~9000 entries, also time consuming.

Output without using json_converter

{
  "data": {
    "vectors": [
      {
        "type": "Feature",
        "properties": "{\"Name\": \"Blues\", \"area\": 0.0006971253332413299, \"bbox\": [74.59639001261124, 24.7077612714826, 74.61615129922414, 24.755648349214077], \"perimeter\": 0.15862406542812008}",
        "geometry": {
          "type": "Polygon",
          "coordinates": [...]
        }
      }
    ]
  }
}

Now if you're working with GeoJSON, you're not working with just one vector, you're probably working with thousands. Voila json_converter!!! Now you can plot it directly, if you store it in such a way! I won't go into how to structure the model, but this is fairly accurate description of GeoJSON, and anyone familiar with django will be able to reproduce it without issues.

{
  "data": {
    "allVectors": [
      {
        "type": "Feature",
        "properties": {
          "Name": "Blues",
          "area": 0.0006971253332413299,
          "bbox": [
            74.59639001261124,
            24.7077612714826,
            74.61615129922414,
            24.755648349214077
          ],
          "perimeter": 0.15862406542812008
        },
        "geometry": {
          "type": "Polygon",
          "coordinates": [...]
        }
      }
    ]
  }
}

AUTHOR

Rishabh Mehta [email protected]

If you have any issues or queries regarding acadbot, please don't hesitate to email the @author. I have a lot of free time.

I forget stuff, this section is for anyone who wants to build the package.

$ python setup.py sdist
$ twine upload dist/*

UPDATE

  • Targeting graphene-v3 update by March'22 -> MR

  • Install the pre-release using:

  • Django 4.2 LTS support by May'23

    pip install graphene-gis==0.0.8b0

LICENSE License: MIT

This code falls under the MIT license which permits the reuse of the proprietary software provided that all copies of the licensed software include a copy of the MIT License terms and the copyright notice. Go crazy!

graphene-gis's People

Contributors

dependabot[bot] avatar everwinter23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

graphene-gis's Issues

Too strict version dependencies

Since this is a library, rather than pinning dependency versions to exact numbers, it should use version ranges. As it is, we can't use it in our project:

There are incompatible versions in the resolved dependencies:
  graphene<4,>=3.0.0b1 (from graphene-django==3.0.0b1->-r requirements.in (line 12))
  graphene==2.1.8 (from graphene-gis==0.0.4->-r requirements.in (line 14))

Unable to update graphene to v3

I'm unable to update graphene to v3 with the current published version of graphene-gis (0.0.7):

Because graphene-gis (0.0.7) depends on graphene (>=2.1,<3)
 and no versions of graphene-gis match >0.0.7,<0.0.8, graphene-gis (>=0.0.7,<0.0.8) requires graphene (>=2.1,<3).
So, because app depends on both graphene (^3.0.0) and graphene-gis (^0.0.7), version solving failed.

^ error message from Poetry

Graphene 3.0 Support

I see that you are target Graphene 3.0 support by March of '22. What needs to be done to enable this support? Do you have a checklist?

Issue storing WKT Polygon In PostGIS

In following the documentation, I created the following:

In postgres, the table is set up as:

create table mydb.location
(
	location_id uuid not null
		constraint location_pkey
			primary key,
	location_drawn_area mydb.geometry(Polygon,4326) not null,

);

create index location_location_drawn_area_id
	on mydb.location using gist (location_drawn_area);
#model
import uuid
from django.contrib.gis.db import models

class Location(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    drawn_area = models.PolygonField(db_column="location_drawn_area", null=False)
#schema
import graphene
from graphene_django import DjangoObjectType
from graphene_gis.converter import gis_converter
from graphene_gis.scalars import PolygonScalar

class PolygonModelType(graphene.ObjectType):
    drawn_area = graphene.Field(graphene.String, to=PolygonScalar()


class LocationType(DjangoObjectType):
    class Meta:
    model = Location
    fields = ("id", "drawn_area")
    interfaces = (graphene.relay.Node, )

class LocationCreateMutation(graphene.Mutation):
    class Arguments:
        drawn_area = graphene.Argument(PolygonScalar)

    location = graphene.Field(LocationType)

    def mutate(self, info, drawn_area):
        drawn_area = PolygonModelType(drawn_area=drawn_area)

        location = Location()
        location.drawn_area = drawn_area
        location.save()
        return  LocationCreateMutation(location=location)

When I try to create a mutation by providing a WKT Polygon

mutation{addLocation( drawnArea:"POLYGON((-78.6207918152582 35.5963473238645,-78.6206335649117 35.5965828721579,-78.6205316409598 35.5966831980725))"){location{id}}

I get the following error:

Cannot set Location SpatialProxy (POLYGON) with value of type: PolygonModelType

I'm not sure what I'm doing wrong or if it may be a bug.

Installation error

i fully realize this may be my own environment, but I am not clear of the issue - while trying the pip install, i get the following verbose error (attached)
given this line

ld: library not found for -lssl

i assumed this was some kind of XCode dependency - even after an update i have not love
I am working from a virtual environment...perhaps that is missing something?
installerror.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.