1

First, I'm new to SharpMap.
I'm adding in labels for a vector layer, but for some reason the labels are not showing up. My PostGIS data is in Geographic NAD 83 and the UI is OpenLayers so am converting it to Google's CRS.

This is what my tile request looks like:

http://lh/customwms/wms.ashx?LAYERS=uscounties&SERVICE=WMS&VERSION=1.3.0&FORMAT=image%2Fpng&TRANSPARENT=TRUE&FILTER=state%20%3D%20'FL'&EXCEPTIONS=INIMAGE&REQUEST=GetMap&STYLES=&MAP_TYPE=PGS&CRS=EPSG%3A900913&BBOX=-9179495,2913453,-8940360,3152589&WIDTH=256&HEIGHT=256

This is what I'm doing to get my Map object.

        public static Map CreateMap(List layerRequests)
        {
            var map = new Map(new Size(1, 1));
            foreach (var layerRequest in layerRequests)
            {
                var vectorLayer = CreateVectorLayer(layerRequest);
                var labelLayer = CreateLabelLayer(vectorLayer, layerRequest);
                map.Layers.Add(vectorLayer);
                map.Layers.Add(labelLayer);
            }
            return map;
        }

        private static LabelLayer CreateLabelLayer(VectorLayer vectorLayer, VectorLayerRequest layerRequest)
        {
            return new LabelLayer("Labels:" + vectorLayer.LayerName)
            {
                DataSource = vectorLayer.DataSource,
                Enabled = true,
                LabelColumn = GetLabelColumnName(layerRequest),
                SRID = vectorLayer.SRID,
                TargetSRID = vectorLayer.TargetSRID,
                CoordinateTransformation = vectorLayer.CoordinateTransformation,
                Style = new LabelStyle
                {
                    ForeColor = Color.Black,
                    Font = new Font(FontFamily.GenericSerif, 11),
                    HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
                    VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,
                    Offset = new PointF(3, 3),
                    Halo = new Pen(Color.Yellow, 2)
                },
                TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias,
                SmoothingMode = SmoothingMode.AntiAlias
            };
        }

        private static string GetLabelColumnName(VectorLayerRequest layerRequest)
        {
            switch (layerRequest.Table.ToLower())
            {
                case "uscounties":
                case "states":
                    return "name";
                default:
                    return "district";
            }
        }

        private static VectorLayer CreateVectorLayer(VectorLayerRequest layerRequest)
        {
            //var cacheKey = String.Format("{0}_{1}_{2}:{3}", 
            //    layerRequest.Schema, 
            //    layerRequest.Table, 
            //    layerRequest.SRID,
            //    layerRequest.Filter == null ? "" : layerRequest.Filter.Replace(" ", ""));
            //return VectorLayersCache.GetOrPut(cacheKey, () =>
            //{
                int? originalSrid = null;
                if (layerRequest.SRID != GeographicNad1983Srid)
                {
                    originalSrid = layerRequest.SRID;
                    layerRequest.SRID = GeographicNad1983Srid;
                }

                var dataSource = new PostGIS(ConnectionString, layerRequest.Table, GeometryColumnName, SpatialTableIdColumnName)
                {
                    Schema = layerRequest.Schema,
                    SRID = layerRequest.SRID,
                    DefinitionQuery = layerRequest.Filter
                };
                return new VectorLayer(layerRequest.Table, dataSource)
                {
                    SRID = GeographicNad1983Srid,
                    TargetSRID = originalSrid.HasValue ? originalSrid.Value : layerRequest.SRID,
                    CoordinateTransformation = ProjHelper.LatLonToGoogle(),
                    Style = layerRequest.Style,
                    SmoothingMode = SmoothingMode.AntiAlias,
                    LayerName = layerRequest.Table,
                    Enabled = true,
                    IsQueryEnabled = true
                }; 
            //});
        }

When I do the above directly through an MVC controller and zoom to map extents and spit out an image, I get the labels layer to show up over the vector layer in the image. However since the calls from my client (using OpenLayers) are discrete, I suspect that the labels are not being shown.

So how do we show labels when using SharpMap WMS?

1 Answer 1

0

I finally got labels to show up in SharpMap's WMS over a vector layer. The trick was to make sure that a label layer was being requested from the WMS. In other words I made sure the LAYER parameter in the WMS tile request looked like this: LAYER=MyLayer,Label_MyLayer Then in the Helper class (from the example code), I detected that if it is a layer looking like "Label_XYZ", I treated it as a request to add the labels to the XYZ vector layer (making sure the vector is requested in the first place).

The reason I had to do that was because in the call to WmsServer.ParseQueryString, if you look under the code that processes "GetMap", there is very limited handling for LabelLayer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.