Select to view content in your preferred language

ComboBox to add layers from folder, selected from .txt list in same folder

693
4
02-09-2023 11:50 AM
Dood345
New Contributor II

I think I've gotten close to getting this working but the function to read in the table stored in the .txt file can not read the table. I am probably using the wrong method, I'm using a FileSystemDatastore.OpenDataset(Of Table)(...).

Dood345_0-1675974127252.png

 

Below is a sample of the list of layers .txt (organized like a .csv):

LayerName,Path
Annotation Text,DRIVE:\FileLocation\LayerFiles\ANNOTATION TEXT.lyr
-----Land & Property (Choose from items below)-----,NOTHING

...

Below is a sample of the first read in of the table/.txt:

Public Async Sub UpdateCombo()

Dim WorkspaceF As FileSystemDatastore
Dim FileConnection As FileSystemConnectionPath
Dim Table As Table
Dim Cur As RowCursor
Dim Row As Row

Dim GDBLocation As Uri
GDBLocation = New Uri("DRIVE:\FileLocation\LayerFiles\")

Try
Await QueuedTask.Run(Sub()
If Not File.Exists(FILE_NAME) Then
MsgBox("File Does Not Exist")

End If
FileConnection = New FileSystemConnectionPath(GDBLocation, FileSystemDatastoreType.Shapefile)
WorkspaceF = New FileSystemDatastore(FileConnection)
Table = WorkspaceF.OpenDataset(Of Table)("coa_LAYERS_list.txt")
MsgBox(Table.GetCount)
Cur = Table.Search(Nothing, False)
Cur.MoveNext()
Row = Cur.Current
Do Until Row Is Nothing

Add(New ComboBoxItem(Row.Item(0).ToString))
Cur.MoveNext()
Row = Cur.Current
Loop

Table = Nothing
Row = Nothing
WorkspaceF = Nothing
FileConnection = Nothing
Cur = Nothing
GC.Collect()
End Sub)

 I do use " (QF.WhereClause = "LayerName = '" & Value & "'") " later but for now I'm just trying to figure out why the table is not being read.

0 Kudos
4 Replies
NarelleChedzey
Esri Contributor

Hi, 

 

Seeing you're reading just a .txt file - I would recommend using standard .NET read/write classes/methods to open your file rather than the FileSystemConnectionPath object. 

https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/drives-directories...

This link has a code snippet which seems to suit your situation -  opening a text file and reading lines. 

 

Thanks

Narelle

Dood345
New Contributor II

A bit late on the response but Exams in college got the better of me. I was trying to avoid that since my focus for this project was to learn more about the add-in API. That being said, I went ahead and swapped to using the basic library (visual basic), and just used a delimited line reader, worked great.

Now I can not for the life of me figure out why it won't add the layer. I have the "OnSelectionChange" method overwritten with 

Protected Overridable Overloads Sub OnSelectionChange(layerName As ComboBoxItem)

If (layerName.ToString = "") Then
MsgBox("Could not read layer name" + layerName.ToString)
Return
End If

MsgBox("getting layer file " + layerName.ToString)

GetLayerFile(layerName.ToString)
MyBase.OnSelectionChange(layerName)

End Sub



However neither msg box appears, and then GetLayerFile is 

Public Sub GetLayerFile(ByVal layerName As String)

Dim activeView As MapView
activeView = MapView.Active

If Not File.Exists(FILE_NAME) Then
MsgBox("File Does Not Exist")
Return
End If

Using lineReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FILE_NAME)

lineReader.TextFieldType = FileIO.FieldType.Delimited
lineReader.SetDelimiters(",")
Dim lineFields As String()

While Not lineReader.EndOfData
Try
lineFields = lineReader.ReadFields()
If (lineFields(0) = layerName) Then
AddLayerToActiveView(activeView, lineFields(1))
MsgBox(lineFields(1))
Return
End If

MsgBox("Could not find layer")

Catch ex As Exception
' Let the user know what went wrong.
MsgBox("The Layer Path Cannot be read: Line " & ex.Message & "is not valid and will be skipped.")
Return

End Try

End While

End Using

End Sub

I don't know if there's anything blaringly obvious, this is the AddLayerToActiveView method

Public Async Sub AddLayerToActiveView(ByVal activeView As MapView, ByVal layerPathFile As System.String) 
Dim Path As Uri
Path = New Uri(layerPathFile)

Try
Await QueuedTask.Run(Sub()
LayerFactory.Instance.CreateLayer(Path, MapView.Active.Map)
End Sub)
Catch e As Exception
' Let the user know what went wrong.
MsgBox("The Layer Path Cannot be read: code2")
MsgBox(e.Message)

End Try



0 Kudos
NarelleChedzey
Esri Contributor

Hi,

Can you change the definition of your OnSelectionChange function to the following and see if that helps with the method firing when a choice is made in the combobox. 

 

Protected Overrides Sub OnSelectionChange(layerName As ComboBoxItem)

(so it is Overrides not Overridable Overloads)

 

Narelle

 

 

0 Kudos
Dood345
New Contributor II

I think that did it, now I just have to sort out my string comparison to identify the layers to add! Hopefully I'm good from here. Thank you, Narelle, for all the help. 

I think I see why that was an issue, overridable overloads basically means that the method I wrote could be overridden and is just another overload for "onSelectionChange" so might not even be called since the program would just do a standard call not the overloaded version.

0 Kudos