Skip to content

Point Serializer

The Point serializer stores geographic coordinates as GeoJSON Point objects, compatible with MongoDB’s geospatial queries.

using Cratis.Geospatial;
public class Store
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public Point Location { get; set; }
}
// Create a point
var point = new Point(longitude: -122.4194, latitude: 37.7749);

Points are serialized as GeoJSON Point documents:

{
"_id": ObjectId("..."),
"name": "Downtown Store",
"location": {
"type": "Point",
"coordinates": [-122.4194, 37.7749]
}
}

The coordinates array follows GeoJSON format: [longitude, latitude].

Use MongoDB’s $near operator with geospatial indexes:

// Create a geospatial index first
collection.Indexes.CreateOne(
new CreateIndexModel<Store>(
Builders<Store>.IndexKeys.Geo2DSphere(s => s.Location)
)
);
// Query for stores near a point
var nearbyStores = await collection.Find(
Builders<Store>.Filter.Near(
s => s.Location,
new Point(-122.4, 37.78),
maxDistance: 5000 // 5km in meters
)
).ToListAsync();
// Find points within a polygon boundary
var storesInArea = await collection.Find(
Builders<Store>.Filter.GeoWithin(
s => s.Location,
polygon
)
).ToListAsync();
  • Coordinate Order: Always use [longitude, latitude] (not latitude, longitude)
  • Create Indexes: Create 2dsphere indexes on Point properties for efficient spatial queries
  • Null Handling: Points can be nullable (Point?) for optional locations
  • Validation: Ensure longitude is between -180 and 180, latitude between -90 and 90