Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json解码函数在PHP中不解码_Php_Sql_Ajax_Postgresql_Postgis - Fatal编程技术网

Json解码函数在PHP中不解码

Json解码函数在PHP中不解码,php,sql,ajax,postgresql,postgis,Php,Sql,Ajax,Postgresql,Postgis,我正在使用PostGIS扩展从我的postgreSQL数据库调用一些多边形特征。 当我在pgAdmin中运行下面的两个查询时。它返回两个特性。属性表只包含geom字段,没有更多属性 //query 1 $sql = $db->query( "CREATE TABLE table_union AS SELECT ST_Union(ST_SnapToGrid(geom,0.0001)) as geom FROM areas

我正在使用PostGIS扩展从我的postgreSQL数据库调用一些多边形特征。 当我在pgAdmin中运行下面的两个查询时。它返回两个特性。属性表只包含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函数没有按照我期望的方式工作。有人暗示了为什么

我希望通过传单阅读以下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 ] ] ] ] } }
]
}
在db上执行代码时得到的图像

打印
json\u decode($sampleRowGeom)
的输出,您就会明白为什么会发生这种情况

json\u decode()
手册:

如果无法解码json或编码数据深度超过递归限制,则返回NULL


从该联合表中获取的值不是有效的json。

411363CBD5A4340C05B2041F143222C0A69BC420B05A4340DD4637804422C0C3F528
看起来不像json!您可以添加一个示例,说明直接在db上执行此操作时得到的结果吗?
json\u decode
遇到错误时返回
null
。因此,
$row['geom']
可能无效,json预期的输出是什么?据我所知,几何数据是以二进制格式存储的,而不是人类可读的格式。@GeoMaps请描述您想要什么样的输出,您完全正确,我需要将geom字段转换为geoJSON,以便使用以下代码:ST_AsGeoJSON(geom,5)作为geoJSON。