Select to view content in your preferred language

Accessing BuildingSceneLayer component data

694
2
Jump to solution
12-09-2021 01:55 AM
davideprovasoli1
New Contributor

Hello everyone,

I import a BuildingSceneLayer in a webscene and and when I click on a component of the 3d model a popup appears with information about that component. I want to access some of these information and use it. I thought it should be straightforward but I cannot seem to extract these information.

I tried with an 'on-click ' event and 'hitTest':

view.on("click", (event) => {
    view.hitTest(event).then((response) => {
    // check if a graphic is returned from the hitTest
    if (response.results[0].graphic) {
         // Create query object: by specifying objectIds, the query will return results only for
         // the feature matching the graphic's objectid
         const query = new Query({
              attributes: [
                    response.results[0].graphic.attributes
              ],
               // indicates the query should return all attributes
               outFields: ["*"]
        });
        console.log('query', query)
        }
    });
});

 

but only the first of the entries displayed in the popup in found in 'attributes' (see attached screenshots).

Thank you in advance for the time and help.

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi @davideprovasoli1

Thanks for reaching out! I think I found what is missing in your code.

Firstly, in order to read the attributes after a hitTest, you need to make them accessible first by configuring the property outFields on the BuildingSceneLayer (see this link, under "Querying"). If you want to make all attributes accessible, use "*". But this is not advised, since it slows down the whole process, so it is better to list only the attributes which are needed. 

Secondly, you actually don't need to make a query. After adding the necessary attributes in the outFields property, you can access them directly from the result of the hitTest by reading response.results[0].graphic.attributes

Here a code snippet:

buildingSceneLayer.outFields = ["*"];   // Make all of the attributes fields accessible 
view.on("click", (event) => {
  view.hitTest(event).then((response) => {
    // check if a graphic is returned from the hitTest
    if (response.results[0].graphic) {
      // Read the attributes directly from the results from the hitTest
      console.log(response.results[0].graphic.attributes);
    }
  });
});

I hope this helps, if you have further questions just ask 😉

Daniel

View solution in original post

0 Kudos
2 Replies
by Anonymous User
Not applicable

Hi @davideprovasoli1

Thanks for reaching out! I think I found what is missing in your code.

Firstly, in order to read the attributes after a hitTest, you need to make them accessible first by configuring the property outFields on the BuildingSceneLayer (see this link, under "Querying"). If you want to make all attributes accessible, use "*". But this is not advised, since it slows down the whole process, so it is better to list only the attributes which are needed. 

Secondly, you actually don't need to make a query. After adding the necessary attributes in the outFields property, you can access them directly from the result of the hitTest by reading response.results[0].graphic.attributes

Here a code snippet:

buildingSceneLayer.outFields = ["*"];   // Make all of the attributes fields accessible 
view.on("click", (event) => {
  view.hitTest(event).then((response) => {
    // check if a graphic is returned from the hitTest
    if (response.results[0].graphic) {
      // Read the attributes directly from the results from the hitTest
      console.log(response.results[0].graphic.attributes);
    }
  });
});

I hope this helps, if you have further questions just ask 😉

Daniel

0 Kudos
davideprovasoli1
New Contributor

Thank you very much that is exactly what I was missing!

Best. Davide