【问题标题】:Save PHP output as a GeoJSON file将 PHP 输出保存为 GeoJSON 文件
【发布时间】:2019-02-21 17:11:04
【问题描述】:

试图为此找到解决方案,因为我希望使用Bootleaf 并且它使用 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 代码中吗?

【问题讨论】:

    标签: php json geojson


    【解决方案1】:

    GeoJSON 只是常见地理数据(如点、线和多边形)的架构,其规范将数据编码为 JSON。

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

    【讨论】:

    • 那么将输出保存为 JSON 文件应该对我有用吗?我很困惑,因为 Bootleaf 专门有一个不同的 .geojson 文件,它会读取这些文件以填充列表。
    • @Gauzdx 是的,它只是 JSON。文件扩展名只是一个命名约定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多