How to load GeoJson data?

I am trying to load some GeoJson data into the built in geo data fields, but it doesn’t seem to match up since there are no latitude And longitude Keys in the arrays.

The 2nd object in this image is what I’m trying to load in.

image

Here is the error when trying to load that bbox field into a [Point] field.


{
  errors: [
    {
      message: 'Expected type PointRef, found -104.057879.',
      locations: [Array]
    },
    {
      message: 'Expected type PointRef, found 42.479635.',
      locations: [Array]
    },
    {
      message: 'Expected type PointRef, found -96.436589.',
      locations: [Array]
    },
    {
      message: 'Expected type PointRef, found 45.94545.',
      locations: [Array]
    }
  ]
}

Just wondering if there is a way to make this work without writing a special function to convert it.

Well, it looks like DQL can accept a flat array like this: https://dgraph.io/docs/tutorial-8/

But as I was reminded today, I am very rusty and a newb in my DQL loosely typed queries and mutations. I still rely heavily on the strongly typed GraphQL API. So… This can be done from GraphQL by transforming the data.

Incoming data type: (TypeScript)

type TBox = [number, number, number, number]
type TCoordinates = [number, number][][]
interface IInput {
  bbox: TBox
  type: string
  coordinates: ICoordinates
}

Expected output types: (Typescript)

interface IPoint {
  longitude: number
  latitude: number
}
interface ICoordinates {
  points: IPoint[]
}
interface IPolygon {
  coordinates: ICoordinates
}
// Knowing our GraphQL schema I am remapping the input object to match our schema fields names and types.
interface IOutput {
  boundsBox: [IPoint, IPoint]
  polygon: IPolygon
}

And the functions to take input and generate output:

const geoifyBox = (box: TBox): [IPoint, IPoint] => {
  return [
    {
      longitude: box[0],
      latitude: box[1]
    },{
      longitude: box[2],
      latitude: box[3]
    }
  ]
}

const geoifyPolygon = (polygon: TCoordinates): IPolygon => {
  const points: IPoint[] = []
  polygon.forEach(val => val.forEach(val => {
    points.push({ longitude: val[0], latitude: val[1] })
  }))
  return {
    coordinates: { 
      points: points
    }
  }
}

const geoifyObject = (obj: IInput): IOutput => {
  return {
    boundsBox: geoifyBox(obj.bbox),
    polygon: geoifyPolygon(obj.coordinates)
  }
}

const geoifyObjects = (objs: IInput[]): IOutput[] => {
  return objs.map(obj => geoifyObject(obj))
}