-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathGeospatial.cs
156 lines (128 loc) · 5.05 KB
/
Geospatial.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Examples.Models;
using NUnit.Framework;
using Realms;
using Realms.Sync;
namespace Examples
{
public class Geospatial
{
App app;
Realm realm;
[OneTimeSetUp]
public void Setup()
{
app = App.Create(Config.FSAppId);
realm = Realm.GetInstance();
// :snippet-start: geopoint
realm.WriteAsync(() =>
{
realm.Add(new Company
{
Location = new CustomGeoPoint(47.68, -122.35)
});
realm.Add(new Company
{
Location = new CustomGeoPoint(47.9, -121.85)
});
});
// :snippet-end:
}
[Test]
public void TestGeospatials()
{
// :snippet-start: geocircle
var circle1 = new GeoCircle((47.8, -122.6),
Distance.FromKilometers(44.4));
var circle2 = new GeoCircle(
new GeoPoint(latitude: 47.3, longitude: -121.9),
Distance.FromDegrees(0.25));
// :snippet-end:
// :snippet-start: geopolygon
var basicPolygon = new GeoPolygon((48, -122.8),
(48.2, -121.8), (47.6, -121.6), (47.0, -122.0),
(47.2, -122.6), (48, -122.8));
// Create a polygon with a single hole
var outerRing = new GeoPoint[] {
(48, -122.8), (48.2, -121.8),
(47.6, -121.6), (47.0, -122.0), (47.2, -122.6),
(48, -122.8) };
var hole1 = new GeoPoint[] {
(47.8, -122.6), (47.7, -122.2),
(47.4, -122.6), (47.6, -122.5),
(47.8, -122.6) };
var polygonWithOneHole = new GeoPolygon(outerRing, hole1);
// Add a second hole to the polygon
var hole2 = new GeoPoint[] {
(47.55, -122.05), (47.5, -121.9),(47.3, -122.1),
(47.55, -122.05) };
var polygonWithTwoHoles =
new GeoPolygon(outerRing, hole1, hole2);
// :snippet-end:
// :snippet-start: geobox
var box1 = new GeoBox(bottomLeftCorner: (47.3, -122.7),
topRightCorner: (48.1, -122.1));
var box2 = new GeoBox(new GeoPoint(47.5, -122.4),
new GeoPoint(47.9, -121.8));
// :snippet-end:
// ***** QUERIES ***** //
// :snippet-start: rql-geowithin
var GeoWthinExample = realm.All<Company>()
.Where(c => QueryMethods.GeoWithin(c.Location, circle1));
var RQLExample = realm.All<Company>()
.Filter("Location geoWithin $0", circle2);
// :snippet-end:
// :snippet-start: geopolygon-query
var companiesInBasicPolygon = realm.All<Company>()
.Where(c => QueryMethods
.GeoWithin(c.Location, basicPolygon));
var companiesInPolygon = realm.All<Company>()
.Where(c => QueryMethods
.GeoWithin(c.Location, polygonWithTwoHoles));
//:snippet-end:
// :snippet-start: geocircle-query
var companiesInCircle = realm.All<Company>()
.Where(c => QueryMethods.GeoWithin(c.Location, circle1));
var companiesInSmallerCircle = realm.All<Company>()
.Where(c => QueryMethods.GeoWithin(c.Location, circle2));
// :snippet-end:
// :snippet-start: geobox-query
var companiesInBox1 = realm.All<Company>()
.Where(c => QueryMethods.GeoWithin(c.Location, box1));
var companiesInBox2 = realm.All<Company>()
.Where(c => QueryMethods.GeoWithin(c.Location, box2));
// :snippet-end:
Assert.AreEqual(2, realm.All<Company>().Count());
Assert.AreEqual(1, companiesInCircle.Count());
Assert.AreEqual(0, companiesInSmallerCircle.Count());
Assert.AreEqual(0, basicPolygon.Holes.Count);
Assert.AreEqual(6, basicPolygon.OuterRing.Count);
Assert.AreEqual(1, polygonWithOneHole.Holes.Count);
Assert.AreEqual(2, polygonWithTwoHoles.Holes.Count);
Assert.AreEqual(2, companiesInBasicPolygon.Count());
Assert.AreEqual(1, companiesInPolygon.Count());
Assert.AreEqual(1, companiesInBox1.Count());
Assert.AreEqual(2, companiesInBox2.Count());
}
[OneTimeTearDown]
public void TearDown()
{
realm.Write(() => { realm.RemoveAll<Company>(); });
return;
}
}
//:snippet-start:usingcustomgeopoint
public partial class Company : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public Guid Id { get; private set; } = Guid.NewGuid();
public CustomGeoPoint? Location { get; set; }
public Company() { }
}
//:snippet-end:
}