How to get the name of a loaded GeoJSONDataSource?

How do I get the name of the GeoJsonDataSource('pois') loaded in Cesium?
When I click on an associated object of the DataSource the name of the individual object is available in the attributes. But I cannot find the name of the entire DataSource (the “layer name”).
I query the objects with viewer.scene.pick(evtObj.position) where the name of the DataSource (“pois”) does not seem to exist.

It looks like the name given to a GeoJsonDataSource is the name of the file loaded. So you should be able to retrieve it by name like this:

var ds = new Cesium.GeoJsonDataSource('test');
ds = ds.load('../SampleData/ne_10m_us_states.topojson');

Okay, this is unfavorably. Because I thought that by clicking on a feature of a GeoJsonDataSoruce it is also possible to query the name of the DataSource associated with the feature. Unfortunately, the file name is also not available in the feature as result of a viewer.scene.pick(evtObj.position).

Are you talking about the names of the individual features (like polygons, lines etc) in the GeoJSON? If so, the below code works for me when testing it in this GeoJSON Sandcastle:

viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
    var pickedFeature = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedFeature)){
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

What do you get when you try this?

Thanks for your answer. This works as expected and returns the name of the single clicked feature. But, to stay with this example:
If I click on the feature for the state e.g. “Georgia”, I get the name of the clicked feature. But I would like to get the name of the GeoJsonDataSource to which the feature in the click event belongs (= “10m_us_states.topojson”).
This is similar to the getFeature of GIS like QGIS. If I click on a feature, the name of the corresponding layer is displayed in addition to the attributes of the feature.

Ok, I see what you mean now. I don’t believe CesiumJS keeps this information in the created entities, but all you’d have to do is add it upon loading the GeoJSON like this:

var promise = Cesium.GeoJsonDataSource.load('../SampleData/ne_10m_us_states.topojson');
promise.then(function(dataSource) {
    // Link each entity to its data source
    var entities = dataSource.entities.values;
    for (let entity of entities) {
        entity.dataSource = dataSource;

And then when you click on the feature you can get its data source name through that. Here’s a modified Sandcastle that will print the name of the picked feature itself as well as the GeoJSON it came from.

1 Like

This is what I needed.

1 Like