Benjamin RD
Benjamin RD

Reputation: 12034

Open layers - format points to multipolygon

I'm using two different services, the first one returns a Polygon, like this:

0: {type: "Feature", id: "postal_fsa_boundaries.1590", geometry: {type: "Polygon",…},…}

geometry: {type: "Polygon",…}
coordinates: [[[-123.09689608, 49.24042409], [-123.09391444, 49.24036854], [-123.09340615, 49.24035906],…]]
type: "Polygon"
geometry_name: "boundary"
id: "postal_fsa_boundaries.1590"

The second service, returns a Points with coordinates, like this:

features: [{type: "Feature", id: "active_address.fid-31c7b976_175e990a2fb_664a",…},…]
0: {type: "Feature", id: "active_address.fid-31c7b976_175e990a2fb_664a",…}
geometry: {type: "Point", coordinates: [-114.0926087, 51.05883014]}
coordinates: [-114.0926087, 51.05883014]
type: "Point"
geometry_name: "the_geom"
id: "active_address.fid-31c7b976_175e990a2fb_664a"

at the moment to parse those responses to polygons, I'm using:

const multi = new MultiPolygon(fsas.map((f) => f.geometry));

where geomety type is Polygon.

And at the moment to parse it I get:

new WKT().writeGeometry(multi)

I get a response like:

(( 51.05301895 -114.09479052,51.05305604 -114.09397244,51.05338313 -114.09335017,51.05341011 -114.09167379,51.05706864 -114.0917623,51.0571091 -114.08597946,51.05952998 -114.08607602,51.05956032 -114.09473956,51.05301895 -114.09479052)), (( 51.05301895 -114.09479052,51.05305604 -114.09397244,51.05338313 -114.09335017,51.05341011 -114.09167379,51.05706864 -114.0917623,51.0571091 -114.08597946,51.05952998 -114.08607602,51.05956032 -114.09473956,51.05301895 -114.09479052))

That is like I expected. But I'm not finding the way to do the same with the Point.

Does someone have an idea about how to format them properly? I'm just using Point([]) but I'm able to change it

Upvotes: 1

Views: 710

Answers (1)

Anatolii Suhanov
Anatolii Suhanov

Reputation: 2682

The following code

const addresses = [{
    "type": "Feature",
    "properties": {},
    "geometry": {
        "type": "Point",
        "coordinates": [-114.0926087, 51.05883014]          
    }
}, {
    "type": "Feature",
    "properties": {},
    "geometry": {
        "type": "Point",
        "coordinates": [-115.0926087, 52.05883014]          
    }
}]; 

const multi = new ol.geom.MultiPolygon([[addresses.map((f) => f.geometry.coordinates)]]);
console.log(new ol.format.WKT().writeGeometry(multi));

produces

MULTIPOLYGON(((-114.0926087 51.05883014,-115.0926087 52.05883014)))

in the browser. Is that somewhat close?

Upvotes: 1

Related Questions