使用jq计算geoJSON多边形的重心 [英] Computing the center of gravity of a geoJSON Polygon using jq

查看:79
本文介绍了使用jq计算geoJSON多边形的重心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在geoJSON文件中有一个用多边形描述的城市列表.

I have a list of cities described by their polygon in geoJSON files.

我想在多边形的内部获取一个样本.

I would like to get a sample inside point of the polygon.

基本数学说重心在多边形内部,足以将所有经度和所有纬度加在一起,然后将其除以点数.

Basic maths says the center of gravity is inside the polygon and it suffices to sum all longitudes and all latitudes together then divide it by the number of points.

要处理的完整文件(可在GitHub上找到可视化文件

Full file to process (visualization is available on GitHub)

{
  "type": "FeatureCollection",
  "features": [
  {
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [[[2.41101, 48.72605], [2.41554, 48.72656], [2.41718, 48.72791], [2.4211, 48.72953], [2.42603, 48.72824], [2.42756, 48.72865], [2.42922, 48.72723], [2.43133, 48.72646], [2.43404, 48.72665], [2.43513, 48.72409], [2.42554, 48.7227], [2.42072, 48.72105], [2.41426, 48.71782], [2.41327, 48.71869], [2.41582, 48.72086], [2.41238, 48.72193], [2.41136, 48.72325], [2.41101, 48.72605]]]
    },
    "properties": {
      "code": "94001",
      "nom": "Ablon-sur-Seine"
    }
  },
  {
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [[[2.41959, 48.81691], [2.4159, 48.81633], [2.40936, 48.81667], [2.40787, 48.81746
    },
    "properties": {
      "code": "94018",
      "nom": "Charenton-le-Pont"
    }
  },
  ...
  ]
}

我已经有一个计算多边形顶点长度的命令.

I already have a command that computes the length of the polygon vertices.

$ curl -s https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements/94-val-de-marne/communes-94-val-de-marne.geojson \
> | jq '.features[0].geometry.coordinates[0][][0]' \
> | jq -s 'add/length'
2.4206944444444445

请参见 https://unix.stackexchange.com/questions/13731/

使用jq和简单的bash命令,如何计算经度总和和纬度总和,然后将重心重新注入另一个geoJSON文件的properties字段中?

Using jq and simple bash commands, how can I compute the sum of the longitudes and sum of the latitudes as well, and reinject the barycenter in properties field in another geoJSON file?

谢谢.

推荐答案

因此,如果我正确理解,您将尝试获取第一组坐标的平均值,然后更新属性以存储结果.

So if I understood correctly, you're trying to get the averages of the first set of coordinates, then updating the properties to store the result.

.features[] |= (
    (.geometry.coordinates[0] | length as $len | reduce .[] as [$x, $y] ([0,0];
        [.[0] + $x, .[1] + $y]
    ) | map(. / $len)) as $barrycenter |
    .properties.barycenter = $barrycenter
)

这篇关于使用jq计算geoJSON多边形的重心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆