如何使用 Observable 和 turf.js 构建多边形?
How to build polygons with Observable and turf.js?
我正在使用来自数据库的坐标构建多边形。
我已经订阅了一个 Observable 来获取所有坐标(这是一个数组)。我将每个坐标数组推送到上数组中,以便能够构建我的多边形:
this.customerApi.getLocations(this.currentUser.id)
.subscribe((response) => {
this.locations = response;
this.polygonsArray.push(this.locations);
this.locations.forEach(element
=> {this.polygons.push(element.geoDefinition.features['0'].geometry.coordinates);
});
getLocations returns 3 个数组:
然后我将这些数组推送到一个名为 this.polygons:
的上层数组中
我的问题是当我打电话给 const varpolygon = turf.polygon(this.polygons);
我收到以下错误:
Uncaught (in promise): Error: Each LinearRing of a Polygon must have 4 or more Positions.
Error: Each LinearRing of a Polygon must have 4 or more Positions.
我认为我的数组结构很好,因为多边形是一个由坐标数组组成的数组。类型是 number[][][],比如 turf.polygon.
我的 validated GEOJSON 的结构是:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5.379856000000018,
43.252967
],
[
5.422988000000032,
43.249466
],
[
5.425048000000061,
43.245153
],
[
5.383804000000055,
43.239838
],
[
5.399856,
43.212967
],
[
5.379856000000018,
43.252967
]
]
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5.390139999999974,
43.279295
],
[
5.393068999999969,
43.279249
],
[
5.391814000000068,
43.278421
],
[
5.390709000000015,
43.278749
],
[
5.390899999999988,
43.2785
],
[
5.390139999999974,
43.279295
]
]
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-0.6070509999999558,
44.840753
],
[
-0.5437080000000378,
44.832962
],
[
-0.5222499999999854,
44.820544
],
[
-0.5663670000000138,
44.808853
],
[
-0.5863669999999956,
44.788853
],
[
-0.6070509999999558,
44.840753
]
]
]
},
"properties": {}
}
]
}
如果是这种情况,有人可以告诉我我做错了什么并帮助我吗?
在此先感谢您的帮助。
this.polygons 是一个多边形数组,因此它不是一个单一的多边形
for(var i;i<this.polygons.length;i++){
polygon = turf.polygon(this.polygons[i]);
}
通过这种方式,您可以为每个不同的多边形创建单独的草皮多边形。
或者,如果您想创建一个草皮多边形,您需要做的是创建一个草皮多多边形
var turfpolygon = turf.multiPolygon(this.polygons);
我正在使用来自数据库的坐标构建多边形。 我已经订阅了一个 Observable 来获取所有坐标(这是一个数组)。我将每个坐标数组推送到上数组中,以便能够构建我的多边形:
this.customerApi.getLocations(this.currentUser.id)
.subscribe((response) => {
this.locations = response;
this.polygonsArray.push(this.locations);
this.locations.forEach(element
=> {this.polygons.push(element.geoDefinition.features['0'].geometry.coordinates);
});
getLocations returns 3 个数组:
然后我将这些数组推送到一个名为 this.polygons:
的上层数组中我的问题是当我打电话给 const varpolygon = turf.polygon(this.polygons);
我收到以下错误:
Uncaught (in promise): Error: Each LinearRing of a Polygon must have 4 or more Positions.
Error: Each LinearRing of a Polygon must have 4 or more Positions.
我认为我的数组结构很好,因为多边形是一个由坐标数组组成的数组。类型是 number[][][],比如 turf.polygon.
我的 validated GEOJSON 的结构是:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5.379856000000018,
43.252967
],
[
5.422988000000032,
43.249466
],
[
5.425048000000061,
43.245153
],
[
5.383804000000055,
43.239838
],
[
5.399856,
43.212967
],
[
5.379856000000018,
43.252967
]
]
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5.390139999999974,
43.279295
],
[
5.393068999999969,
43.279249
],
[
5.391814000000068,
43.278421
],
[
5.390709000000015,
43.278749
],
[
5.390899999999988,
43.2785
],
[
5.390139999999974,
43.279295
]
]
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-0.6070509999999558,
44.840753
],
[
-0.5437080000000378,
44.832962
],
[
-0.5222499999999854,
44.820544
],
[
-0.5663670000000138,
44.808853
],
[
-0.5863669999999956,
44.788853
],
[
-0.6070509999999558,
44.840753
]
]
]
},
"properties": {}
}
]
}
如果是这种情况,有人可以告诉我我做错了什么并帮助我吗?
在此先感谢您的帮助。
this.polygons 是一个多边形数组,因此它不是一个单一的多边形
for(var i;i<this.polygons.length;i++){
polygon = turf.polygon(this.polygons[i]);
}
通过这种方式,您可以为每个不同的多边形创建单独的草皮多边形。
或者,如果您想创建一个草皮多边形,您需要做的是创建一个草皮多多边形
var turfpolygon = turf.multiPolygon(this.polygons);