将 PHP 输出保存为 GeoJSON 文件
Save PHP output as a GeoJSON file
正在尝试为此找到解决方案,因为我希望使用 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 之间的区别,但它们可以在用法方面互换吗?我可以在 bootleaf 代码中将输出保存为 JSON 和 link 吗?
GeoJSON 只是点、线和多边形等常见地理数据的模式,其规范是将数据编码为 JSON.
任何 JSON 解析器都可以将 GeoJSON 解析为其数据,然后您的应用程序负责解释其中的数据。
正在尝试为此找到解决方案,因为我希望使用 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 之间的区别,但它们可以在用法方面互换吗?我可以在 bootleaf 代码中将输出保存为 JSON 和 link 吗?
GeoJSON 只是点、线和多边形等常见地理数据的模式,其规范是将数据编码为 JSON.
任何 JSON 解析器都可以将 GeoJSON 解析为其数据,然后您的应用程序负责解释其中的数据。