0

I have imported into my mongoDB collection a set of geoJSON polygons that equate to the legislative districts in the Commonwealth of Pennsylvania. This data came from a geoJSON file on Penn State University's website and different tools do draw from the file correctly, which tells me the original data is valid.

I imported it by loading the data into an array and iterating through each entry and populating the corresponding geometry fields in the mongoosejs schema. The schema for a district looks like this:

const polygonSchema = new Schema({
    type: {
      type: String,
      enum: ['Polygon'],
      required: true
    },
    coordinates: {
      type: [[[Number]]],
      required: true
    }
  });

var districtSchema = new Schema({

    jurisdiction: {type: String, enum:['state', 'us']},
    chamber: {type: String, enum: ['house', 'senate']},

    districtNumber: {type: Number},

    contact: {
        "firstName": String,
        "lastName": String,
        "party": String,
        "url": String,
    },

    geometry: {type: polygonSchema, index: '2dsphere'},
    
    deleted: {type: Boolean, default: false}
});

An example entry looks something like this (the polygons contain a ton of points, so I truncated it)

{
  "_id": "602dc87f9f8326e4e5825b70",
  "chamber": "house",
  "districtNumber": 5,
  "jurisdiction": "us",
  "__v": 0,
  "contact": {
    "firstName": "Mary Gay",
    "lastName": "Scanlon",
    "party": "D",
    "url": "https://scanlon.house.gov"
  },
  "geometry": {
    "coordinates": [
      [
        [
          -75.59557557627258,
          39.83744617888617
        ],
        [
          -75.5965303118867,
          39.8380378976056
        ],
        [
          -75.59689506526837,
          39.8383438227424
        ],
        [
          -75.59720859368865,
          39.83857630244814
        ],
        [
          -75.59754671724104,
          39.83897169444833
        ],
        [
          -75.59780380953957,
          39.83936864305231
        ]
      ]
    ],
    "type": "Polygon"
  }
}

To test out the data, I am trying to find the District that contains a specific coordinate pair. However, I am not getting any results when I know it's a valid set of coordinates.

This is the test query I am running using mongoose; it finds no matches and returns no errors when it should return the district sampled above:

models.district.findOne(
{"geometry": {
  "$geoIntersects": { "$geometry": { "type": "Point", "coordinates": [39.948164,-75.339984] }
  }
}
}).exec((err, result) => console.log("Coordinate test", err,result));

Interestingly, I also used the demo databases that mongoDB uses in their geospatial tutorial that do work with the queries. Structurally, the coordinates seem to be situated the same; the only difference I can spot is that my Polygons have a ton more points.

1 Answers1

0

This was a case of RTFM mixed with fatigue-induced oversight. When I had written my test coordinates, I hand-wrote them in the query and, without noticing the order in the geoJSON version, I assumed the order was latitude, longitude. Turns out that the spec has them inverted.

When I inverted my test coordinates, the query works how I expected it to.