Select to view content in your preferred language

How to access to layer description/properties with ArcGIS Pro SDK w/o instantiation the layer

1349
6
Jump to solution
10-05-2022 08:13 AM
Amadeus111
Frequent Contributor

I would like to get layers' description/properties without adding/creating the layer. Is it possible to get the all the info directly from the layer file (.lyr or .lyrx) in ArcGIS Pro SDK. It is fairly simple in ArcPy but I don't wanna switch between ArcPy and the SDK. 

When I open a layer file I get something like this 

Amadeus111_0-1664982740884.png

 

0 Kudos
2 Solutions

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

You want to use the LayerDocument class.

Your workflow will be similar to that shown here in this code snippet: apply-symbology-to-a-layer-from-a-layer-file (there are also a number of other snippets on the LayerDocument reference page). 

 

View solution in original post

Amadeus111
Frequent Contributor

@CharlesMacleod Never mind, I figured it out. 

QueuedTask.Run(() =>
                {
                    LayerDocument lyrDocFromLyrxFile = new LayerDocument(layerPath);
                    var jsonfile = lyrDocFromLyrxFile.AsJson();
                    dynamic array = JsonConvert.DeserializeObject(jsonfile);
                    string nl = "\r\n";
                    var geometry = "Geometry : " + array.layerDefinitions[0].featureTemplates[0].tags.ToString() + nl;
                    var descriptionAll = array.binaryReferences[0].data.ToString();
                    var fufu = array.binaryReferences[0].uRI.ToString();


                    XmlDocument xmltest = new XmlDocument();
                    xmltest.LoadXml(descriptionAll);

                    string creationDate;
                    XmlNodeList xmlNodeListCreationDate = xmltest.GetElementsByTagName("CreaDate");
                    if (xmlNodeListCreationDate.Count > 0)
                        creationDate = "Creation Date : " + xmlNodeListCreationDate[0].InnerXml + nl;
                    else
                        creationDate = "Creation Date : No info" + nl;

                    string projection;
                    XmlNodeList xmlNodeListProjection = xmltest.GetElementsByTagName("projcsn");            
                    if(xmlNodeListProjection.Count > 0)
                       projection = "Projection : " + xmlNodeListProjection[0].InnerXml + nl;
                    else
                        projection = "Projection : No info" + nl;

                    string tags;
                    XmlNodeList xmlNodeListTags = xmltest.GetElementsByTagName("keyword");
                    if (xmlNodeListTags.Count > 0)
                        tags = "Tags : " + xmlNodeListTags[0].InnerXml + nl;
                    else
                        tags = "Tags : No info" + nl;

                    string description;
                    XmlNodeList xmlNodeListDescription = xmltest.GetElementsByTagName("idAbs");
                    if (xmlNodeListDescription.Count > 0)
                        description = "Description : " + xmlNodeListDescription[0].InnerXml + nl;
                    else
                        description = "Description : No info" + nl;

                    string credit;
                    XmlNodeList xmlNodeListCredit = xmltest.GetElementsByTagName("idCredit");
                    if (xmlNodeListCredit.Count > 0)
                        credit = "Credit : " + xmlNodeListCredit[0].InnerXml + nl;
                    else
                        credit = "Credit : No info" + nl;

                    string limitation;
                    XmlNodeList xmlNodeListLimitation = xmltest.GetElementsByTagName("useLimit");
                    if (xmlNodeListCredit.Count > 0)
                        limitation = "Use Limitations: " + xmlNodeListLimitation[0].InnerXml + nl;
                    else
                        limitation = "Use Limitations : No info" + nl;

                    DescriptionTextBoxText = geometry + creationDate + projection + tags + description + credit + limitation;
                });

View solution in original post

0 Kudos
6 Replies
CharlesMacleod
Esri Regular Contributor

You want to use the LayerDocument class.

Your workflow will be similar to that shown here in this code snippet: apply-symbology-to-a-layer-from-a-layer-file (there are also a number of other snippets on the LayerDocument reference page). 

 

Amadeus111
Frequent Contributor

@CharlesMacleod Thanks for the reply. I was able to use this for .lyrx extension but it does not work for the .lyr. All my files are .lyr files. 

 

for the .lyr extension layer files I get this

Amadeus111_0-1665073690429.png

 

Do you know if there is a solution for this? 

I will mark your answer as a solution anyway since it gives a solution to my question. 

0 Kudos
CharlesMacleod
Esri Regular Contributor

Hi Amadeus, i checked with the development team. Only JSON format is supported for LayerDocument. In Pro version 1.x, layer files with .lyr suffix could also be created with JSON format - hence the inclusion of both .lyr and .lyrx suffixes in the code example I referenced. The API reference for LayerDocument does correctly mention that .lyrx is the intended format.

Arcmap .lyr files, which are binary format, are not supported via LayerDocument. Therefore the only workaround would be to create the layer via LayerFactory (which I think u were trying to avoid).

Amadeus111
Frequent Contributor

@CharlesMacleod Thanks for the inquisition, yes unfortunately I dont have lyrx option at the moment. I will see if I can solve this in another way. I will share it here if I do. 

0 Kudos
Amadeus111
Frequent Contributor

@CharlesMacleod 

I am using one of the .lyrx files to get the info from the layer document.

I was able to get geometry type by using JsonConvert.DeserializeObject(jsonfile)

but some of the info looks like in XML format 

Amadeus111_0-1666013375235.png

 QueuedTask.Run(() =>
                {
                    LayerDocument lyrDocFromLyrxFile = new LayerDocument(layerPath);
                    var jsonfile = lyrDocFromLyrxFile.AsJson();
                    dynamic array = JsonConvert.DeserializeObject(jsonfile);
                    string nl = "\r\n";
                    var geometry = "Geometry: " + array.layerDefinitions[0].featureTemplates[0].tags.ToString() + nl;
                    var description = "Description: " + array.binaryReferences[0].data + nl; //This is in XML how can I read child nodes and convert to string
                
                    DescriptionTextBoxText = geometry + date.ToString();
                    
                });

//Is there way to get child nodes in string?
0 Kudos
Amadeus111
Frequent Contributor

@CharlesMacleod Never mind, I figured it out. 

QueuedTask.Run(() =>
                {
                    LayerDocument lyrDocFromLyrxFile = new LayerDocument(layerPath);
                    var jsonfile = lyrDocFromLyrxFile.AsJson();
                    dynamic array = JsonConvert.DeserializeObject(jsonfile);
                    string nl = "\r\n";
                    var geometry = "Geometry : " + array.layerDefinitions[0].featureTemplates[0].tags.ToString() + nl;
                    var descriptionAll = array.binaryReferences[0].data.ToString();
                    var fufu = array.binaryReferences[0].uRI.ToString();


                    XmlDocument xmltest = new XmlDocument();
                    xmltest.LoadXml(descriptionAll);

                    string creationDate;
                    XmlNodeList xmlNodeListCreationDate = xmltest.GetElementsByTagName("CreaDate");
                    if (xmlNodeListCreationDate.Count > 0)
                        creationDate = "Creation Date : " + xmlNodeListCreationDate[0].InnerXml + nl;
                    else
                        creationDate = "Creation Date : No info" + nl;

                    string projection;
                    XmlNodeList xmlNodeListProjection = xmltest.GetElementsByTagName("projcsn");            
                    if(xmlNodeListProjection.Count > 0)
                       projection = "Projection : " + xmlNodeListProjection[0].InnerXml + nl;
                    else
                        projection = "Projection : No info" + nl;

                    string tags;
                    XmlNodeList xmlNodeListTags = xmltest.GetElementsByTagName("keyword");
                    if (xmlNodeListTags.Count > 0)
                        tags = "Tags : " + xmlNodeListTags[0].InnerXml + nl;
                    else
                        tags = "Tags : No info" + nl;

                    string description;
                    XmlNodeList xmlNodeListDescription = xmltest.GetElementsByTagName("idAbs");
                    if (xmlNodeListDescription.Count > 0)
                        description = "Description : " + xmlNodeListDescription[0].InnerXml + nl;
                    else
                        description = "Description : No info" + nl;

                    string credit;
                    XmlNodeList xmlNodeListCredit = xmltest.GetElementsByTagName("idCredit");
                    if (xmlNodeListCredit.Count > 0)
                        credit = "Credit : " + xmlNodeListCredit[0].InnerXml + nl;
                    else
                        credit = "Credit : No info" + nl;

                    string limitation;
                    XmlNodeList xmlNodeListLimitation = xmltest.GetElementsByTagName("useLimit");
                    if (xmlNodeListCredit.Count > 0)
                        limitation = "Use Limitations: " + xmlNodeListLimitation[0].InnerXml + nl;
                    else
                        limitation = "Use Limitations : No info" + nl;

                    DescriptionTextBoxText = geometry + creationDate + projection + tags + description + credit + limitation;
                });
0 Kudos