I am trying to retrieve the extent of a KML layer I am bringing into OpenLayer.
When I open the KML layer in QGIS, I see there is extent information attached to it that I would like to use inside OpenLayer.
(Note: that extent information is "correct" => matches my expectations)
But when I load the layer in my script using VectorSource
and format: new KML()
, it seems as though the extent information is lost and turned into [Infinity, Infinity, -Infinity, -Infinity]
.
import VectorSource from "ol/source/Vector.js";
import KML from "ol/format/KML.js";
const model = new VectorSource({
url: ".....",
format: new KML(),
});
console.log("model");
console.log(model);
console.log("model.getExtent()");
console.log(model.getExtent());
My question: Is there a way to programmatically retrieve the extent information I see in QGIS directly from within my OpenLayer script (by reading a property of the imported object rather than copy pasting manually from QGIS or some other data-source)?
To load the source you need to add it (in a layer) to a map
const model = new VectorSource({
url: ".....",
format: new KML(),
});
model.once('featuresloadend', () => {
console.log(model.getExtent());
});
map.addLayer(
new VectorLayer({
source: model,
})
);
Alternatively fetch the url and read it with the KML format parser
const model = new VectorSource();
fetch( ".....")
.then((response) => response.text())
.then((text) => {
model.addFeatures(
new KML().readFeatures(text)
);
console.log(model.getExtent());
});