项目作者: EverWinter23

项目描述 :
A geojson serializer for graphene
高级语言: Python
项目地址: git://github.com/EverWinter23/graphene-gis.git
创建时间: 2020-03-12T17:16:37Z
项目社区:https://github.com/EverWinter23/graphene-gis

开源协议:

下载


graphene-gis

CircleCI
Monthly Downloads

INSTALLATION

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

  1. $ 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:

  1. INSTALLED_APPS = [
  2. ...
  3. 'graphene_gis',
  4. ]

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

  1. from django.contrib.gis.db import models
  2. class Place(models.Model):
  3. name = models.CharField(max_length=255)
  4. location = models.PointField()

schema.py

  1. from graphene_django import DjangoObjectType
  2. from graphene_gis.converter import gis_converter # noqa
  3. class PlaceType(DjangoObjectType):
  4. class Meta:
  5. model = Place
  6. class Query(graphene.ObjectType):
  7. place = graphene.Field(Place)
  8. def resolve_place(self, info):
  9. return Place(name="San Andreas", location="POINT(34.2 54.3)")
  10. schema = graphene.Schema(query=Query)

Query

  1. query {
  2. place {
  3. name
  4. location
  5. }
  6. }

Query Output

  1. "place": {
  2. "name": "San Andreas",
  3. "location": {
  4. "type": "Point",
  5. "coordinates": [34.2, 54.3]
  6. }
  7. }

MUTATION

schema.py

  1. class PointModelType(graphene.ObjectType):
  2. location = graphene.Field(graphene.String, to=scalars.PointScalar())
  3. class CreatePointModelType(graphene.Mutation):
  4. point = graphene.Field(PointModelType)
  5. class Arguments:
  6. location = graphene.Argument(scalars.PointScalar)
  7. def mutate(root, info, location):
  8. point = PointModelType(location=location)
  9. return CreatePointModelType(point=point)

Mutation

  1. mutation {
  2. createPoint (location: "POINT(3 5)") {
  3. point {
  4. location
  5. }
  6. }
  7. }

Mutation Output

  1. "createPoint": {
  2. "point": {
  3. "location": "{'type': 'Point', 'coordinates': [3.0, 5.0]}"
  4. }
  5. }

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

  1. {
  2. "data": {
  3. "vectors": [
  4. {
  5. "type": "Feature",
  6. "properties": "{\"Name\": \"Blues\", \"area\": 0.0006971253332413299, \"bbox\": [74.59639001261124, 24.7077612714826, 74.61615129922414, 24.755648349214077], \"perimeter\": 0.15862406542812008}",
  7. "geometry": {
  8. "type": "Polygon",
  9. "coordinates": [...]
  10. }
  11. }
  12. ]
  13. }
  14. }

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.

  1. {
  2. "data": {
  3. "allVectors": [
  4. {
  5. "type": "Feature",
  6. "properties": {
  7. "Name": "Blues",
  8. "area": 0.0006971253332413299,
  9. "bbox": [
  10. 74.59639001261124,
  11. 24.7077612714826,
  12. 74.61615129922414,
  13. 24.755648349214077
  14. ],
  15. "perimeter": 0.15862406542812008
  16. },
  17. "geometry": {
  18. "type": "Polygon",
  19. "coordinates": [...]
  20. }
  21. }
  22. ]
  23. }
  24. }

AUTHOR

Rishabh Mehta eternal.blizzard23@gmail.com

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.

  1. $ python setup.py sdist
  2. $ 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!