我正在使用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 ] ] ] ] } }
]
}
答案 0 :(得分:1)
打印json_decode($sampleRowGeom)
的输出,您将了解为什么会发生这种情况。
摘自json_decode()
手册:
如果无法解码json或编码数据深于递归限制,则返回NULL。
您从该联合表中获取的值不是有效的json。