My goal is to build a turf.js
multiPolygon.
This kind of hard-coding is working fine:
const searchWithin = turf.multiPolygon(
[
[[
[-0.607051, 44.840753],
[-0.543708, 44.832962],
[-0.52225, 44.820544],
[-0.566367, 44.808853],
[-0.586367, 44.788853],
[-0.607051, 44.840753]
]],
[[
[5.39014, 43.279295],
[5.393069, 43.279249],
[5.391814, 43.278421],
[5.390709, 43.278749],
[5.3909, 43.2785],
[5.39014, 43.279295]
]]
]);
As you can see, there is 3 levels of brackets before getting the coordinates arrays. Coming from the database, I am getting:
[
[
[-0.607051, 44.840753],
[-0.543708, 44.832962],
[-0.52225, 44.820544],
[-0.566367, 44.808853],
[-0.586367, 44.788853],
[-0.607051, 44.840753]
],
[
[5.39014, 43.279295],
[5.393069, 43.279249],
[5.391814, 43.278421],
[5.390709, 43.278749],
[5.3909, 43.2785],
[5.39014, 43.279295]
]
]
To get data from the Database:
.subscribe((response) => {
this.locations = response;
this.polygonsArray.push(this.locations);
this.locations.forEach(element => {
this.polygons.push(element.geoDefinition.features['0'].geometry.coordinates);
with polygons declared as:
polygons: number[][][] = [];
I tried:
this.polygons.push('['+element.geoDefinition.features['0'].geometry.coordinates)+']';
but coordinates are numbers so I cannot concatenate.
How could I do to have this structure with 3 square-brackets, please? Any help would be very helpful.
I thank you in advance for your help.
It is not clear about expected structure. But first notable things are:
polygons: number[][][] = [];
, you can't push number directly to it, like polygons.push(5)
, you have to take into account structure:polygons[0][0].push(5)
or polygons.push([[5]])
;
Another thing is this.polygonsArray.push(this.locations);
- it should be at least this.polygonsArray.push(...this.locations);
as push would add entire this.locations
array as first element of this.polygonsArray
.
And the last. If you push something like this.polygons.push('['+something+']');
- you just push a string to the simple array. Instead of that you can use
this.polygons[0].push(something);
Hope it can provide some ideas how to organize 3-dimensional structure in your case.