Я вызываю некоторые функции полигонов из моей базы данных postgreSQL, используя расширение PostGIS.
Когда я запускаю два запроса ниже в 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 ;"
);
Итак, я запускаю следующий код PHP через вызов AJAX.
//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 ] ] ] ] } }
]
}
Изображение того, что я получаю при выполнении кода на БД