对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 Spring批处理JdbcCursorItemReader还是RepositoryItemReader?   java如何在JTable Swing中增加标题列的字体?   java在数组方面遇到问题,导致表达式非法启动   java如何为maven pom的某些部分关闭Eclipse代码格式化程序。xml   java Dropwizard登录代码   java Jersey 2.22:客户端的默认连接超时是多少?   java无法自动连接字段:javax。sql。数据来源   如何从java中的行列表中获取单词列表?   java JDBC批量更新和处理异常?   计算大根:bigdecimal/java   java如何在JavaCC语法中提到trycatch块   javasocket。getInetAddress()不返回任何内容   oracle SQL开发人员错误无法找到Java虚拟机   java我如何计算和显示未来5年每一年的投资价值   java如何关闭浏览器选项卡?   java如何在showMessageDialog中打印双2D数组?   java从站点抓取播放列表URL?   selenium中的java点击css按钮