|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smindel\GIS\Control; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use Smindel\GIS\GIS; |
|
9
|
|
|
use Smindel\GIS\Interfaces\HeaderAltererInterface; |
|
10
|
|
|
|
|
11
|
|
|
class GeoJsonService extends AbstractGISWebServiceController |
|
12
|
|
|
{ |
|
13
|
|
|
private static $url_handlers = [ |
|
14
|
|
|
'$Model' => 'handleAction', |
|
15
|
|
|
]; |
|
16
|
2 |
|
|
|
17
|
|
|
/** |
|
18
|
2 |
|
* @var HeaderAltererInterface |
|
19
|
2 |
|
*/ |
|
20
|
|
|
private $headerAlterer; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
2 |
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
2 |
|
parent::__construct(); |
|
26
|
|
|
$this->headerAlterer = Injector::inst()->get('Smindel\GIS\Helper\HeaderAlterer'); |
|
27
|
|
|
} |
|
28
|
2 |
|
|
|
29
|
|
|
public function getConfig($model) |
|
30
|
2 |
|
{ |
|
31
|
2 |
|
$modelConfig = parent::getConfig($model); |
|
32
|
2 |
|
if (!$modelConfig) { |
|
33
|
|
|
return false; |
|
34
|
2 |
|
} |
|
35
|
|
|
$defaults = [ |
|
36
|
|
|
'property_map' => singleton($model)->summaryFields(), |
|
37
|
|
|
]; |
|
38
|
|
|
return is_array($modelConfig) ? array_merge($defaults, $modelConfig) : $defaults; |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function index($request) |
|
42
|
2 |
|
{ |
|
43
|
|
|
$model = $this->getModel($request); |
|
44
|
2 |
|
$config = $this->getConfig($model); |
|
45
|
2 |
|
$list = $this->getRecords($request); |
|
46
|
|
|
|
|
47
|
|
|
$propertyMap = $config['property_map']; |
|
48
|
|
|
|
|
49
|
2 |
|
|
|
50
|
|
|
$this->headerAlterer->setHeader('Access-Control-Allow-Origin: ' . $config['access_control_allow_origin']); |
|
51
|
2 |
|
|
|
52
|
|
|
// The HTTP kernel keeps a copy of the response body, which |
|
53
|
2 |
|
// can exhaust the memory limit for large data sets. So we |
|
54
|
2 |
|
// opt out and flush the buffer after processing each feature. |
|
55
|
2 |
|
if (!($is_test = headers_sent())) { |
|
56
|
|
|
$this->headerAlterer->setHeader('Content-Type: application/geo+json'); |
|
57
|
|
|
} |
|
58
|
2 |
|
echo '{"type":"FeatureCollection","features":['; |
|
59
|
2 |
|
|
|
60
|
|
|
foreach ($list as $item) { |
|
61
|
2 |
|
if (!$item->canView()) { |
|
62
|
2 |
|
continue; |
|
63
|
|
|
} |
|
64
|
2 |
|
|
|
65
|
|
|
echo isset($geo) ? ',' : ''; |
|
66
|
|
|
|
|
67
|
|
|
$geo = GIS::create($item->{$config['geometry_field']})->reproject(4326); |
|
68
|
2 |
|
|
|
69
|
2 |
|
$properties = []; |
|
70
|
|
|
foreach ($propertyMap as $fieldName => $propertyName) { |
|
71
|
|
|
$properties[$propertyName] = $item->$fieldName; |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
$feature = [ |
|
75
|
|
|
'type' => 'Feature', |
|
76
|
|
|
'geometry' => ['type' => $geo->type], |
|
77
|
|
|
'properties' => $properties, |
|
78
|
|
|
]; |
|
79
|
|
|
if ($geo->type == GIS::TYPES['geometrycollection']) { |
|
80
|
|
|
$geometries = []; |
|
81
|
|
|
foreach ($geo->geometries as $geometry) { |
|
82
|
|
|
$geometries[] = [ |
|
83
|
|
|
'type' => $geometry->type, |
|
84
|
|
|
'coordinates' => $geometry->coordinates, |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
$feature['geometry']['geometries'] = $geometries; |
|
88
|
|
|
} else { |
|
89
|
|
|
$feature['geometry']['coordinates'] = $geo->coordinates; |
|
90
|
|
|
} |
|
91
|
|
|
echo json_encode($feature); |
|
92
|
|
|
} |
|
93
|
|
|
echo ']}'; |
|
94
|
|
|
if (!$is_test) { |
|
95
|
|
|
die; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.