对django graphql的geojson支持

django-graphql-geojson的Python项目详细描述


PypiWheelBuild StatusCodecovCode Climate

GeoJSON支持Django GraphQL

依赖关系

  • python≥3.4
  • django≥1.11

安装

从pypi安装最后一个稳定版本。

pip install django-graphql-geojson

geojsontype

GeoJSONTypeDjangoObjectType的一个子类,它以geojson的格式提供graphql字段。

只需定义一个Meta.geojson_field来表示为Geometry类型。

models.py

fromdjango.contrib.gis.dbimportmodelsclassPlace(models.Model):name=models.CharField(max_length=255)location=models.PointField()

schema.py

importgrapheneimportgraphql_geojsonclassPlaceType(graphql_geojson.GeoJSONType):classMeta:model=models.Placegeojson_field='location'classQuery(graphene.ObjectType):places=graphene.List(PlaceType)schema=graphene.Schema(query=Query)

查询

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

几何类型

Geometry是表示GEOS geometry object的特殊graphql类型。

schema.py

importgrapheneimportgraphql_geojsonclassCreatePlace(graphene.Mutation):place=graphene.Field(types.PlaceType)classArguments:name=graphene.String(required=True)location=graphql_geojson.Geometry(required=True)@classmethoddefmutate(cls,root,info,**args):place=models.Place.objects.create(**args)returncls(place=place)

突变

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

Geometry类型可以通过几种方式初始化:

  • 知名文本(wkt):
"POINT(5 23)"
  • 十六进制(十六进制):
"010100000000000000000014400000000000003740"
  • GeoJSON:
{"type":"Point","coordinates":[5,23]}

geometryFilterset

django graphql geojson为spatial lookups提供了一个自定义过滤器集。

Meta.fields选项与model结合,自动生成过滤器。

filters.py

fromgraphql_geojson.filtersimportGeometryFilterSetclassPlaceFilter(GeometryFilterSet):classMeta:model=models.Placefields={'name':['exact'],'location':['exact','intersects','distance_lte'],}

schema.py

importgrapheneimportgraphql_geojsonfromgrapheneimportrelayfromgraphene_django.filterimportDjangoFilterConnectionFieldclassPlaceNode(graphql_geojson.GeoJSONType):classMeta:model=Placeinterfaces=[relay.Node]geojson_field='location'classQuery(graphene.ObjectType):places=DjangoFilterConnectionField(PlaceNode,filterset_class=PlaceFilter)

查询

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

Distance lookups接受一个Distance参数,包含:

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何反射地迭代数组字段?   java NamedQuery错误“具有给定标识符的多行:1”   java无法使用单独类中的计时器更新TextView   兼容性什么时候可以很快使用新的Java功能?   java二叉树路径和   java矩形的性能   java我想从同一个子表在主表中添加两个外键   java如何获取基于特定日期的所有数据?   java javafx、OO编程规则和写入变量类型的选择   java使用带枚举的switch语句   java异步任务生成运行时异常   java为什么JLabel不显示下划线字符?   java如何解析具有可变参数号的函数?   带有按钮的java JavaFX自定义列表单元格:未调用处理程序   java Modelmapper无法映射整个模型?   传递给持久化的java分离实体,包含LatLng列表