mysql - Formatting json to geojson via PHP -
i trying data have called mysql database correctly display in geojson format. here's of php code:
$data = array(); //setting empty php array data go if($result = mysqli_query($db,$query)) { while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } } $jsondata =json_encode($data); $original_data = json_decode($jsondata, true); $coordinates = array(); foreach($original_data $key => $value) { $coordinates[] = array($value['latitude'], $value['longitude']); } $new_data = array( 'type' => 'featurecollection', 'features' => array(array( 'type' => 'feature', 'properties' => array('time' => $value['time']), 'geometry' => array('type' => 'point', 'coordinates' => $coordinates), ), ), ); $final_data = json_encode($new_data, json_pretty_print); print_r($final_data);
but need them every set of coordinates has own "type" , "properties" key-value pair:
i've found issue here, can't manage on last formatting hurdle...
instead of building coordinates, need build features:
$data = array(); //setting empty php array data go if($result = mysqli_query($db,$query)) { while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } } $jsondata =json_encode($data); $original_data = json_decode($jsondata, true); $features = array(); foreach($original_data $key => $value) { $features[] = array( 'type' => 'feature', 'properties' => array('time' => $value['time']), 'geometry' => array( 'type' => 'point', 'coordinates' => array( $value['latitude'], $value['longitude'], 1 ), ), ); } $new_data = array( 'type' => 'featurecollection', 'features' => $features, ); $final_data = json_encode($new_data, json_pretty_print); print_r($final_data);
Comments
Post a Comment