This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
GeospacialQuerySpec.groovy
138 lines (106 loc) · 3.29 KB
/
GeospacialQuerySpec.groovy
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
package org.grails.datastore.gorm.mongo
import grails.gorm.tests.GormDatastoreSpec
import grails.persistence.Entity
class GeospacialQuerySpec extends GormDatastoreSpec {
void "Test geolocation with BigDecimal values"() {
given:"Some entities stored with BigDecimal locations"
new Hotel(name:"Hilton", location:[50.34d, 50.12d]).save()
new Hotel(name:"Raddison", location:[150.45d, 130.67d]).save(flush:true)
session.clear()
when:"We query by location"
def h = Hotel.findByLocation([50.34d, 50.12d])
then:"The location is found"
h != null
}
void "Test that we can query within a circle"() {
given:
new Hotel(name:"Hilton", location:[50, 50]).save()
new Hotel(name:"Raddison", location:[150, 130]).save(flush:true)
session.clear()
when:
def h = Hotel.findByLocation([50, 50])
then:
h != null
when:
h = Hotel.findByLocationWithinCircle([[40, 30],40])
then:
h != null
h.name == "Hilton"
when:
h = Hotel.findByLocationWithinCircle([[10, 10],30])
then:
h == null
}
void "Test that we can query within a box"() {
given:
new Hotel(name:"Hilton", location:[50, 50]).save()
new Hotel(name:"Raddison", location:[150, 130]).save(flush:true)
session.clear()
when:
def h = Hotel.findByLocation([50, 50])
then:
h != null
when:
h = Hotel.findByLocationWithinBox([[40, 30],[60, 70]])
then:
h != null
h.name == "Hilton"
when:
h = Hotel.findByLocationWithinBox([[20, 10],[40, 30]])
then:
h == null
}
void "Test that we can query for nearby location"() {
given:
new Hotel(name:"Hilton", location:[50, 50]).save()
new Hotel(name:"Raddison", location:[150, 130]).save(flush:true)
session.clear()
when:
def h = Hotel.findByLocation([50, 50])
then:
h != null
when:
h = Hotel.findByLocationNear([50, 60])
then:
h != null
h.name == "Hilton"
when:
h = Hotel.findByLocationNear([170, 160])
then:
h != null
h.name == "Raddison"
}
void "Test that we can query for nearby location with criteria"() {
given:
new Hotel(name:"Hilton", location:[50, 50]).save()
new Hotel(name:"Raddison", location:[150, 130]).save(flush:true)
session.clear()
when:
def h = Hotel.findByLocation([50, 50])
then:
h != null
when:
h = Hotel.createCriteria().get { near( "location", [50, 60] ) }
then:
h != null
h.name == "Hilton"
when:
h = Hotel.createCriteria().get { near( "location", [170, 160] ) }
then:
h != null
h.name == "Raddison"
}
@Override
List getDomainClasses() {
[Hotel]
}
}
@Entity
class Hotel {
Long id
String name
List location
static mapping = {
location geoIndex:true
}
}