This project is read-only.

Creating Label using FeatureSet

May 16, 2013 at 11:22 AM
Edited May 16, 2013 at 11:32 AM
Hi All,

Using shapefiles labeling works like a charm using the following
 IFeatureLayer CountriesLayer = (IFeatureLayer)WebMap1.AddLayer(BasePath + @"\United States states 2011_AREAS.SHP");
            PolygonSymbolizer SymbCountries = new PolygonSymbolizer(Color.FromArgb(0, 191, 0));
            SymbCountries.SetFillColor(Color.Transparent);
            SymbCountries.OutlineSymbolizer = new LineSymbolizer(Color.Magenta, 1);
            SymbCountries.ScaleMode = ScaleMode.Simple;
            CountriesLayer.Symbolizer = SymbCountries;

            MapLabelLayer  thislabelLayer = new MapLabelLayer();
            CountriesLayer.LabelLayer = thislabelLayer;
            CountriesLayer.ShowLabels = true;

            WebMap1.MapViewExtents = CountriesLayer.Extent;



            thislabelLayer.Symbology.Categories[0].Expression = "[" + CountriesLayer.DataSet.DataTable.Columns[2].ColumnName+"]";
            thislabelLayer.Symbolizer.ScaleMode = ScaleMode.Simple;
            thislabelLayer.Symbolizer.Orientation = ContentAlignment.TopCenter;
            thislabelLayer.Symbolizer.LabelPlacementMethod = LabelPlacementMethod.Centroid;
            thislabelLayer.Symbolizer.PartsLabelingMethod = PartLabelingMethod.LabelLargestPart;
            thislabelLayer.Symbolizer.FontSize =7.0F;
Now when I create a featureset using the following code.

http://science.nature.nps.gov/im/units/ARCN/data_management/DotSpatialCodeExamples/index.htm?CreateFeatureSetfromaSQLServerqu.html

Can anyone help me how to display this featureset on the maps and label them?

Regards
psarun
May 16, 2013 at 1:03 PM
I create so called "spatialite" layer too and there is no chance to do "Label Setup" from legend or to do "Export Data All features" too (shape file from this action is corrupted).

I am very interested for help on this.

regards, Vojko
May 28, 2013 at 5:33 PM
Here is the code i use... it shows the layer from an SQL string input
 Private Sub MaakSwungAdresTabelLayer(ByVal strSwungPadFile, ByVal ProgressieWaarde)
        Try
            'Maak een nieuwe datatabel voor tabellen uit de database
            Dim dtAdres As New DataTable()
            'Maak een connectie met de database en vul de tabel
            Dim conSwungDatabase As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strSwungPadFile & ";Persist Security Info=False;User Id=admin;Password=;")
            conSwungDatabase.Open()
            Using cmd As New System.Data.OleDb.OleDbCommand()
                cmd.Connection = conSwungDatabase
                cmd.CommandText = "SELECT tblACN.Postcode, tblACN.Huisnummer, tblACN.Toevoeging, tblACN.Gemeente, tblACN.Woonplaats, tblACN.Straatnaam, tblACN.HERKOMST, tblACN.X, tblACN.Y, tblACN.BAG_PandID, tblACN.BAG_Verblijfsobject, tblACNPlus.ID_Bestemmingstype, tblACNPlus.BinnenOnderzoeksgebied, tblACNPlus.NieuwGPP, tblACNPlus.Wm11_56, tblACNPlus.AantalWonEq, tblACNPlus.Tekstlabel1, tblACNPlus.Tekstlabel2, tblACNPlus.Tekstlabel3, tblACNPlus.[1eHWvoorSan], tblACNPlus.StadEnMilieu, tblACNPlus.Art83_84 " _
                                & "FROM tblACN INNER JOIN tblACNPlus ON (tblACN.Toevoeging = tblACNPlus.Toevoeging) AND (tblACN.Huisnummer = tblACNPlus.Huisnummer) AND (tblACN.Postcode = tblACNPlus.Postcode);"

                Dim rdrA As System.Data.OleDb.OleDbDataReader = cmd.ExecuteReader()
                dtAdres.Load(rdrA)
            End Using
            'sluit de connectie
            conSwungDatabase.Close()
            'maak een nieuwe featureset aan
            Dim fsAdres As New FeatureSet(FeatureType.Point)
            'neem de kolommen over
            For Each Col As DataColumn In dtAdres.Columns
                fsAdres.DataTable.Columns.Add(New DataColumn(Col.ColumnName, Col.DataType))
            Next
            'loop through the temporary datatable and load in the features and attributes
            For RowIndex As Integer = 0 To dtAdres.Rows.Count - 1
                'extract the data from the datatable
                Dim Xadres As Double = dtAdres.Rows(RowIndex).Item("X")
                Dim Yadres As Double = dtAdres.Rows(RowIndex).Item("Y")
                'add the feature to the featureset
                fsAdres.AddFeature(New Feature(New Coordinate(Xadres, Yadres)))
                'now update the feature's row with attributes
                For ColumnIndex As Integer = 0 To dtAdres.Columns.Count - 1
                    fsAdres.DataTable.Rows(RowIndex).Item(ColumnIndex) = dtAdres.Rows(RowIndex).Item(ColumnIndex)
                Next
                SwungInleesProgressBar.Value = ProgressieWaarde + (95 - ProgressieWaarde) * (RowIndex / dtAdres.Rows.Count)

            Next
            'apply the projection
            fsAdres.Projection = DefineProjection()
            'fsAdres.SaveAs("c:\test.shp", True)
            '----------------------------------------------------------------------------------
            'Maak een shape aan indien aangevinkt
            Dim Layer As IMapLayer = Form1.myVenster.Layers.Add(fsAdres)
            Dim pPointMapLayer As IMapFeatureLayer
            'Dim fsAdres As IFeatureSet

            If Layer IsNot Nothing Then
                If Layer.ProjectionString Is Nothing Then
                    Layer.Projection = DefineProjection()
                End If
                fsAdres = TryCast(Layer.DataSet, IFeatureSet)

                pPointMapLayer = TryCast(Layer, IMapFeatureLayer)
                pPointMapLayer.Symbology = SetAdresBestemmingstypeLegendSWUNG(fsAdres)
                Return
                fsAdres = Nothing
            Else
                Return
            End If
            '----------------------------------------------------------------------------------
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "Fout in MaakSwungAdresTabelLayer")
        End Try
    End Sub
May 29, 2013 at 9:56 AM
Joska, I see that you made a new shape file with saveas command. This way labelling works OK. But is any chance to label layer made only from featureset built from SQL string (not to operate with shape file)? See DotSpatial code for retrieving the spatialite database:
      SpatiaLiteHelper slh = new SpatiaLiteHelper();
      GeometryColumnInfo item = r.DataBoundItem as GeometryColumnInfo;                   
      IFeatureSet fs = slh.ReadFeatureSet(connString, item);
      IMapFeatureLayer lay = mainMap.Layers.Add(fs);

        >>>>>   how to label now this layer in the legend?
Regards, Vojko
May 29, 2013 at 12:56 PM
hi Vojko

I didnt saved it first, look closely its just a commentline i use d for testing....


'fsAdres.SaveAs("c:\test.shp", True)

i am a noob myself so i can not help you really... i survive on other peopls examples myself.... :o(
May 30, 2013 at 3:54 PM
Hi Joska

Joska, please could you review my code for labelling in C#.
  1. This first peace of code is dealing with shape file and labelling works OK, also from legend for "Label Setup" although opening
    the labelling window form reports "List box contains too many items", but labelling from this form works OK


    FeatureSet fs = new FeatureSet();
    string fn = @"C:\lines.shp";
    fs = (FeatureSet)FeatureSet.Open(fn);
    
    IMapFeatureLayer lay = mainMap.Layers.Add(fs);
    
    
    ..... here labelling bellow
  2. in this case fs is returned from SQL query string and labelling is not working, labels are not appear in the map at all.
    All data operations for this newly created layer from legend and map works OK except "labelling" and "save as SHP" from legend.
    IFeatureSet fs = slh.ReadFeatureSet(connString, item);       
    
    
    IMapFeatureLayer lay = mainMap.Layers.Add(fs);
    
    
    
    ..... and labelling now
    lay.Symbolizer = new LineSymbolizer();
    LabelSymbolizer las = new LabelSymbolizer()
    {
        FontFamily = "Tahoma",
        FontColor = Color.Black,
        FontSize = 8,
    };
    
    ILabelCategory lac = new LabelCategory()
    {
        Expression = "[OPIS] [STEV]",
        Symbolizer = las
     };
    
    lay.LabelLayer = new MapLabelLayer(fs);
    lay.ShowLabels = true;
    lay.LabelLayer.Symbology.Categories.Clear();
    lay.LabelLayer.Symbology.Categories.Add(lac);
    lay.LabelLayer.CreateLabels();
    
    mainMap.Refresh();
    
    
    
Is this above approach equivalent to your example?
May 31, 2013 at 1:04 PM
mmm Vojko i dont think i can really help you as i am a noob in this as well...

my programm is pretty advanced already but as long i didnt encountered a certain problem myself I cant help you..... I let the user itself do yhe labelling....

so sorry..... but i will look into it myself and try to make a button which immediatly label a whole layer
Nov 29, 2013 at 12:48 AM
This is what i use...
              Form1.myVenster.AddLabels(layer, "[Huisnummer][Toevoeging]", New Font("Arial", 8, FontStyle.Bold), Color.Black)