Select to view content in your preferred language

Converting array to a table

1738
9
03-09-2011 11:02 AM
JoannaLaroussi
Emerging Contributor
I am preparing a report based on selected records from a feature class. Using Arc Objects I select values from each record/row in fields street, fromLeft, toLeft, fromRight, toRight and concatenate it into one line for each one record/row. Final selection is exported to pdf file. Because streets names have a different length my output looks messy. I wish I can build a table, where each column will store one field. I also would like to be able sort my records by street name before it produces final output. Any ideas how to change this:

IFeatureCursor pedCursor = featureClassPed.Search(queryAPedFilter, true);
IFeature pedFeature = pedCursor.NextFeature();
while (pedFeature != null)
{
    //getting the values for each field and saving it into variables
    IFields pedAFields = featureClassPed.Fields;
    int distanceAField = pedAFields.FindField("S_00000");
    int streetAField = pedAFields.FindField("Street");
    int fromALeftField = pedAFields.FindField("FromLeft");
    int toALeftField = pedAFields.FindField("ToLeft");
    int fromARightField = pedAFields.FindField("FromRight");
    int toARightField = pedAFields.FindField("ToRight");
    //saving variables
    string streetAValue = pedFeature.get_Value(streetAField).ToString();
    string fromALeftValue = pedFeature.get_Value(fromALeftField).ToString();
    string toALeftValue = pedFeature.get_Value(toALeftField).ToString();
    string fromARightValue = pedFeature.get_Value(fromARightField).ToString();
    string toARightValue = pedFeature.get_Value(toARightField).ToString();
    //building an array
    string[] pedA = new string[9] { streetAValue, "      ", fromALeftValue, "      ",  toALeftValue, "      ", fromARightValue, "      ", toARightValue };
              
    outputA += string.Join(" ", pedA) + "\r\n";
               
    pedFeature = pedCursor.NextFeature();
}

into a table?

Here is an example of output:

LAFAYETTE ST 33 51 34 54
CRAIG AV 538 552 537 549
FINLAY ST 1 19 2 20
AMBOY RD 7660 7682 7657 7679
BENTLEY ST 131 177 128 180

Which I rather see as:

AMBOY RD 7660 7682 7657 7679
BENTLEY ST 131 177 128 180
CRAIG AV 538 552 537 549
FINLAY ST 1 19 2 20
LAFAYETTE ST 33 51 34 54
0 Kudos
9 Replies
BrianBottoms
Emerging Contributor
I replace text place holders with dynamic tabular data when batching maps. My solution to get the columns to line up in a clean order was to write a routine that samples the maximum possible string length for each field and then buffer each string for that field to the right with the needed spaces to go one more than the maximum. This way every string is the same length for each field and each column is separated by a space. I then concatenate all the strings for each record on a new line in the string. The key to making this all work is to use a monotype font i.e. a font where each character takes up the same amount of horizontal space, such as Courier New.

As far as sorting, my first thought would be to convert your records to an object, add your objects to a collection such as an arraylist and sort using IComparer on the road name property of the object.

Brian
0 Kudos
JoannaLaroussi
Emerging Contributor
Thank you for your response. Do you have maybe any example of code to �??convert your records to an object, add your objects to a collection such as an arraylist and sort using IComparer on the road name property of the object�?�? I am not sure how to do it.
0 Kudos
BrianBottoms
Emerging Contributor
Look up in ArrayList.Sort() method, IComparable, and IComparer in visual studio help. There are also a ton of examples on the web and a whole bunch of ways to sort.

Below is a simple example.

Module Module1
    Public Structure Road
        Implements IComparable
        Public Name As String
        Public Address As String
        Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
            Dim tmpObj As Road = CType(obj, Road)
            Return (Me.Name.CompareTo(tmpObj.Name))
        End Function
    End Structure

    Sub Main()
        Dim rd1 As Road
        rd1.Name = "Big Road"
        rd1.Address = ("1234")
        Dim rd2 As Road
        rd2.Name = "Little Road"
        rd2.Address = "3452"
        Dim rd3 As Road
        rd3.Name = "A Road"
        rd3.Address = "9883"

        Dim al As New ArrayList
        al.Add(rd1)
        al.Add(rd2)
        al.Add(rd3)

        'Unsorted
        Console.WriteLine("Unsorted roads")
        Console.WriteLine("**************")
        Dim o As Object
        For Each o In al
            Console.WriteLine(CType(o, Road).Name)
        Next

        'Sorted on name property
        Console.WriteLine()
        Console.WriteLine("Sorted Records")
        Console.WriteLine("**************")
        al.Sort()
        For Each o In al
            Console.WriteLine(CType(o, Road).Name)
        Next

        Console.ReadLine()

    End Sub



End Module
0 Kudos
JoannaLaroussi
Emerging Contributor
Thank you for your advice. I am not using VB, but I will try to look up ArrayList.Sort() method, IComparable, and IComparer as you suggest.
0 Kudos
JamesCrandall
MVP Alum
I've had a great deal of success with the ADO.NET data classes -- specifically, the DataSet and DataTable objects.  I have several implementations that have highly detailed/customized Crystal Reporting components for reporting and the ADO.NET data model is very compatible with CR and a whole host of other technologies. 

While this may/may not be an exact solution to your needs, you might find a use for the ADO.NET DataTable in your own implementation(s).  Here's a great example of converting a FeatureLayer's attributes into an ADO.NET DataTable.  I've uploaded this onto ArcScripts for everyone to use:

http://resources.arcgis.com/gallery/file/arcobjects-net-api/details?entryID=675318D8-1422-2418-8814-...

My other upload that some might find useful: http://resources.arcgis.com/gallery/file/arcobjects-net-api/details?entryID=6753AB56-1422-2418-7F3C-...
0 Kudos
JoannaLaroussi
Emerging Contributor
Thanks! I am not very much familiar with VB, so as much as I understood the logic behind your script, I am more looking for some C# examples.
0 Kudos
VivekPrasad
Deactivated User
Thanks! I am not very much familiar with VB, so as much as I understood the logic behind your script, I am more looking for some C# examples.


Hi,

Have a look at below thread

http://bytes.com/topic/c-sharp/answers/506528-move-array-table
0 Kudos
VivekPrasad
Deactivated User
Hi,

I think ITableSort interface may help you.

Please have a look at the below sample VBA code, which helps in understanding ITableSort

Dim pTableSort As ITableSort
        pTableSort = New TableSort
        Dim pQueryFilter As IQueryFilter
        pQueryFilter = New QueryFilter
        pQueryFilter.WhereClause = "[STATE_NAME] like  'A%'"
        With pTableSort
            .Fields = "state_name, count_name"
            .Ascending("state_name") = False
            .Ascending("count_name ") = True
            .CaseSensitive("state_name") = True
            .CaseSensitive("count_name ") = True
            .QueryFilter = pQueryFilter
            .Table = pTable
        End With
        pTableSort.Sort(Nothing)
        Dim pCursor As ICursor
        pCursor = pTableSort.Rows
        Dim pRow As IRow
        pRow = pCursor.NextRow
        Do While Not pRow Is Nothing
            Debug.Print(pRow.Value(2) & " , " & pRow.Value(1))
            pRow = pCursor.NextRow
        Loop
0 Kudos
JoannaLaroussi
Emerging Contributor
Thank you!
0 Kudos