场景:
我有一个system向web服务发出请求。你知道吗
JSON对象的一个小片段是:
{
"objectIdFieldName": "OBJECTID",
"globalIdFieldName": "",
"geometryType": "esriGeometryPolyline",
"spatialReference": {
"wkid": 476,
"latestWkid": 476
},
"fields": [
{
"name": "OBJECTID",
"alias": "OBJECTID",
"type": "esriFieldTypeOID"
}
],
"features": [
{
"attributes": {
"OBJECTID": 3311
},
"geometry": {
"paths": [
[
[
675844.1562959617,
4861766.9811610579
],
[
675878.30397594348,
4861792.5977392439
],
[
675891.38832408097,
4861800.4024024364
],
[
675902.17710777745,
4861804.9933949765
],
[
675912.27726199664,
4861808.2070551421
],
[
675923.52513550781,
4861810.2730065044
],
[
675934.77300901897,
4861811.1911861338
],
[
675943.03676202707,
4861811.1911861338
],
[
675951.07095439639,
4861810.502546167
],
[
675961.17111910321,
4861808.6661449578
],
[
675970.35304125212,
4861806.1411667075
],
[
675981.51595173683,
4861800.7007851209
],
[
675998.03647276573,
4861792.2469376959
]
]
]
}
},
**The JSON object has been cut off.**
完整的JSON对象可以在这里找到:JSON Polylines
问题:
使用JSON顶点,我想计算多段线的中点(见下面的绿点):
输出如下(格式可能不同):
OBJECTID MIDPOINT_X MIDPOINT_Y
2165 676163.9343 4861476.373
2320 676142.0017 4861959.66
2375 676118.1226 4861730.258
2682 676060.3917 4861904.762
2683 675743.1666 4861724.081
2691 676137.4796 4861557.709
3311 675916.9815 4861809.071
3385 676208.669 4861536.555
3546 676262.2623 4861665.146
3547 676167.5738 4861612.699
3548 676021.3677 4861573.141
3549 675914.4334 4861669.87
3550 675866.6003 4861735.572
3551 675800.1232 4861827.482
3552 675681.9432 4861918.989
3716 675979.6493 4861724.323
捕获:
这需要在Python2.7.0中完成——因为我的系统使用Jython2.7.0。你知道吗
是否可以使用Python2.7.0(不导入库)计算JSON多段线的中点?你知道吗
是的,您可以只使用内置的Python函数/而不导入外部库来轻松计算多段线的中点。你知道吗
让我们打破你的要求:
features
字段中的功能因此,我们需要一些辅助函数来计算两点之间的距离,然后是多段线的距离,依此类推:
让我们迭代您的数据(我将您的对象命名为
dataset
),并使用这些函数来计算中点:结果对象:
请注意:首先,我选择了节点数最多的路径(而不是距离最长的路径-使用类似
def get_longest(li): return max(li, key=len)
的方法),结果(视觉)与提供的结果更接近,所以可能这就是为什么您在说the longest part of the line
时想要的,但我不确定!相关问题 更多 >
编程相关推荐