ThinkGeo.com    |     Documentation    |     Premium Support

How to set .tif file dynamically from Controller for Raster Layer

Hi,



   I am using Think GEO in ASP.NET MVC .  I set raster layer statically in .cshtml file using bellow code…



.StaticOverlay(overlay =>

                            {

                                GdalRasterLayer gdiPlusImageLayer = new GdalRasterLayer(Server.MapPath(@"~\App_Data\afghanistan2.tif"));

                                gdiPlusImageLayer.UpperThreshold = double.MaxValue;

                                gdiPlusImageLayer.LowerThreshold = 0;



                                overlay.Layer(gdiPlusImageLayer);

                            })



 Now I want to set .tif file dynamically form controller for Raster Layer.



  So, Please guide me.

Hi Gautam,



Thanks for your post, basically, there are two ways to render map in the MVC edition. First is return a empty view to page, then render the map in the page, like the way you are using now:

Controller:


public ActionResult ClickEventOnMarker()
        {
            return View();
        }

Page:



       @{
           Html.ThinkGeo().Map("Map1"new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 510)
               .MapBackground(new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"))))
               .CurrentExtent(-131.22, 55.05, -54.03, 16.91)
               .MapUnit(GeographyUnit.DecimalDegree)
               .CustomOverlays(overlays =>
               {
                   overlays.WorldMapKitWmsWebOverlay();
 
                   overlays.InMemoryMarkerOverlay("MarkerOverlay")
                       .Feature("Kansas"new Feature(-94.48242, 38.75977))
                       .ZoomLevelSet(z =>
                       {
                           z.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
                       })
                       .OnClientClick("clickOnMarker");
               })
               .Render();
       }
   

Second is render the map in the controller then return that map to the page, I think this is the dynamic way you are searching for?

Controller:


public ActionResult ClickEventToHighlightOverlay()
        {
            Map map = new Map("Map1"new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 510);
            map.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
            map.CurrentExtent = new RectangleShape(-131.22, 55.05, -54.03, 16.91);
            map.MapUnit = GeographyUnit.DecimalDegree;
            map.MapTools.OverlaySwitcher.Enabled = true;
 
            GdalRasterLayer gdiPlusImageLayer = new GdalRasterLayer(Server.MapPath(@"~\App_Data\afghanistan2.tif"));
            gdiPlusImageLayer.UpperThreshold = double.MaxValue;
            gdiPlusImageLayer.LowerThreshold = 0;
            map.StaticOverlay.Layers.Add(gdiPlusImageLayer);
 
            return View(map);
        }

View:



        @{
            Html.ThinkGeo().Map(Model).HighlightOverlay(hl =>
                {
                    hl.OnClientClick("highlightOverlayClick");
                })
                .Render();
        }
    



Please feel free to let us know your queries.



Regards,



Gary






Hi,



you can see that only .tif image seen. I want it with map. But Map is not displaying.







 




    In controller: 



 [MapActionFilter]

        public void LoadMap(Map map, GeoCollection args)

        {

            if (null != map)

            {

                LayerOverlay layerOverlay = map.CustomOverlays[“ManageLayer”] as LayerOverlay;

                layerOverlay.Layers.Clear();

                string LayerID = string.Empty;



                if (args[“LayerID”] != null)

                    LayerID = Convert.ToString(args[“LayerID”]);



                string[] strID = LayerID.Split(’,’);

                for (int index = 0; index < strID.Length; index++)

                {

                    if (LayerID == “”)

                        return;



                    ManageLayersService objService = new ManageLayersService();

                    GISLayerCustom obj = objService.GetGISLayerCustomByID(Convert.ToInt64(strID[index]));



                    Session[“DataType”] =Convert.ToString(args[“DataType”]);

                    if (Convert.ToString(args[“DataType”]) == “Raster”)

                    {

                        GdalRasterLayer gdiPlusImageLayer =

                            new GdalRasterLayer(Server.MapPath(@"~" + Convert.ToString(args[“LayerFilePath”])));

                        gdiPlusImageLayer.UpperThreshold = double.MaxValue;

                        gdiPlusImageLayer.LowerThreshold = 0;



                        layerOverlay.Layers.Add(gdiPlusImageLayer);

                    }

                    else

                    {

                        if (obj != null && !string.IsNullOrEmpty(obj.LayerFilePath))

                        {

                            if (obj != null && !string.IsNullOrEmpty(obj.LayerFilePath))

                            {

                                ShapeFileFeatureLayer layer = new ShapeFileFeatureLayer(Server.MapPath(obj.LayerFilePath));



                                if (!string.IsNullOrEmpty(args[“FillBrush”].ToString()) && !string.IsNullOrEmpty(args[“OutLine”].ToString()))

                                    layer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add((AreaStyles.CreateSimpleAreaStyle(GeoColor.FromHtml(args[“FillBrush”].ToString()), GeoColor.FromHtml(args[“OutLine”].ToString()), 1)));



                                if (!string.IsNullOrEmpty(args[“LineColor”].ToString()) && !string.IsNullOrEmpty(args[“LineType”].ToString()))

                                    layer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add((LineStyles.CreateSimpleLineStyle(GeoColor.FromHtml(args[“LineColor”].ToString()), Convert.ToInt32(args[“BoderWidth”]), this.GetLineStyle(args[“LineType”].ToString()), false)));



                                //layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = this.GetZoomScale(obj.ZoomLevel.Value);

                                layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                                if (!string.IsNullOrEmpty(args[“ColumnName”].ToString()) && !string.IsNullOrEmpty(args[“FontName”].ToString()))

                                {

                                    string str = string.Empty;

                                    string[] columnColl = args[“ColumnName”].ToString().Split(’,’);

                                    for (int i = 0; i < columnColl.Length; i++)

                                    {

                                        if (columnColl.Trim() != string.Empty)

                                        {

                                            if (i == 0 && string.IsNullOrEmpty(obj.FieldExtensionText))

                                                str += “[” + columnColl + “]”;

                                            else

                                                str += “_[” + columnColl + “]”;

                                        }

                                    }





                                    TextStyle style = TextStyles.CreateSimpleTextStyle(obj.FieldExtensionText + String.Format(str), this.GetFontName(Convert.ToInt32(args[“FontName”].ToString())), Convert.ToInt32(args[“FontWidth”].ToString()), DrawingFontStyles.Bold, GeoColor.FromHtml("#000000"), 10, 0);

                                    style.OverlappingRule = LabelOverlappingRule.AllowOverlapping;

                                    layer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(style);

                                }

                                layer.Transparency = (!string.IsNullOrEmpty(Convert.ToString(args[“Transparency”])) ? Convert.ToInt32(args[“Transparency”]) : 0);

                                if (!string.IsNullOrEmpty(args[“PointImage”].ToString()) && args[“PointImage”].ToString() == “checked” && !string.IsNullOrEmpty(obj.PointImagePath))

                                    layer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(new PointStyle(new GeoImage(Server.MapPath(obj.PointImagePath))));

                                else if (!string.IsNullOrEmpty(args[“PointStandard”].ToString()) && args[“PointStandard”].ToString() == “checked”)

                                    layer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add((new PointStyle(PointSymbolType.Diamond, new GeoSolidBrush(GeoColor.StandardColors.Green), new GeoPen(GeoColor.StandardColors.Red, 5), 20)));

                                else

                                    layer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(this.GetPointStylesTheme(args[“Default”].ToString()));





                                layer.DrawingMarginPercentage = 100;

                                Proj4Projection proj4 = new Proj4Projection();

                                proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);

                                proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();

                                layer.FeatureSource.Projection = proj4;

                                if (!proj4.IsOpen)

                                    proj4.Open();



                                layerOverlay.Layers.Add(layer);

                            }

                        }

                    }

                }

            }

        }



And In View :



  if (Session[“DataType”] != null && Convert.ToString(Session[“DataType”]) == “Raster”)

                            {

                                Html.ThinkGeo().Map(“Map1”, new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 510)

                                    .MapBackground(new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"))))

                                    .MapUnit(GeographyUnit.DecimalDegree)

                                    .CurrentExtent(-80.5, 68.9, 80, -60.43)

                                    .MapTools(mapTools =>

                                    {

                                        mapTools.MouseCoordinateMapTool().Enabled(true);

                                        mapTools.PanZoomBarMapTool().Enabled(true);

                                    })

                                    .CustomOverlays(overlays =>

                                    {



                                        //overlays.GoogleOverlay(“Google Map”)

                                        //    .JavaScriptLibraryUri(new Uri(ConfigurationManager.AppSettings[“GoogleUriV3”]))

                                        //    .GoogleMapType(GoogleMapType.Normal);



                                        InMemoryFeatureLayer shapeLayer = new InMemoryFeatureLayer();

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.FromArgb(180, 102, 255, 102), 10, GeoColor.StandardColors.DarkGreen, 1);

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.StandardColors.Green, 4, true);

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(180, 102, 255, 102), GeoColor.StandardColors.DarkGreen, 1);

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                                        shapeLayer.DrawingQuality = DrawingQuality.HighQuality;



                                        // Google Map

                                        //overlays.GoogleOverlay(“Google Map”).IsBaseOverlay(true).GoogleMapType(GoogleMapType.Normal);

                                        //OpenStreet Map

                                        //overlays.OpenStreetMapOverlay(“Open Street Map”);

                                        ////Yahoo Map

                                        ////overlays.YahooOverlay(“Yahoo Map”).YahooMapType(YahooMapType.Regular).IsBaseOverlay(true).JavaScriptLibraryUri(new Uri(ConfigurationManager.AppSettings[“YahooUri”]));

                                        //// Bing Map

                                        //overlays.BingMapsOverlay(“Bing Map”).MapType(BingMapsStyle.Road);



                                        overlays.LayerOverlay(“ManageLayer”).IsBaseOverlay(false).TileType(TileType.MultipleTile);



                                    }).StaticOverlay(overlay =>

                                    {

                                        GdalRasterLayer gdiPlusImageLayer = new GdalRasterLayer(Server.MapPath(@"~" + Convert.ToString(Session[“LayerFilePath”])));

                                        //GdalRasterLayer gdiPlusImageLayer = new GdalRasterLayer(Server.MapPath(@"~\App_Data\afghanistan2.tif"));

                                        gdiPlusImageLayer.UpperThreshold = double.MaxValue;

                                        gdiPlusImageLayer.LowerThreshold = 0;



                                        overlay.Layer(gdiPlusImageLayer);

                                    

                                    }).Render();

                            }

                            else

                            {

                                Html.ThinkGeo().Map(“Map1”, new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 510)

                                    .MapBackground(new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"))))

                                    .CurrentExtent(-13939426.6371, 6701997.4056, -7812401.86, 2626987.386962)

                                    .CurrentExtent(6643042.96358, 4644924.705353, 8355232.396930, 3421932.252960)

                                    .MapUnit(GeographyUnit.Meter)

                                    .MapTools(mapTools =>

                                    {

                                        mapTools.MouseCoordinateMapTool().Enabled(true);

                                        mapTools.PanZoomBarMapTool().Enabled(true);

                                    })

                                    .CustomOverlays(overlays =>

                                    {



                                        overlays.GoogleOverlay(“Google Map”)

                                            .JavaScriptLibraryUri(new Uri(ConfigurationManager.AppSettings[“GoogleUriV3”]))

                                            .GoogleMapType(GoogleMapType.Normal);



                                        InMemoryFeatureLayer shapeLayer1 = new InMemoryFeatureLayer();

                                        shapeLayer1.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.PointType = PointType.Bitmap;

                                        shapeLayer1.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image = new GeoImage(HttpContext.Current.Request.MapPath("~/Skin/Img/PointImage.png"));

                                        shapeLayer1.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                                        shapeLayer1.InternalFeatures.Add(“Line”, new Feature(BaseShape.CreateShapeFromWellKnownData(“LINESTRING(60 60, 70 70,75 60, 80 70, 85 60,95 80)”)));

                                        shapeLayer1.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.FromArgb(200, GeoColor.StandardColors.Red), 5);

                                        overlays.LayerOverlay(“DynamicOverlay1”).Layer(“shapeLayer”, shapeLayer1).IsBaseOverlay(false);





                                        InMemoryFeatureLayer shapeLayer = new InMemoryFeatureLayer();

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.FromArgb(180, 102, 255, 102), 10, GeoColor.StandardColors.DarkGreen, 1);

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.StandardColors.Green, 4, true);

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(180, 102, 255, 102), GeoColor.StandardColors.DarkGreen, 1);

                                        shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                                        shapeLayer.DrawingQuality = DrawingQuality.HighQuality;



                                        // Google Map

                                        //overlays.GoogleOverlay(“Google Map”).IsBaseOverlay(true).GoogleMapType(GoogleMapType.Normal);

                                        //OpenStreet Map

                                        overlays.OpenStreetMapOverlay(“Open Street Map”);

                                        //Yahoo Map

                                        //overlays.YahooOverlay(“Yahoo Map”).YahooMapType(YahooMapType.Regular).IsBaseOverlay(true).JavaScriptLibraryUri(new Uri(ConfigurationManager.AppSettings[“YahooUri”]));

                                        // Bing Map

                                        overlays.BingMapsOverlay(“Bing Map”).MapType(BingMapsStyle.Road);

                                        overlays.LayerOverlay(“ManageLayer”).IsBaseOverlay(false).TileType(TileType.MultipleTile);

                                    }).Render();

                            }



Please provide me solution



Thanks,

Gautam


Hi Gautam, 
  
 Seems like the attachment is missed here, could you please add it again? 
  
 Sorry that we are unable to debug the code attached here, seems like there are some stuffs missed. I guess maybe the problem is that the layers loaded in the map, there are 2 kinds of layers, one is in Meter(google or some others), while another one is in Decimal Degree(raster),if we use GoogleMap or some layers in the same projection, the mapUnit should be Meter, thus, we need to project the layers overlapped on the map to meter as well, please have a try and let us know. Also would you please provide us a complete demo? 
  
  
  
 Thanks, 
  
 Johnny 


Hi,



  Please see old post with image. and I changed MapUnit to meter, but unable to find solution.



Please provide me solutions…



Thanks,

Gautam

Hi Gautam,



Sorry that we were still find the file in your previous posts. But after checking the code you attached here further reviewed your code of  View, I have some insights on the issue, there may be no background layer for the Map. Seems like you’ve tried ussing Google Map, Open Street Map etc. but finally you commented these codes out, maybe the reason is that the dismatch projection, maybe not. I’m not sure this is the real reason for issue because I can’t debug code on my end, would you please try below code to set our WorldMapKit as background layer for the map?


In View,



if (Session["DataType"] != null && Convert.ToString(Session["DataType"]) == "Raster")
{
     Html.ThinkGeo().Map("Map1"new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 510)
         .CustomOverlays(overlays =>
          {
             overlays.WorldMapKitWmsWebOverlay("worldMapKit").IsBaseOverlay(true);
            //...
          }
}


Hope it works. If the issue still persist, I’m afraid we need you provide us a complete demo for us to recreate your issue.


Thanks,

Johnny