forked from Super-Visions/sv-geolocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sv-geolocation.php
389 lines (347 loc) · 9.42 KB
/
main.sv-geolocation.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
* @copyright Copyright (C) 2019 Super-Visions
* @license http://opensource.org/licenses/AGPL-3.0
*/
class AttributeGeolocation extends AttributeDBField
{
public function GetEditClass()
{
return 'GeoLocation';
}
public function GetSQLColumns($bFullSpec = false)
{
$aColumns = array();
$aColumns[$this->GetCode().'_lat'] = 'DECIMAL(8,6)';
$aColumns[$this->GetCode().'_lng'] = 'DECIMAL(9,6)';
return $aColumns;
}
public function GetSQLExpressions($sPrefix = '')
{
if ($sPrefix == '')
{
$sPrefix = $this->GetCode();
}
$aColumns = array();
$aColumns[''] = $sPrefix.'_lat';
$aColumns['latitude'] = $sPrefix.'_lat';
$aColumns['longitude'] = $sPrefix.'_lng';
return $aColumns;
}
/**
* @param $aCols
* @param string $sPrefix
* @return ormGeolocation
* @throws MissingColumnException
*/
public function FromSQLToValue($aCols, $sPrefix = '')
{
if (!array_key_exists($sPrefix.'latitude', $aCols))
{
$sAvailable = implode(', ', array_keys($aCols));
throw new MissingColumnException("Missing column '".$sPrefix."latitude' from {$sAvailable}");
}
if (!array_key_exists($sPrefix.'longitude', $aCols))
{
$sAvailable = implode(', ', array_keys($aCols));
throw new MissingColumnException("Missing column '".$sPrefix."longitude' from {$sAvailable}");
}
if (isset($aCols[$sPrefix.'latitude'], $aCols[$sPrefix.'longitude']))
{
return new ormGeolocation(floatval($aCols[$sPrefix.'latitude']), floatval($aCols[$sPrefix.'longitude']));
}
}
public function GetSQLValues($value)
{
$aValues = array();
if ($value instanceof ormGeolocation)
{
$aValues[$this->GetCode().'_lat'] = $value->getLatitude();
$aValues[$this->GetCode().'_lng'] = $value->getLongitude();
}
else
{
$aValues[$this->GetCode().'_lat'] = null;
$aValues[$this->GetCode().'_lng'] = null;
}
return $aValues;
}
/**
* @param null|string|ormGeolocation $proposedValue
* @param DBObject $oHostObj
* @return ormGeolocation
*/
public function MakeRealValue($proposedValue, $oHostObj)
{
if ($proposedValue instanceof ormGeolocation) return $proposedValue;
return ormGeolocation::fromString($proposedValue);
}
/**
* Geolocation raw value always contains comma character
*
* @inheritDoc
*/
public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false) {
if (!empty($sValue) && strpos($sSeparator, ',') !== false) return $sTextQualifier.$sValue.$sTextQualifier;
return parent::GetAsCSV($sValue, $sSeparator, $sTextQualifier,$oHostObject, $bLocalize, $bConvertToPlainText);
}
/**
* @inheritDoc
* @param ormGeolocation $value
* @throws ConfigException
* @throws CoreException
*/
public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
{
if ($value instanceOf ormGeolocation)
{
$iWidth = $this->GetWidth();
$iHeight = $this->GetHeight();
$sApiKey = utils::GetConfig()->GetModuleSetting('sv-geolocation', 'api_key');
$iZoom = utils::GetConfig()->GetModuleSetting('sv-geolocation', 'default_zoom');
$bDisplay = utils::GetConfig()->GetModuleSetting('sv-geolocation', 'display_coordinates');
$sStaticMapUrl = sprintf(static::GetStaticMapUrl(), $value->GetLatitude(), $value->GetLongitude(), $iWidth, $iHeight, $sApiKey, $iZoom);
if (empty($sStaticMapUrl))
{
$sHTML = sprintf('<pre>%s</pre>', $value);
}
else
{
$sHTML = sprintf('<img src="%s" width="%d" height="%d" title="%s"/>', $sStaticMapUrl, $iWidth, $iHeight, $value);
if ($bDisplay) $sHTML .= sprintf('<pre>%s</pre>', $value);
}
}
else
{
$sHTML = '<em>'.Dict::S('UI:UndefinedObject').'</em>';
}
return $sHTML;
}
/**
* @inheritDoc
*/
function EnumTemplateVerbs()
{
return array(
'' => 'Plain text representation in EPSG:4326 (lat,lon)',
'wgs_84' => 'Plain text representation in EPSG:4326 (lat,lon)',
'rd' => 'Plain text representation in EPSG:28992 (X,Y)',
'rijksdriehoek' => 'Plain text representation in EPSG:28992 (X,Y)',
'html' => 'HTML representation',
);
}
/**
* @inheritDoc
* @param ormGeolocation|null $value
* @return string
*/
function GetForTemplate($value, $sVerb, $oHostObject = null, $bLocalize = true)
{
switch ($sVerb)
{
case 'rijksdriehoek':
case 'rd':
if ($value instanceof ormGeolocation) return sprintf('%f,%f', $value->getRijksdriehoekX(), $value->getRijksdriehoekY());
else return;
case 'wgs_84':
if ($value instanceof ormGeolocation) return sprintf('%f,%f', $value->getLatitude(), $value->getLongitude());
else return;
default:
return parent::GetForTemplate($value, $sVerb, $oHostObject, $bLocalize);
}
}
/**
* @inheritDoc
*/
public function GetAsHTMLForHistory($sValue, $oHostObject = null, $bLocalize = true)
{
return (string) $sValue;
}
public function GetImportColumns()
{
$aColumns = array();
$aColumns[$this->GetCode()] = 'VARCHAR(25)'.CMDBSource::GetSqlStringColumnDefinition();
return $aColumns;
}
/**
* @param array $aCols
* @param string $sPrefix
* @return ormGeolocation|null
* @throws MissingColumnException
*/
public function FromImportToValue($aCols, $sPrefix = '')
{
if (!isset($aCols[$sPrefix]))
{
$sAvailable = implode(', ', array_keys($aCols));
throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
}
return $this->MakeRealValue($aCols[$sPrefix], null);
}
/**
* @return int Width of the map
*/
public function GetWidth()
{
return (int) $this->GetOptional('width', 200);
}
/**
* @return int Height of the map
*/
public function GetHeight()
{
return (int) $this->GetOptional('height', 150);
}
/**
* @return string Image URL to use as static map
* @throws ConfigException
* @throws CoreException
*/
public static function GetStaticMapUrl()
{
$sStaticMapUrl = utils::GetConfig()->GetModuleSetting('sv-geolocation', 'staticmapurl');
if ($sStaticMapUrl) return $sStaticMapUrl;
$sApiKey = utils::GetConfig()->GetModuleSetting('sv-geolocation', 'api_key');
switch (utils::GetConfig()->GetModuleSetting('sv-geolocation', 'provider'))
{
case 'GoogleMaps':
if ($sApiKey) return 'https://maps.googleapis.com/maps/api/staticmap?markers=%1$f,%2$f&size=%3$dx%4$d&key=%5$s';
break;
case 'OpenStreetMap':
return 'http://staticmap.openstreetmap.de/staticmap.php?center=%1$f,%2$f&markers=%1$f,%2$f,red-pushpin&size=%3$dx%4$d&zoom=%6$d';
case 'MapQuest':
if ($sApiKey) return 'https://www.mapquestapi.com/staticmap/v5/map?locations=%1$f,%2$f&size=%3$d,%4$d&zoom=%6$d&key=%5$s';
break;
}
return null;
}
}
class ormGeolocation implements JsonSerializable
{
protected $fLatitude = 0.0;
protected $fLongitude = 0.0;
public function __construct($fLatitude, $fLongitude)
{
$this->fLatitude = $fLatitude;
$this->fLongitude = $fLongitude;
}
/**
* @return float
*/
public function getLatitude()
{
return $this->fLatitude;
}
/**
* @return float
*/
public function getLongitude()
{
return $this->fLongitude;
}
/**
* Calculate the X coordinate in the Rijksdriehoek (RD) system
*
* @since 1.6.0
* @return float
*/
public function getRijksdriehoekX()
{
static $aPQR = [
[0,1,190094.945],
[1,1,-11832.228],
[2,1,-114.221],
[0,3,-32.391],
[1,0,-0.705],
[3,1,-2.34],
[1,3,-0.608],
[0,2,-0.008],
[2,3,0.148],
];
$fX = 155E3;
$oRijksdriehoekReference = static::getRijksdriehoekReference();
$oD = new static(
0.36 * ($this->getLatitude() - $oRijksdriehoekReference->getLatitude()),
0.36 * ($this->getLongitude() - $oRijksdriehoekReference->getLongitude())
);
foreach ($aPQR as list($iP, $iQ, $fR))
{
$fX += $fR * pow($oD->getLatitude(), $iP) * pow($oD->getLongitude(), $iQ);
}
return $fX;
}
/**
* Calculate the Y coordinate in the Rijksdriehoek (RD) system
*
* @since 1.6.0
* @return float
*/
public function getRijksdriehoekY()
{
static $aPQS = [
[1, 0, 309056.544],
[0, 2, 3638.893],
[2, 0, 73.077],
[1, 2, -157, 984],
[3, 0, 59.788],
[0, 1, 0.433],
[2, 2, -6.439],
[1, 1, -0.032],
[0, 4, 0.092],
[1, 4, -0.054]
];
$fY = 463E3;
$oRijksdriehoekReference = static::getRijksdriehoekReference();
$oD = new static(
0.36 * ($this->getLatitude() - $oRijksdriehoekReference->getLatitude()),
0.36 * ($this->getLongitude() - $oRijksdriehoekReference->getLongitude())
);
foreach ($aPQS as list($iP, $iQ, $fS))
{
$fY += $fS * pow($oD->getLatitude(), $iP) * pow($oD->getLongitude(), $iQ);
}
return $fY;
}
public function __toString()
{
return sprintf('%f,%f', $this->fLatitude, $this->fLongitude);
}
/**
* @inheritDoc
* @return array{lat: float, lng: float}
*/
public function jsonSerialize(): array
{
return array(
'lat' => $this->fLatitude,
'lng' => $this->fLongitude,
);
}
/**
* @since 1.6.0
* @return static
*/
public static function getRijksdriehoekReference()
{
return new static(52.1551744, 5.38720621);
}
/**
* Create ormGeolocation object from string input
*
* @since 1.8.0
* @param string|null $sInput
* @return static|null
*/
public static function fromString(?string $sInput)
{
if (preg_match('{^([-+]?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?)),\s*([-+]?(?:180(?:\.0+)?|(?:(?:1[0-7]\d)|(?:[1-9]?\d))(?:\.\d+)?))$}', trim($sInput), $aMatches))
{
return new static(floatval($aMatches[1]), floatval($aMatches[2]));
}
else
{
// TODO: Implement location to coordinates
return null;
}
}
}