使用Php查询SQL并为地质多边形输出geojson

使用Php查询SQL并为地质多边形输出geojson,php,mysql,geospatial,geojson,Php,Mysql,Geospatial,Geojson,为存储在MySQL数据库中的PHP中的地理多边形输出geojson 我尝试过很多东西,包括GeoHP插件,但迄今为止没有成功,还尝试过使用ST_AsGeoJSON,但没有任何用处 $sql = "select ST_AsGeoJSON(ST_GeomFromText('coordinates')) from buildings where id = 1"; 我正在尝试实现类似的输出 "shape":{"type":"Polygon","coordinates":[[[53.294974,-6

为存储在MySQL数据库中的PHP中的地理多边形输出geojson

我尝试过很多东西,包括GeoHP插件,但迄今为止没有成功,还尝试过使用ST_AsGeoJSON,但没有任何用处

$sql = "select ST_AsGeoJSON(ST_GeomFromText('coordinates')) from buildings where id = 1";

我正在尝试实现类似的输出

"shape":{"type":"Polygon","coordinates":[[[53.294974,-6.426631],[53.294847,-6.426419],[53.294289,-6.426888],[53.294326,-6.427194],[53.294974,-6.426631]]]}
这是我的插入声明-

INSERT INTO `mrp_buildings` (
  `buildingId`, `companyId`, `name`, `noOfFloors`,
  `coordinates`, `city`, `country`, `address`, `phone`,
  `email`) VALUES (
    '1', '1', 'My Sample Building', '4',
    PolyFromText('POLYGON((33.294974 -2.426631, 53.294847 -6.426419,
      73.294289 -6.426888, 13.294326 -6.427194, 43.294974 -6.426631,
      33.294974 -2.426631))'), 'Dublin', 'Ireland', 'Mayor Street', '089449 8500', 'email@example.ie ');

任何建议-

获取数据库行,用JSON键创建一个数组,然后
JSON\u encode()

当然,存储坐标的方式很混乱,实际上需要将$coordinates转换为如下数组:

$coordinates = [
    [
        55.123456,
        0.123456,
    ],
    [
       //etc
    ]
为了做到这一点,你需要先扔掉你不需要的东西:

$string = $row['coordinates']; /* POLYGON((33.294974 -2.426631, 53.294847 -6.426419,
      73.294289 -6.426888, 13.294326 -6.427194, 43.294974 -6.426631,
      33.294974 -2.426631)) */

$string = str_replace('POLYGON', '', $string); /* ((33.294974 -2.426631, 53.294847 -6.426419,
  73.294289 -6.426888, 13.294326 -6.427194, 43.294974 -6.426631,
  33.294974 -2.426631)) */

 $string = str_replace('(', '', $string);
 $string = str_replace(')', '', $string); // remove brackets

 $coords = explode(',' $string);
在这一点上,你将有一个坐标数组,但是X和Y都是一个由空格分隔的值,所以我们也需要分解它

$coordinates = [];
foreach ($coords as $co) {
    $ex = explode(' ', $co);
    $coordinates[] = [
        $ex[0], $ex[1];
    ];
}
最后,您可以创建要进行JSON编码的数组:

$array = []; //empty array

$array['shape'] = [
    'type' => 'Polygon',
    'coordinates' => $coordinates
];

$json = json_encode($array);

我希望这有帮助!如果可以的话,试着将坐标存储在DB中,而不需要额外的东西来节省所有的麻烦

对不起,我的英语是hanks del,但我想你没有理解我的问题,存储的数据是GeoPolygon(它是一种空间数据),在javascript中,有方法使用坐标获取这种类型的数据。ST_ASGeoJson()但我正在努力解决itGeoPolygon的php实现是一种DB类型吗?那你能帮我把这列倒出来吗?列的类型是几何体