简体   繁体   English

在JS中构建geojson坐标网格

[英]Building geojson coordinate grid in JS

I'm trying to build a coordinate grid using geojson format.我正在尝试使用 geojson 格式构建坐标网格。 My JS knowledge is a little basic so I'm struggling on how to build the grid not just in the x direction but the y direction, so I end up with a square grid not just a x-axis line.我的 JS 知识有点基础,所以我在努力研究如何不仅在 x 方向而且在 y 方向构建网格,所以我最终得到了一个方形网格,而不仅仅是 x 轴线。

This is my code:这是我的代码:

var yStart = -90; // Start coodinatate on y-axis
var xEnd = 180; // End point on x-axis
var yEnd = 90; // End point on y-axis
var gridSize = 10; // Size of the grid increments

geojson['type'] = 'FeatureCollection';
geojson['features'] = [];

for (let i = xStart; i <= xEnd; i += gridSize) {
    var newFeature = {
        "type": "Feature",
    "properties": {
    },
        "geometry": {
            "type": "Polygon",
            "coordinates": (i, i)
        }
    }
    geojson['features'].push(newFeature);
}
console.log(geojson);

How do I turn this from an x-axis incrementing loop to both x and y axis so it builds a grid not just a line of coordinates?如何将它从 x 轴递增循环转换为 x 和 y 轴,以便它构建一个网格而不仅仅是一条坐标线?

What you're doing is boiling down to the fact that enclosing your coordinate values in parenthesis () is materially different that what is intended or expected as part of GeoJSON.您正在做的事情归结为这样一个事实,即将您的坐标值括在括号()中与作为 GeoJSON 的一部分的预期或预期内容存在本质上的不同 Take the following example - in this instance, what you believe to be the x, y point 1, 2 evaluates to simply 2 in JavaScript:以下面的例子为例——在这个例子中,你认为的 x、y 点1, 2在 JavaScript 中计算为简单的2

 console.log((1, 2))

It's clear from the documentation on GeoJSON FeatureCollection that the values for "coordinates" of a Polygon are expressed as an Array of Arrays (both enclosed in square brackets):GeoJSON FeatureCollection 的文档中可以清楚地看出,多边形的“坐标”值表示为 Arrays 的数组(都括在方括号中):

var newFeature = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [[i, i]]
  }
}
geojson['features'].push(newFeature);

It's not clear where you got the guidance to the contrary to use parenthesis to enclose your coordinate values.目前尚不清楚您在哪里得到了相反的指导,使用括号将坐标值括起来。 It's also not clear how a single point is meant to create a polygon in this instance, but I can't knock it if it meets your requirements.在这种情况下,也不清楚单点是如何创建多边形的,但如果它符合您的要求,我无法敲它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM