I've declare the following JsType in order to manipulate [GeoJson][1] compliant data :
@JsType
public class FeatureCollection extends GeoJson {
@JsProperty
private Feature[] features;
public FeatureCollection() {
super("FeatureCollection");
features = new Feature[]{};
}
public Feature[] getFeatures() {
return features;
}
Sometimes I need to pass my FeatureCollection
object to external libraries (like Turfs.js for instance to perform unit conversion) which access data throught features
properties. The lib returns me a new object with same properties (they follow the GeoJson RFC like my JsType) but I can't cast it back to FeatureCollection
:
FeatureCollection fc = new FeatureCollection();
Object o = TurfUtils.toWgs84(fc); // Works and give an object which respect the FeatureCollection scheme (ie an array of Features) when I print it on the javascript console.
FeatureCollection featureCollection = TurfUtils.toWgs84(fc); // Throw a java.lang.ClassCastException
The Turf library is JsInteroped:
@JsType(isNative = true, namespace = GLOBAL, name = "turf")
public class TurfUtils {
public static native <T extends GeoJson> T toWgs84(T geojson);
}
When making my FeatureCollection a native JsType, it works but prevent me to use my current constructor, so I'm looking for a way to cast back a javascript object to my JsType. [1]: https://www.rfc-editor.org/rfc/rfc7946