Skip to content

Instantly share code, notes, and snippets.

@crstn crstn/gist:6552662
Last active Dec 23, 2015

Embed
What would you like to do?
Transforms JSON files exported from the moves API [1] into GeoJSON, which can then be used in a number of applications, such as CartoDB or Leaflet. [1] http://dev.moves-app.com
<?php
// this is the directory where you have your json files from the moves API stored
$dir = "/my/input/dir";
// this is where your geojson files will be stored
$outdir = "/my/output/dir";
$geojsonplaces = [
"type" => "FeatureCollection",
"features" => [ ],
];
$geojsontracks = [
"type" => "FeatureCollection",
"features" => [ ],
];
if ($handle = opendir($dir)) {
// iterate through the input json files obtained from the moves api
while (false !== ($entry = readdir($handle))) {
$file = $dir.'/'.$entry;
if (is_file($file)){
echo "Processing $entry<br />";
$json_file = file_get_contents($file);
$array = json_decode($json_file, true);
foreach ($array as $json) {
$segments = $json["segments"];
foreach($segments as $segment){
// distinguish between static locations and movement segments
// we will put them into two separate output files
if($segment["type"] == "place"){
// we'll store the time spent at each location in minutes:
$duration = (date("U",strtotime($segment["endTime"])) - date("U",strtotime($segment["startTime"])));
$place = $segment["place"];
$location = $place["location"];
// reassemble the feature a la geojson:
$place = [ "type" => "Feature",
"geometry" =>
[ "type" => "Point",
"coordinates" => [$location["lon"], $location["lat"]],
],
"properties" => [ "placename" => $place["name"],
"placetype" => $place["type"],
"startTime" => $segment["startTime"],
"endTime" => $segment["endTime"],
"duration" => $duration,
]
];
array_push($geojsonplaces["features"], $place);
// movement segments:
}else if($segment["type"] == "move"){
$activities = $segment["activities"];
foreach ($activities as $activity) {
// assemble the geojson lineString from the moves json waypoints:
$coordinates = [];
foreach ($activity["trackPoints"] as $trackpoint) {
$thispoint = [$trackpoint["lon"], $trackpoint["lat"]];
array_push($coordinates, $thispoint);
}
// reassemble the feature a la geojson:
$line = [ "type" => "Feature",
"geometry" =>
[ "type" => "LineString",
"coordinates" => $coordinates
],
"properties" =>
[ "activity" => $activity["activity"],
"distance" => $activity["distance"],
"startTime" => $activity["startTime"],
"endTime" => $activity["endTime"],
"duration" => $activity["duration"]
]
];
array_push($geojsontracks["features"], $line);
}
}
}
}
}
}
closedir($handle);
}
// add the required properties to the two geojson files to make them valid geojson
$properties = [ "properties" => [ "title" => "My moves."] ];
array_push($geojsonplaces, $properties);
array_push($geojsontracks, $properties);
// write to disk
file_put_contents($outdir.'/places.json', json_encode($geojsonplaces));
file_put_contents($outdir.'/tracks.json', json_encode($geojsontracks));
echo "Done.";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.