A simple way to track your position.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5KB

  1. <?php
  2. /*
  3. * Title: CSV to GeoJSON
  4. * Notes: Convert a comma separated CSV file of points with x & y fields to GeoJSON format, suitable for use in OpenLayers, Leaflet, etc. Only point features are supported.
  5. * Author: Bryan R. McBride, GISP
  6. * Contact: bryanmcbride.com
  7. * GitHub: https://github.com/bmcbride/PHP-Database-GeoJSON
  8. */
  9. # Read the CSV file
  10. $csvfile = 'positions.txt';
  11. $handle = fopen($csvfile, 'r');
  12. # Build GeoJSON feature collection array
  13. $geojson = array(
  14. 'type' => 'FeatureCollection',
  15. 'features' => array()
  16. );
  17. # Loop through rows to build feature arrays
  18. $header = NULL;
  19. while (($row = fgetcsv($handle, 1000, ',')) !== FALSE) {
  20. if (!$header) {
  21. $header = $row;
  22. } else {
  23. $data = array_combine($header, $row);
  24. $properties = $data;
  25. # Remove x and y fields from properties (optional)
  26. unset($properties['x']);
  27. unset($properties['y']);
  28. $feature = array(
  29. 'type' => 'Feature',
  30. 'geometry' => array(
  31. 'type' => 'Point',
  32. 'coordinates' => array(
  33. $data['x'],
  34. $data['y']
  35. )
  36. ),
  37. 'properties' => $properties
  38. );
  39. # Add feature arrays to feature collection array
  40. #array_push($geojson['features'], $feature);
  41. }
  42. }
  43. array_push($geojson['features'], $feature);
  44. fclose($handle);
  45. header('Content-type: application/json');
  46. header("Access-Control-Allow-Origin: *");
  47. echo json_encode($geojson, JSON_NUMERIC_CHECK);
  48. ?>