I recieve json objects like the following over an rest-service
[
{
"pos_time": "04.09.2018 09:57:02",
"receivetime": "04.09.2018 09:57:18",
"latitude": 47554898,
"longitude": 13173448,
"speed": 8,
"course": 359,
"country": "AT"
},
{
"pos_time": "04.09.2018 09:58:02",
"receivetime": "04.09.2018 09:58:31",
"latitude": 47835502,
"longitude": 13653503,
"speed": 7,
"course": 174,
"country": "AT"
},
]
form this json I want to create a geojson "linestring". The "linestring" is not the problem. the problem is, that I have to use both coordinates of each object to create the "linestring" and I have no idea how to loop through the objects and use the coordinates from the second object and first object to create the "linestring"
the result should look like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"pos_time": "04.09.2018 09:56:22",
"receivetime": "04.09.2018 09:57:18",
"course": 177,
"speed": 2,
"country": "AT",
"error": null
},
"geometry": {
"type": "LineString",
"coordinates": [
[
13.173448, // object 1
47.554898 // object 1
],
[
13.653503, // object 2
47.835502 // object 2
]
]
}
}
]
}
and the code for the moment looks like this:
// create geojson
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
///////
foreach ($tomtom_request_array as $key => $value) {
if (empty($value['longitude']) || empty($value['latitude']))
{
$longitude = "13.07202";
$latitude = "47.889486";
$error = "Missing or incorrect GPS data";
}
else
{
$latitude = $value['longitude']; // object 1
$longitude = $value['latitude']; // object 1
$latitude_previous = $value['longitude']; // object 2
$longitude_previous = $value['latitude']; // object 2
$error = NULL;
}
if (empty($value['speed']))
{
$speed = "0";
}
else
{
$speed = $value['speed'];
}
if (empty($value['course']))
{
$course = "0";
}
else
{
$course = $value['course'];
}
$feature = array(
'type' => 'Feature',
'properties' => array(
'pos_time' => $value['pos_time'],
'receivetime' => $value['receivetime'],
'course' => $course,
'speed' => $speed,
'country' => $value['country'],
'error' => $error
),
'geometry' => array(
'type' => 'LineString',
'coordinates' => array(
array(($latitude * 0.000001), ($longitude* 0.000001)),
array(($latitude_previous * 0.000001), ($longitude_previous* 0.000001))
),
),
);
array_push($geojson['features'], $feature);
}
$this->response($geojson, $response_status);
}
thanks in advance!