What is the correct JSON schema to support a GeoJSON polygon?

Hi,

I am trying to create documents with a GeoJSON Polygon via Graphql but I cannot create the correct JSON schema to support this.

I have GeoJSON Points working as it is an array of numbers but the Polygon requires nested arrays and Mongo cannot generate the graphql schema even though my JSON schema is valid.

any help appreciated

1 Like

I have the same problem here, but with realmDB. When I add these nested arrays in my Realm Schema it throw the following message error:

array property “polygons” cannot be of type array to be sync-compatible

{
      "title": "Field",
      "properties": {
      
        "_appId": {
          "bsonType": "string"
        },
        "_id": {
          "bsonType": "objectId"
        },
        "active": {
          "bsonType": "bool"
        },
      
        "name": {
          "bsonType": "string"
        },

        "polygons": {
          "bsonType": "array",
          "items": {
            "bsonType": "array",
            "minItems": {
              "$numberInt": "2"
            },
            "maxItems": {
              "$numberInt": "2"
            },
            "items": {
              "type": "number"
            }
          }
        },
       

      "required": [
        "_appId"
      ]
    }

Hi Virmerson,

I have just got it working using the below JSON-schema and the real-web SDK I can create/upsert documents with a geojson polygon, however grapql is unable to generate the schema so I cannot use graphql until I figure out how to ignore a specific field validation.

Ultimately I want to find documents with geoIntersect between the user location polygon and the document location polygon so I will test this next :slight_smile:

"location": {
  "bsonType": "object",
  "properties": {
    "coordinates": {
      "type": "array",
      "items": {
              "type": "array",
              "items": {
                  "type": "array",
                  "items": {
                      "bsonType": "double"
                  }
              }
      }
    },
    "type": {
      "type": "string"
    }
  }
},
1 Like

I found this, which is super useful:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://json-schema.org/geojson/geometry.json#",
    "title": "geometry",
    "description": "One geometry as defined by GeoJSON",
    "type": "object",
    "required": [ "type", "coordinates" ],
    "oneOf": [
        {
            "title": "Point",
            "properties": {
                "type": { "enum": [ "Point" ] },
                "coordinates": { "$ref": "#/definitions/position" }
            }
        },
        {
            "title": "MultiPoint",
            "properties": {
                "type": { "enum": [ "MultiPoint" ] },
                "coordinates": { "$ref": "#/definitions/positionArray" }
            }
        },
        {
            "title": "LineString",
            "properties": {
                "type": { "enum": [ "LineString" ] },
                "coordinates": { "$ref": "#/definitions/lineString" }
            }
        },
        {
            "title": "MultiLineString",
            "properties": {
                "type": { "enum": [ "MultiLineString" ] },
                "coordinates": {
                    "type": "array",
                    "items": { "$ref": "#/definitions/lineString" }
                }
            }
        },
        {
            "title": "Polygon",
            "properties": {
                "type": { "enum": [ "Polygon" ] },
                "coordinates": { "$ref": "#/definitions/polygon" }
            }
        },
        {
            "title": "MultiPolygon",
            "properties": {
                "type": { "enum": [ "MultiPolygon" ] },
                "coordinates": {
                    "type": "array",
                    "items": { "$ref": "#/definitions/polygon" }
                }
            }
        }
    ],
    "definitions": {
        "position": {
            "description": "A single position",
            "type": "array",
            "minItems": 2,
            "items": [ { "type": "number" }, { "type": "number" } ],
            "additionalItems": false
        },
        "positionArray": {
            "description": "An array of positions",
            "type": "array",
            "items": { "$ref": "#/definitions/position" }
        },
        "lineString": {
            "description": "An array of two or more positions",
            "allOf": [
                { "$ref": "#/definitions/positionArray" },
                { "minItems": 2 }
            ]
        },
        "linearRing": {
            "description": "An array of four positions where the first equals the last",
            "allOf": [
                { "$ref": "#/definitions/positionArray" },
                { "minItems": 4 }
            ]
        },
        "polygon": {
            "description": "An array of linear rings",
            "type": "array",
            "items": { "$ref": "#/definitions/linearRing" }
        }
    }
}
1 Like

I too would like to leverage a json-schema to validate geojson objects within a document.

This project provides ‘The overall GeoJSON schema’, but the resulting json file is a bit long and it doesn’t appear that MongoDB supports the ability to include another schema file reference.