andrewl / gatsby-source-geo

Geospatial Source Plugin for gatsby
MIT License
3 stars 2 forks source link

GraphQL Omits Coordinates with Conflicting Types #1

Open nathanieltalbot opened 4 years ago

nathanieltalbot commented 4 years ago

Interesting issue I've run into here that seems to be a conflict when you're using data from several different shapefiles.

A little background -- I'm using data from two different .shp files -- one of these files defines a series of polygons (it is showing census tracts) and another defines a series of points.

The coordinates of the polygons are represented in GeoJSON as an array of length 2 arrays like so:

"coordinates": [[700, -600], [701, -601], [702, -602]] 

The points, on the other hand, are represented by a single array of length 2, like so:

"coordinates": [700, -600]

GraphQL/Gatsby doesn't like this, as it treats these as conflicting datatypes with the following error message:

warn There are conflicting field types in your data.

If you have explicitly defined a type for those fields, you can safely ignore this warning message.
Otherwise, Gatsby will omit those fields from the GraphQL schema.

If you know all field types in advance, the best strategy is to explicitly define them with the `createTypes` action, and skip inference with the `@dontInfer` directive.
See https://www.gatsbyjs.org/docs/actions/#createTypes
geoFeature.geometry.coordinates:
 - type: [array]
   value: [ ..., [ ..., [ ..., -71.160393, ... ], ... ], ... ]
 - type: [number]
   value: [ ..., 236007.5383913815, ... ]

The coordinates field is then omitted from the results, to my dismay.

I've got the repository forked to try and fix this -- this seems to be within GeoJSON specification (see: https://geojson.org/). My approach right now is to simply split each datasource into separate nodes (since they all end up together under allGeoFeature) but this might prove problematic for shapefiles that define both polygons and points.

nathanieltalbot commented 4 years ago

From reading the GeoJSON spec, it looks like there are several different formats that an object can define its coordinates, which means there needs to be change in the way the data is typed in the plugin -- otherwise, these types will conflict. I'll see about a processFeature change to fix this.