2

我想从字符串下方获取多边形坐标。

{"polygon":{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[-7302732.4720101,6527844.6333235],[-3193477.8319711,6606116.1502766],[-5111129.9973226,5001550.0527375],[-6637424.5779086,4884142.7773079],[-7772361.5737289,5158093.0866438],[-7302732.4720101,6527844.6333235]]]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}}

这是我使用以下代码解码为数组的 GeoJson 字符串:

$polygon = CJSON::decode($str);

当我想获得多边形时,我得到了错误!

$var= $polygon->polygon;

或使用以下代码:

$polygon = CJSON::decode($str,true);
$var = $polygon['polygon'];

虽然为了获取坐标:

foreach($polygon as $key=>$value)
$coordinates = $value['coordinates'];
or
$coordinates = $value[coordinates];

如何从我从 javascript 发送到 php 的 geojson 获取坐标,以便使用 postgis 保存在 postgresql 上?

4

2 回答 2

2
$polygon->polygon->geometry->coordinates[0]

或者

$polygon['polygon']['geometry']['coordinates'][0]

你所拥有的是一个多维数组/对象,不确定在你的情况下它被解码时输出到哪个,因为看起来你有一个类在做它我本来只是使用 json_decode,但无论如何。是的,从外观上看,多边形是主要对象,然后是几何,它是具有类型和坐标的对象,然后坐标中有多个对象/数组。

如果我输入正确,上面的示例将显示该对象中的第一组坐标。当然,您可以通过循环运行它,即:

如果它是一个对象,则假设您的 Class 解码为对象而不是数组。不完全确定是什么$polygon = CJSON::decode($str,true);。但如果它有任何类似的东西,json_decode()那么你应该已经准备好了。

这是我在此处介绍的分解对象的方法,值得注意的是,您可能需要检查计数,并查看是否首先设置了对象,或者该属性是否存在于对象中以防止代码分解的其他方式马路。但是我在这里所拥有的只是其核心的纯粹示例,尽管它会服务于它的目的。但是不会处理错误,这就是为什么我说您可能想进一步详细说明它进行这些检查的原因。

无论如何继承我的代码:

<?php
$str = '{"polygon":{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[-7302732.4720101,6527844.6333235],[-3193477.8319711,6606116.1502766],[-5111129.9973226,5001550.0527375],[-6637424.5779086,4884142.7773079],[-7772361.5737289,5158093.0866438],[-7302732.4720101,6527844.6333235]]]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}}';

$polygon = json_decode($str);
echo'<pre>';print_r($polygon);echo'</pre>';

$set = 1;
foreach($polygon->polygon->geometry->coordinates[0] as $coordinates)
{
    echo 'Set '.$set.': ';$set++;
    echo $coordinates[0].','.$coordinates[1].'<br>';
}
?>

看看它在行动http://7pz.net/geojson-parse.php(滚动到底部)

于 2012-08-01T15:08:16.520 回答
1

This should give you an array of all the coordinates and print them out line by line:

$string = '{"polygon":{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[-7302732.4720101,6527844.6333235],[-3193477.8319711,6606116.1502766],[-5111129.9973226,5001550.0527375],[-6637424.5779086,4884142.7773079],[-7772361.5737289,5158093.0866438],[-7302732.4720101,6527844.6333235]]]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}}';

$json = json_decode($string);

$coords_array = $json->polygon->geometry->coordinates[0];

foreach($coords_array as $c_a) {
        echo $c_a[0] . "," .$c_a[1] . "<br>"; 
    }

Access with:

$coords_array[0];
$coords_array[1];
$coords_array[2];

etc.

Basically you can turn the JSON string into an object and access each element with the -> notation.

I usally use a site called http://jsonviewer.stack.hu/ to decode JSON and find the path I need, then simply write them out as they appear, as in as in the above - $json->polygon->geometry->coordinates;.

Try it out yourself on the site.

于 2012-08-01T15:11:51.803 回答