尝试编写一个将从MS Sql Server返回GeoJSON的PHP页面。
错误消息:geometry成员应该是object,但是是一个String 代替。
结果是什么:
[{"type":"Feature","geometry":"\"MULTILINESTRING ((-77.083060324719384 42.15108721847372, -77.087448024528669 42.151768518696542
我的代码:
$sql = "SELECT name, pwl_id, wbcatgry, basin, fact_sheet, geom.STAsText() as geo FROM dbo.total";
while ($res = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) {
$str = $res['geo'];
$wkt = explode("((",$str);
$msg [] = array(
'type' => 'Feature',
'geometry' => ($res['geo']), //<<<--- where I'm stuck.
// 'geometry' => json_decode($res['geo'], true),
'properties' => array(
"PWL_ID" => $res['pwl_id'],
"Name" => $res['name'],
我是在MySQL中使用
完成的ST_AsGeoJSON(`geom`) as geo and 'geometry' => json_decode($res['Geo'])
但是SQL-Server没有AsGeoJSON功能。
我以为我可以抓取地理文本并解析它并重新创建我需要的东西,但我希望有更好的方法,而且我对解析代码并不是100%肯定。
输出就像:
{&#34; type&#34;:&#34; Feature&#34;,&#34; properties&#34;:{&#34; PWL_ID&#34 ;: 18,&#34; Name&#34 ;:&#34; IN787&#34;,}, &#34;几何&#34;:{&#34;类型&#34;:&#34; MultiLineString&#34;,&#34;坐标&#34;:[[[ -73.781598476340562,42.633203605299833],[ - 73.764907547494587,42.63285861396318],[ - 73.75312949415769,42.639574643901661] .....
答案 0 :(得分:0)
您可以使用preg_match_all
解析它们$string = "MULTILINESTRING ((-77.083060324719384 42.15108721847372, -77.087448024528669 42.151768518696542";
$pattern = '/((-?\d+\.\d+)\s(-?\d+\.\d+))/';
$arr = preg_match_all($pattern,$string,$matches);
print_r($matches);
哪个输出
Array (
[0] => Array (
[0] => -77.083060324719384 42.15108721847372
[1] => -77.087448024528669 42.151768518696542
)
[1] => Array (
[0] => -77.083060324719384 42.15108721847372
[1] => -77.087448024528669 42.151768518696542
)
[2] => Array (
[0] => -77.083060324719384
[1] => -77.087448024528669
)
[3] => Array (
[0] => 42.15108721847372
[1] => 42.151768518696542
)
)
我以一种更容易使用的结果组织捕获,并且它只匹配成对的坐标。
所以你需要的是项目2
和3
。其余的都是微不足道的。
但我决定很好,
$len = count($matches[0]);
$coordinates = [];
for($i=0;$i<$len;++$i){
$coordinates[] = [
$matches[2][$i], $matches[3][$i]
];
}
print_r($coordinates);
哪个输出
Array (
[0] => Array (
[0] => -77.083060324719384
[1] => 42.15108721847372
)
[1] => Array (
[0] => -77.087448024528669
[1] => 42.151768518696542
)
)
你可以在这里看到完整的交易
然后你应该能够将它插入你需要的地方json_encode
等......
[["-77.083060324719384","42.15108721847372"],["-77.087448024528669","42.151768518696542"]]
当然,我只是在解决你在问题中所做的一点点投入,但它可能有效。