Working with KnowledgeGraphLayer

Note: Sign in to access the data in this sample. username: viewer01 password: I68VGU^nMurF

Getting Started

A knowledge graph allows you work with a graph network. This network connects people, places, and things (represented by entities) with each other through relationships that define how they are associated. Both entities and relationships can have associated properties.
An entity with a spatial location can be connected with other entities that do not have a spatial location.

A KnowledgeGraphLayer is a composite layer that represents a knowledge graph service on a map. The layer contains sublayers for each entity type and relationship type contained in the KnowledgeGraphLayer.

The sample dataset contains observations of bumble bees made at locations around the United States. Each observation was made and verified by users and is of a specific species of bumble bee.

sample-data-model

Known Limitations

A KnowledgeGraphLayer can be added to an instance of a map but it cannot be saved as an ArcGIS Enterprise portal item or added to an a web map in ArcGIS Map Viewer. KnowledgeGraphLayer can only be used with KnowledgeGraphServices on ArcGIS Enterprise 11.1 or later.

For additional information on working with knowledge graph services see:

How it works

To create a KnowledgeGraphLayer from a knowledge graph service, set the url property to the REST endpoint of the service. A KnowledgeGraphLayer also optionally takes an inclusion definition. The inclusion definition specifies a set of named types and/or specific entities and relationships to be included in the layer. This sample illustrates how to use an inclusion definition to only include the research grade Observations in the layer. These specific observations are determined by first querying the knowledge graph to get just the observations who's property quality_grade is "research". See KnowledgeGraphLayer.inclusionModeDefinition for more on inclusion lists. The inclusionModeDefinition is optional. If excluded, all named types in the array will be included as sublayers. All spatial entity types will be drawn on the map. Any non spatial entity types and all relationship types will be included as table sublayers and will not be drawn on the map.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//create a map of the ids of those research grade observations to include in the layer.
//Since we only returned one variable we know that the first item in the array will be the observation entity
let members = new Map();
for (observation of observationList.resultRows) {
    members.set(observation[0].id, { id: observation[0].id })
}
//Define an inclusion list that limits the layer to only one sublayer for
//only the research grade observation returned from the query.
let namedTypes = new Map();
//only one named type, and only the subset of instance of that type.
namedTypes.set("Observation", { useAllData: false, members: members });
//only create a sublayer for the specified named types.
const inclusionDef = {
    generateAllSublayers: false,
    namedTypeDefinitions: namedTypes
}

//Create the layer
//The inclusionModeDefinition is optional. If excluded, all named types in the array will be included as sublayers.
//Any non spatial entity types and all relationship types will be included as table sublayers and will not be drawn on the map.
const compositeLayer = new KnowledgeGraphLayer({
    url: url,
    inclusionModeDefinition: inclusionDef
});

Define the renderer and labels for each named type. In this example the inclusion list contains only one named type so only one renderer and label definition is need.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//define renderer for the geometry (all of the Observations are point geometry)
const observationRenderer = {
    type: "simple", // autocasts as new SimpleFillSymbol()
    symbol: {
        type: "simple-marker",
        size: 6,
        color: "yellow",
        outline: {  // autocasts as new SimpleLineSymbol()
            width: 0.5,
            color: "black"
        }
    }
};
//define labels
const observationLabels = new LabelClass({
    labelExpressionInfo: {
        expression: `$feature.species_guess`
    },
    labelPlacement: "above-center",
    symbol: {
        type: "text",  // autocasts as new TextSymbol()
        color: "black"
    }
})

Enable popups and apply a renderer to the sublayers once the composite layer is loaded. The labels and renderers are applied to the KnowledgeGraphSublayer that represents each named type included in the KnowledgeGraphLayer (i.e. each entity type or relationship type).

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
//once the layer has loaded, enable popups and apply the renderer to each sublayer
//since an inclusion list is set to only return Observations, there will only be one sublayer
await compositeLayer.load()
compositeLayer.layers.items.forEach((sublayer) => {
    sublayer.popupTemplate = sublayer.createPopupTemplate();
    sublayer.renderer = observationRenderer;
    sublayer.labelingInfo = [observationLabels];
    sublayer.labelsVisible = true;
map.add(compositeLayer);
});

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.