DocumentDB - Geospatial Data

Microsoft added geospatial support, which lets you store location data in your documents and perform spatial calculations for distance and intersections between points and polygons.

  • Spatial data describes the position and shape of objects in space.

  • Typically, it can be used to represent the location of a person, a place of interest, or the boundary of a city, or a lake.

  • Common use cases often involve proximity queries. For e.g., "find all universities near my current location".

A Point denotes a single position in space which represents the exact location, e.g. street address of particular university. A point is represented in DocumentDB using its coordinate pair (longitude and latitude). Following is an example of JSON point.

   "coordinates":[ 28.3, -10.7 ] 

Let’s take a look at a simple example which contains the location of a university.

   "name":"CASE: Center For Advanced Studies In Engineering", 
   "location": { 
      "coordinates":[ 33.7194136, -73.0964862 ] 

To retrieve the university name based on the location, you can use the following query.


WHERE = "case-university" AND ST_ISVALID({ 
      "coordinates":[ 33.7194136, -73.0964862 ] })

When the above query is executed you will receive the following output.

      "name": "CASE: Center For Advanced Studies In Engineering" 

Create Document with Geospatial Data in .NET

You can create a document with geospatial data, let’s take a look at a simple example in which a university document is created.

private async static Task CreateDocuments(DocumentClient client) {
   Console.WriteLine("**** Create Documents ****"); 
   var uniDocument = new UniversityProfile {
      Id = "nust", 
      Name = "National University of Sciences and Technology", 
      City = "Islamabad", 
      Loc = new Point(33.6455715, 72.9903447) 
   Document document = await CreateDocument(client, uniDocument); 
   Console.WriteLine("Created document {0} from typed object", document.Id); 

Following is the implementation for the UniversityProfile class.

public class UniversityProfile { 
   [JsonProperty(PropertyName = "id")] 
   public string Id { get; set; }  
   public string Name { get; set; }
   public string City { get; set; }  
   public Point Loc { get; set; } 

When the above code is compiled and executed, you will receive the following output.

**** Create Documents ****  
Created new document: nust 
   "id": "nust", 
   "name": "National University of Sciences and Technology", 
   "city": "Islamabad", 
   "location": { 
      "type": "Point", 
      "coordinates": [ 
   "_rid": "Ic8LAMEUVgANAAAAAAAAAA==", 
   "_ts": 1450200910, 
   "_self": "dbs/Ic8LAA==/colls/Ic8LAMEUVgA=/docs/Ic8LAMEUVgANAAAAAAAAAA==/", 
   "_etag": "\"00004100-0000-0000-0000-56704f4e0000\"", 
   "_attachments": "attachments/" 
Created document nust from typed object