0

Trying to find a solution for this as I wish to use Bootleaf and it uses GeoJSON data. I followed one of his guide on how to convert a CSV file into a GeoJSON file structure and have managed to do that. Here is the code I have

<?php
/*
* Title:   CSV to GeoJSON
* 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.
* Author:  Bryan R. McBride, GISP
* Contact: bryanmcbride.com
* GitHub:  https://github.com/bmcbride/PHP-Database-GeoJSON
*/
# Read the CSV file
$csvfile = 'Property_CSVTruncated.csv';
$handle = fopen($csvfile, 'r');
# Build GeoJSON feature collection array
$geojson = array(
  'type' => 'FeatureCollection',
  'features' => array()
);
# Loop through rows to build feature arrays
$header = NULL;
while (($row = fgetcsv($handle, 1000000, ',')) !== FALSE) {
  if (!$header) {
      $header = $row;
  } else {
      $data = array_combine($header, $row);
//        print_r($data) ;
      $properties = $data;
      # Remove x and y fields from properties (optional)
//        unset($properties['x']);
//        unset($properties['y']);
      $feature = array(
          'type' => 'Feature',
          'geometry' => array(
              'type' => 'Point',
              'coordinates' => array(
                  $data['51.045681'],
                  $data['-114.191544']
              )
          ),
//            'properties' => $properties
      );
      # Add feature arrays to feature collection array
      array_push($geojson['features'], $feature);
  }
}
fclose($handle);
header('Content-type: application/json');
echo json_encode($geojson, JSON_NUMERIC_CHECK);

?>

Part of the output is like this: enter image description here

Not sure if I actually understand the difference between JSON and GeoJSON but can they both be interchanged in terms of usage? Can I save the output as JSON and link it in the bootleaf code?

0

1 Answer 1

1

GeoJSON is simply a schema for common geo data like points, lines, and polygons, with the specification that the data is encoded into JSON.

Any JSON parser can parse GeoJSON into its data, and your application is then responsible for interpreting the data within.

Sign up to request clarification or add additional context in comments.

2 Comments

So saving the output as a JSON file should then work for me? I am confused as Bootleaf specifically has a different .geojson files that it reads through to populate listings.
@Gauzdx Yep, it's just JSON. The file name extension is just a naming convention.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.