如何从mysql空间表中输出geojson文件? 我使用OGR2OGR工具从点shapefile获得了一个表('pk')的MySQL空间数据库('MyDB1')。此“ PK”表的一行就像: ogr_fid形状codif位置...

问题描述 投票:0回答:1

<?php /** * Title: MySQL to GeoJSON (Requires https://github.com/phayes/geoPHP) * Notes: Query a MySQL table or view and return the results in GeoJSON format, suitable for use in OpenLayers, Leaflet, etc. * Author: Bryan R. McBride, GISP * Contact: bryanmcbride.com * GitHub: https://github.com/bmcbride/PHP-Database-GeoJSON */ # Include required geoPHP library and define wkb_to_json function include_once('geoPHP/geoPHP.inc'); function wkb_to_json($wkb) { $geom = geoPHP::load($wkb,'wkb'); return $geom->out('json'); } # Connect to MySQL database $conn = new PDO('mysql:host=localhost;dbname=mydb1','root','admin'); # Build SQL SELECT statement and return the geometry as a WKB element $sql = 'SELECT *, AsWKB(SHAPE) AS wkb FROM pk'; # Try query or error $rs = $conn->query($sql); if (!$rs) { echo 'An SQL error occured.\n'; exit; } # Build GeoJSON feature collection array $geojson = array( 'type' => 'FeatureCollection', 'features' => array() ); # Loop through rows to build feature arrays while ($row = $rs->fetch(PDO::FETCH_ASSOC)) { $properties = $row; # Remove wkb and geometry fields from properties unset($properties['wkb']); unset($properties['SHAPE']); $feature = array( 'type' => 'Feature', 'geometry' => json_decode(wkb_to_json($row['wkb'])), 'properties' => $properties ); # Add feature arrays to feature collection array array_push($geojson['features'], $feature); } header('Content-type: application/json'); echo json_encode($geojson, JSON_NUMERIC_CHECK); $conn = NULL; ?>

但是,当我在浏览器上执行代码时,我会完全获得一个空白页。我应该解决什么问题要输出我的geojson文件plz?

您应该检查PHP日志以获取更多信息,以防发生错误。确保启用了错误报告和显示:

error_reporting(E_ALL); ini_set('display_errors', 1);
php mysql geojson
1个回答
0
投票
如果没有出现错误,请检查$ Geojson对象最容易使用

print_r($geojson);
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.