public async Task TestIsValid() { CosmosDatabaseSettings database = await this.client.CreateDatabaseAsync( new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString("N") }); CosmosContainerSettings collectionDefinition = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString("N") }; collectionDefinition.IndexingPolicy = new IndexingPolicy() { IncludedPaths = new Collection <IncludedPath>() { new IncludedPath() { Path = "/" }, new IncludedPath() { Path = "/Location/?", Indexes = new Collection <Index>() { new SpatialIndex(DataType.Point) } } } }; CosmosContainerSettings collection = await this.client.CreateDocumentCollectionAsync( database.SelfLink, collectionDefinition); var invalidGeometry = new SpatialSampleClass { Location = new Point(20, 180), Id = Guid.NewGuid().ToString() }; await this.client.CreateDocumentAsync(collection.SelfLink, invalidGeometry); var validGeometry = new SpatialSampleClass { Location = new Point(50, 50), Id = Guid.NewGuid().ToString() }; await this.client.CreateDocumentAsync(collection.SelfLink, validGeometry); IOrderedQueryable <SpatialSampleClass> sampleClasses = this.client.CreateDocumentQuery <SpatialSampleClass>(collection.DocumentsLink, new FeedOptions() { }); SpatialSampleClass[] distanceQuery = sampleClasses .Where(f => f.Location.Distance(new Point(20, 180)) < 20000) .ToArray(); Assert.AreEqual(0, distanceQuery.Count()); SpatialSampleClass[] isNotValidQuery = sampleClasses .Where(f => !f.Location.IsValid()) .ToArray(); Assert.AreEqual(1, isNotValidQuery.Count()); Assert.AreEqual(invalidGeometry.Location, isNotValidQuery[0].Location); IOrderedQueryable <SpatialSampleClass> invalidDetailed = this.client.CreateDocumentQuery <SpatialSampleClass>(collection.DocumentsLink, new FeedOptions() { }); var query = invalidDetailed .Where(f => !f.Location.IsValid()).Select(f => f.Location.IsValidDetailed()); var isNotValidDetailedQuery = query.ToArray(); Assert.AreEqual(1, isNotValidDetailedQuery.Count()); Assert.AreEqual("Latitude values must be between -90 and 90 degrees.", isNotValidDetailedQuery[0].Reason); Assert.AreEqual(false, isNotValidDetailedQuery[0].IsValid); SpatialSampleClass[] isValidQuery = sampleClasses .Where(f => f.Location.IsValid()) .ToArray(); Assert.AreEqual(1, isValidQuery.Count()); Assert.AreEqual(validGeometry.Location, isValidQuery[0].Location); }
private async Task TestDistanceAndWithin(bool allowScan) { CosmosDatabaseSettings database = await this.client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString("N") }); CosmosContainerSettings collectionDefinition = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString("N") }; CosmosContainerSettings collection; if (allowScan) { collection = await this.client.CreateDocumentCollectionAsync(database.SelfLink, collectionDefinition); } else { collectionDefinition.IndexingPolicy = new IndexingPolicy() { IncludedPaths = new Collection <IncludedPath>() { new IncludedPath() { Path = "/" }, new IncludedPath() { Path = "/Location/?", Indexes = new Collection <Index>() { new SpatialIndex(DataType.Point) } } } }; collection = await this.client.CreateDocumentCollectionAsync(database.SelfLink, collectionDefinition); } var class1 = new SpatialSampleClass { Location = new Point(20, 20), Id = Guid.NewGuid().ToString() }; await this.client.CreateDocumentAsync(collection.SelfLink, class1); var class2 = new SpatialSampleClass { Location = new Point(100, 100), Id = Guid.NewGuid().ToString() }; await this.client.CreateDocumentAsync(collection.SelfLink, class2); IOrderedQueryable <SpatialSampleClass> sampleClasses = this.client.CreateDocumentQuery <SpatialSampleClass>(collection.DocumentsLink, new FeedOptions() { EnableScanInQuery = allowScan }); SpatialSampleClass[] distanceQuery = sampleClasses .Where(f => f.Location.Distance(new Point(20.1, 20)) < 20000) .ToArray(); Assert.AreEqual(1, distanceQuery.Count()); Polygon polygon = new Polygon( new[] { new Position(10, 10), new Position(30, 10), new Position(30, 30), new Position(10, 30), new Position(10, 10), }); SpatialSampleClass[] withinQuery = sampleClasses .Where(f => f.Location.Within(polygon)) .ToArray(); Assert.AreEqual(1, withinQuery.Count()); }