[英]Json decode function not decoding in PHP
我使用PostGIS擴展程序從postgreSQL數據庫中調用了一些面要素。 當我在pgAdmin中運行以下兩個查詢時。 它返回2個功能。 而且屬性表僅包含geom字段,沒有更多屬性。
//query 1
$sql = $db->query(
"CREATE TABLE table_union AS
SELECT ST_Union(ST_SnapToGrid(geom,0.0001)) as geom
FROM areas_demo
GROUP BY type;"
);
//query 2
$sql = $db->query(
"SELECT * FROM table_union ;"
);
因此,我通過AJAX調用運行以下PHP代碼。
//compiling result to create a geoJSON file
$features=[];
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
$feature=['type'=>'Feature'];
$feature['geometry']=json_decode($row['geom']);
unset($row['geom']);
$feature['properties']=$row;
array_push($features, $feature);
}
$featureCollection=['type'=>'FeatureCollection', 'features'=>$features];
echo json_encode($featureCollection);
它運行完美,但是我在ajax成功函數中得到的響應是:
{"type":"FeatureCollection","features":
[
{"type":"Feature","geometry":null,"properties":[]},
{"type":"Feature","geometry":null,"properties":[]}
]}
它錯過了幾何列。
當我評論該行時:
//unset($row['geom']);
並改用以下代碼:
$feature['geom'] = json_decode($row['geom']);
我收到以下結果:
{"type":"FeatureCollection","features":
[
{"type":"Feature","geometry":null,"geom":null,"properties":{"geom":"0106000020E6100000030000000103000000010000000C000000304CA60A4...."}},
{"type":"Feature","geometry":null,"geom":null,"properties":{"geom":"0106000020E6100000030000000103000000010000000800000048E17A14A...."}}
]}
換句話說,json_decode函數無法按我期望的方式工作。 有人暗示為什么?
我期望的是Leaflet讀取的以下geoJSON格式:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -9.1353, 38.7089 ], [ -9.1327, 38.7085 ], [ -9.1329, 38.7075 ], [ -9.1339, 38.7066 ], [ -9.1349, 38.7065 ], [ -9.1369, 38.7066 ], [ -9.1395, 38.7062 ], [ -9.1409, 38.7065 ], [ -9.141, 38.7071 ], [ -9.1395, 38.708 ], [ -9.1376, 38.7088 ], [ -9.1353, 38.7089 ] ] ], [ [ [ -9.1353, 38.7148 ], [ -9.1358, 38.713 ], [ -9.131, 38.7131 ], [ -9.1313, 38.7148 ], [ -9.1334, 38.7159 ], [ -9.1353, 38.7148 ] ] ], [ [ [ -9.1373, 38.7117 ], [ -9.1373, 38.711 ], [ -9.1382, 38.711 ], [ -9.1389, 38.7117 ], [ -9.138602739726027, 38.712167123287671 ], [ -9.1388, 38.7122 ], [ -9.1391, 38.7125 ], [ -9.139, 38.7131 ], [ -9.1386, 38.7137 ], [ -9.1375, 38.7138 ], [ -9.1368, 38.713 ], [ -9.1365, 38.7116 ], [ -9.1373, 38.7117 ] ] ] ] } },
{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -9.14, 38.7137 ], [ -9.1401, 38.7146 ], [ -9.1393, 38.7146 ], [ -9.1384, 38.714 ], [ -9.1385, 38.7129 ], [ -9.1393, 38.7125 ], [ -9.1403, 38.7127 ], [ -9.14, 38.7137 ] ] ], [ [ [ -9.142254171066526, 38.709864202745514 ], [ -9.1429, 38.7096 ], [ -9.1437, 38.7105 ], [ -9.142446368715083, 38.71144022346369 ], [ -9.1426, 38.7127 ], [ -9.1395, 38.7103 ], [ -9.1421, 38.7086 ], [ -9.142254171066526, 38.709864202745514 ] ] ], [ [ [ -9.1352, 38.7093 ], [ -9.135114856230031, 38.709287539936099 ], [ -9.1354, 38.7102 ], [ -9.134548387096778, 38.710412903225809 ], [ -9.136, 38.7108 ], [ -9.1332, 38.7128 ], [ -9.1322, 38.7126 ], [ -9.1327, 38.7115 ], [ -9.1334, 38.7107 ], [ -9.1328, 38.7102 ], [ -9.1323, 38.709 ], [ -9.132510550458722, 38.708906422018352 ], [ -9.1311, 38.7087 ], [ -9.132, 38.7074 ], [ -9.1337, 38.7075 ], [ -9.1346, 38.7084 ], [ -9.134733333333335, 38.7086 ], [ -9.1349, 38.7086 ], [ -9.135047058823528, 38.709070588235292 ], [ -9.1352, 38.7093 ] ] ] ] } }
]
}
打印json_decode($sampleRowGeom)
的輸出,您將了解為什么會發生這種情況。
從json_decode()
手冊中:
如果無法解碼json或編碼的數據深於遞歸限制,則返回NULL。
您從該聯合表中獲取的值不是有效的json。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.