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