0

我使用节点模块“query-overpass”查询从openstreetmaps 获取农场商店。我想将所有多边形转换为该脚本中的点。我使用 turf.js 来获取这些多边形的质心,但我无法永久更改对象。到目前为止,这是我的代码:

const query_overpass = require("query-overpass");
const turf = require ("turf");
const fs = require("fs")
let test
let filename = "data/test.js"
let bbox = "48.91821286473131,8.309097290039062,49.0610446187357,8.520584106445312";
console.log('starting query for ' +filename)
console.log('bbox: ' +bbox)
let query = ` 
  [out:json][timeout:250];
  // gather results
  (
  // query part for: “vending=milk”
  node["vending"="milk"](${bbox});
  way["vending"="milk"](${bbox});
  relation["vending"="milk"](${bbox});
  // query part for: “shop=farm”
  node["shop"="farm"](${bbox});
  way["shop"="farm"](${bbox});
  relation["shop"="farm"](${bbox});
  // query part for: “vending=food”
  node["vending"="food"](${bbox});
  way["vending"="food"](${bbox});
  relation["vending"="food"](${bbox});
  );
  // print results
  out body;
  >;
  out skel qt;
`;

// query overpass, write result to file
query_overpass(query, (error, data)  => {
    data = JSON.stringify(data , null, 1)
    console.log(data)
    test = JSON.parse(data)


//create centroids for every polyon and save them as a point
    for (var i = 0; i < test.features.length; i++) { 
      console.log("Log: " +test.features[i].geometry.type)
      console.log("Log: " +test.features[i].properties.name)
      if (test.features[i].geometry.type === "Polygon"){
        console.log("polygon detected")
        var centroid = turf.centroid(test.features[i]);
            var lon = centroid.geometry.coordinates[0];
            var lat = centroid.geometry.coordinates[1];
            console.log(" lon: " +lon +" lat: " +lat)

            test.features[i].geometry.type = 'Point'
            //delete Polygon structure and insert centroids as new points here
            console.log("polygon deleted and changed to point")
      }

  }
  console.log(test)
   fs.writeFile(filename, `var file = ${test};` , ["utf-8"], (error, data) => {if (error) {console.log(error)}})
 }, {flatProperties: true}
)

似乎我可以更改 for 循环内的内容,但稍后保存数据时它们不会出现。这基本上是一个如何正确编辑 json 对象的问题,但我不明白为什么这在这里根本不起作用。

所以基本上有两个问题:

  • 为什么我不能在上面的示例中覆盖 geometry.type?
  • 如何删除旧多边形并向要素添加新点?

谢谢你的帮助。

4

1 回答 1

3

这很复杂......为什么不让 Overpass API 完成这项工作并使用out center;而不是out body;>;out skel qt;返回所有节点、路径和关系的中心点。您可以先使用 overpass-turbo.eu 进行尝试。

于 2018-05-14T18:13:19.300 回答