2

How to convert Feature type into GeoJSON feature?

I have tried this:

var feature: FeatureLike;
var writer = new GeoJSON();
const res = writer.writeFeatureObject(feature);
console.log(res);
2
  • The GeoJSON writeFeatureObject method only takes a Feature. If you had a RenderFeature you would need to convert it to a Feature const res = writer.writeFeatureObject(feature instanceof RenderFeature ? toFeature(feature) : feature); openlayers.org/en/latest/apidoc/…
    – Mike
    Commented Dec 14, 2022 at 14:57
  • Thank you lot! Post it as answer, I will accept
    – Beginner
    Commented Dec 14, 2022 at 14:59

1 Answer 1

3

The GeoJSON writeFeatureObject method only takes a Feature. If you had a RenderFeature you would need to convert it to a Feature

const res = writer.writeFeatureObject(feature instanceof RenderFeature ? toFeature(feature) : feature);

https://openlayers.org/en/latest/apidoc/module-ol_render_Feature.html#.toFeature

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.