Search code examples
angularxmlhttprequestopenlayers

openLayers addFeature to a vectorsource from xmlhttprequest in angular


I took insperation from https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html where they use

   vectorSource.addFeatures(vectorSource.getFormat().readFeatures(xhr.responseText));

but in angular using typescript addFeatures accepts Feature[] while vectorSource.getFormat().readFeatures(xhr.responseText) gives FeatureLike[].

Argument of type 'FeatureLike[] | undefined' is not assignable to parameter of type 'Feature[]'. Type 'undefined' is not assignable to type 'Feature[]'.ts(2345)

heres the section which errors:

     xhr.onload = () => {
                    if (xhr.status === 200) {
                        source.addFeatures(
                            source
                                .getFormat()
                                ?.readFeatures(xhr.responseText)
                        );
                    } else {
                        console.log('error');
                    }
                };

is there a way to transform/cast FeatureLike to ordinary Feature, or maybe I'm doing this wrong?


Solution

  • found a solution to transform the data by using GeoJSON first (ref:How to reference another answer?)

      xhr.onload = () => {
                        if (xhr.status === 200) {
                            let features = new GeoJSON({
                                featureProjection: 'EPSG:32633',
                            }).readFeatures(xhr.responseText);
                            source.addFeatures(features);
                        } else {
                            console.log('error');
                        }
                    };