将PHP输出另存为GeoJSON文件

将PHP输出另存为GeoJSON文件,php,json,geojson,Php,Json,Geojson,试图找到我希望使用的解决方案,它使用GeoJSON数据。我遵循了他的一个指南,介绍了如何将CSV文件转换为GeoJSON文件结构,并成功地做到了这一点。这是我的密码 <?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,

试图找到我希望使用的解决方案,它使用GeoJSON数据。我遵循了他的一个指南,介绍了如何将CSV文件转换为GeoJSON文件结构,并成功地做到了这一点。这是我的密码

<?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);

?>
部分输出如下所示:


我不确定我是否真的理解JSON和GeoJSON之间的区别,但它们在用法上可以互换吗?我可以将输出保存为JSON并将其链接到bootleaf代码中吗

GeoJSON只是点、线和多边形等常见地理数据的模式,其规范是将数据编码为JSON


任何JSON解析器都可以将GeoJSON解析为其数据,然后您的应用程序负责解释其中的数据。

那么将输出保存为JSON文件对我来说应该有用吗?我很困惑,因为Bootleaf有一个不同的.geojson文件,它通过读取来填充列表。@Gauzdx是的,它只是JSON。文件扩展名只是一种命名约定。