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