1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smindel\GIS\ORM; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DB; |
6
|
|
|
use Smindel\GIS\ORM\FieldType\DBGeometry; |
7
|
|
|
use Smindel\GIS\GIS; |
8
|
|
|
|
9
|
|
|
trait GISSchemaManager |
10
|
|
|
{ |
11
|
|
|
// Ellipsoidal spatial data type. |
12
|
3 |
|
public function geography($values) |
|
|
|
|
13
|
|
|
{ |
14
|
3 |
|
return 'geography'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
// Planar spatial data type. |
18
|
|
|
public function geometry($values) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
return 'geometry'; |
21
|
|
|
} |
22
|
4 |
|
public function translateStGenericFilter($field, $value, $inclusive, $hint) |
23
|
|
|
{ |
24
|
4 |
|
$null = $inclusive ? '' : ' OR ' . DB::get_conn()->nullCheckClause($field, true); |
25
|
4 |
|
$fragment = sprintf( |
26
|
4 |
|
'%sST_%s(%s, ST_GeomFromText(?, ?))%s', |
27
|
4 |
|
$inclusive ? '' : 'NOT ', |
28
|
4 |
|
$hint, |
29
|
4 |
|
$field, |
30
|
4 |
|
$null |
31
|
|
|
); |
32
|
4 |
|
return [$fragment => [GIS::create($value)->wkt, GIS::create($value)->srid]]; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function translateStContainsFilter($field, $value, $inclusive, $hint = false) |
36
|
|
|
{ |
37
|
1 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function translateStCrossesFilter($field, $value, $inclusive, $hint = false) |
41
|
|
|
{ |
42
|
1 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function translateStDisjointFilter($field, $value, $inclusive, $hint = false) |
46
|
|
|
{ |
47
|
1 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function translateStDistanceFilter($field, $value, $inclusive) |
51
|
|
|
{ |
52
|
2 |
|
$null = $inclusive ? '' : ' OR ' . DB::get_conn()->nullCheckClause($field, true); |
53
|
2 |
|
$fragment = sprintf('ST_Distance(ST_GeomFromText(?, ?),%s) %s ?%s', $field, $inclusive ? '<=' : '> ', $null); |
54
|
2 |
|
$geo = GIS::create($value[0]); |
55
|
2 |
|
$distance = $value[1]; |
56
|
2 |
|
return [$fragment => [$geo->wkt, $geo->srid, $distance]]; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function translateStEqualsFilter($field, $value, $inclusive, $hint = false) |
60
|
|
|
{ |
61
|
1 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function translateStGeometryTypeFilter($field, $value, $inclusive) |
65
|
|
|
{ |
66
|
1 |
|
$null = $inclusive ? '' : ' OR ' . DB::get_conn()->nullCheckClause($field, true); |
67
|
1 |
|
$fragment = sprintf( |
68
|
1 |
|
'%sLOWER(ST_GeometryType(%s)) = ?%s', |
69
|
1 |
|
$inclusive ? '' : 'NOT ', |
70
|
1 |
|
$field, |
71
|
1 |
|
$null |
72
|
|
|
); |
73
|
1 |
|
return [$fragment => 'st_' . strtolower($value)]; |
74
|
|
|
} |
75
|
4 |
|
public function translateStIntersectsFilter($field, $value, $inclusive, $hint = false) |
76
|
|
|
{ |
77
|
4 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function translateStOverlapsFilter($field, $value, $inclusive, $hint = false) |
81
|
|
|
{ |
82
|
1 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function translateStTouchesFilter($field, $value, $inclusive, $hint = false) |
86
|
|
|
{ |
87
|
1 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function translateStWithinFilter($field, $value, $inclusive, $hint = false) |
91
|
|
|
{ |
92
|
1 |
|
return $this->translateStGenericFilter($field, $value, $inclusive, $hint); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function translateDistanceQuery($geo1, $geo2) |
96
|
|
|
{ |
97
|
1 |
|
$geo1 = GIS::create($geo1); |
98
|
1 |
|
$geo2 = GIS::create($geo2); |
99
|
1 |
|
return sprintf("ST_Distance(ST_GeomFromText('%s', %d),ST_GeomFromText('%s', %d))", $geo1->wkt, $geo1->srid, $geo2->wkt, $geo2->srid); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.