Table of Contents

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
 
[assembly: OwinStartup(typeof(Adornments.Startup))]
 
namespace Adornments
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

Startup.Auth.cs

using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OAuth;
using Owin;
using System;
 
namespace Adornments
{
    public partial class Startup
    {
        public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
 
        public static string PublicClientId { get; private set; }
 
        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
 
            PublicClientId = "self";
            OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/Token"),
                AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
                AllowInsecureHttp = true
            };
 
            app.UseOAuthBearerTokens(OAuthOptions);
        }
    }
}

WebApiConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;
 
namespace Adornments
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
 
            // Web API routes
            config.MapHttpAttributeRoutes();
 
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

AdornmentsController.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebApiEdition;
 
namespace Adornments
{
    [RoutePrefix("Adornments")]
    public class AdornmentsController : ApiController
    {
        private static readonly string baseDirectory;
 
        static AdornmentsController()
        {
            baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
        }
 
        [Route("{adornmentType}/{size}/{extent}")]
        [HttpGet]
        public HttpResponseMessage RefreshSourceLayer(string adornmentType, Size size, string extent)
        {
            AdornmentsType currentAdornmentType = (AdornmentsType)Enum.Parse(typeof(AdornmentsType), adornmentType);
            string[] extentStrings = extent.Split(',');
 
            RectangleShape currentExtent = new RectangleShape(Convert.ToDouble(extentStrings[0]), Convert.ToDouble(extentStrings[3]), Convert.ToDouble(extentStrings[2]), Convert.ToDouble(extentStrings[1]));
            LayerOverlay layerOverlay = new LayerOverlay();
            layerOverlay.Layers.Add(GetAdornmentLayer(currentAdornmentType));
 
            return DrawAdornmentImage(layerOverlay, size.Width, size.Height, currentExtent);
        }
 
        [Route("SchoolShapeFileLayer/{z}/{x}/{y}")]
        [HttpGet]
        public HttpResponseMessage SchoolShapeFileLayer(int z, int x, int y)
        {
            LayerOverlay layerOverlay = new LayerOverlay();
            ManagedProj4Projection wgs84ToGoogleProjection = new ManagedProj4Projection();
            wgs84ToGoogleProjection.InternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString(); //4326
            wgs84ToGoogleProjection.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString(); //900913
            wgs84ToGoogleProjection.Open();
 
            string shpFilePathName = string.Format(@"{0}\App_Data\ShapeFile\Schools.shp", baseDirectory);
            string schoolImage = string.Format(@"{0}\Images\school.png", baseDirectory);
            ShapeFileFeatureLayer schoolsLayer = new ShapeFileFeatureLayer(shpFilePathName);
            schoolsLayer.Name = "schoolLayer";
            schoolsLayer.Transparency = 200f;
            schoolsLayer.ZoomLevelSet.ZoomLevel10.DefaultPointStyle = new PointStyle(new GeoImage(schoolImage));
            schoolsLayer.ZoomLevelSet.ZoomLevel10.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
            schoolsLayer.FeatureSource.Projection = wgs84ToGoogleProjection;
            layerOverlay.Layers.Add(schoolsLayer);
 
            return DrawTileImage(layerOverlay, z, x, y);
        }
 
        private HttpResponseMessage DrawAdornmentImage(LayerOverlay layerOverlay, int width, int height, RectangleShape currentExtent)
        {
            using (Bitmap bitmap = new Bitmap(width, height))
            {
                GdiPlusGeoCanvas geoCanvas = new GdiPlusGeoCanvas();
                geoCanvas.BeginDrawing(bitmap, currentExtent, GeographyUnit.Meter);
                layerOverlay.Draw(geoCanvas);
                geoCanvas.EndDrawing();
 
                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Png);
 
                HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.OK);
                msg.Content = new ByteArrayContent(ms.ToArray());
                msg.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
 
                return msg;
            }
        }
 
        private HttpResponseMessage DrawTileImage(LayerOverlay layerOverlay, int z, int x, int y)
        {
            using (Bitmap bitmap = new Bitmap(256, 256))
            {
                GdiPlusGeoCanvas geoCanvas = new GdiPlusGeoCanvas();
                RectangleShape boundingBox = WebApiExtentHelper.GetBoundingBoxForXyz(x, y, z, GeographyUnit.Meter);
                geoCanvas.BeginDrawing(bitmap, boundingBox, GeographyUnit.Meter);
                layerOverlay.Draw(geoCanvas);
                geoCanvas.EndDrawing();
 
                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Png);
 
                HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.OK);
                msg.Content = new ByteArrayContent(ms.ToArray());
                msg.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
 
                return msg;
            }
        }
 
        private AdornmentLayer GetAdornmentLayer(AdornmentsType adornmentType)
        {
            AdornmentLayer adornmentLayer;
            switch (adornmentType)
            {
                case AdornmentsType.ScaleBarAdornment:
                    adornmentLayer = new ScaleBarAdornmentLayer();
                    break;
                case AdornmentsType.ScaleLineAdornment:
                    adornmentLayer = new ScaleLineAdornmentLayer();
                    break;
                case AdornmentsType.ScaleTextAdornment:
                    adornmentLayer = new ScaleTextAdornmentLayer();
                    break;
                case AdornmentsType.LogoAdornment:
                    adornmentLayer = BuildLogoAdornmentLayer();
                    break;
                case AdornmentsType.GraticuleAdornment:
                    adornmentLayer = BuildGraticuleAdornmentLayer();
                    break;
                case AdornmentsType.LegendAdornment:
                    adornmentLayer = BuildLegendAdornmentLayer();
                    break;
                default:
                    adornmentLayer = null;
                    break;
            }
 
            return adornmentLayer;
        }
 
        private LegendAdornmentLayer BuildLegendAdornmentLayer()
        {
            LegendItem title = new LegendItem();
            title.TextStyle = new TextStyle("Map Legend", new GeoFont("Arial", 10, DrawingFontStyles.Bold), new GeoSolidBrush(GeoColor.SimpleColors.Black));
 
            // Create a legend item for the state borders.  This example uses a modified LineStyle.  
            LegendItem legendItem1 = new LegendItem();
            LineStyle stateBorderStyle = new LineStyle();
            stateBorderStyle.OuterPen.DashStyle = LineDashStyle.Solid;
            stateBorderStyle.OuterPen.Width = 2;
            stateBorderStyle.OuterPen.Color = GeoColor.FromArgb(255, 156, 155, 154);
            legendItem1.ImageStyle = stateBorderStyle;
            legendItem1.TextStyle = new TextStyle("Railroad ", new GeoFont("Arial", 8), new GeoSolidBrush(GeoColor.SimpleColors.Black));
 
            // Create a legend item for the state borders.  This example uses a simple AreaStyle.  
            LegendItem legendItem2 = new LegendItem();
            legendItem2.ImageStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(255, 167, 204, 149));
            legendItem2.TextStyle = new TextStyle("Forests", new GeoFont("Arial", 8), new GeoSolidBrush(GeoColor.SimpleColors.Black));
 
            // Create A legend item for the airports.  This example uses a .png file.  
            LegendItem legendItem3 = new LegendItem();
            string path = string.Format(@"{0}\Images\school.png", baseDirectory);
            legendItem3.ImageStyle = new PointStyle(new GeoImage(path));
            legendItem3.TextStyle = new TextStyle("School", new GeoFont("Arial", 8), new GeoSolidBrush(GeoColor.SimpleColors.Black));
 
            // Create the LegendAdornmentLayer and add the LegendItems.  
            LegendAdornmentLayer legendLayer = new LegendAdornmentLayer();
            legendLayer.BackgroundMask = AreaStyles.CreateLinearGradientStyle(new GeoColor(255, 255, 255, 255), new GeoColor(255, 230, 230, 230), 90, GeoColor.SimpleColors.Black);
            legendLayer.LegendItems.Add(legendItem1);
            legendLayer.LegendItems.Add(legendItem2);
            legendLayer.LegendItems.Add(legendItem3);
            legendLayer.Height = 125;
            legendLayer.Title = title;
            legendLayer.Location = AdornmentLocation.LowerLeft;
 
            return legendLayer;
        }
 
        private GraticuleAdornmentLayer BuildGraticuleAdornmentLayer()
        {
            GraticuleAdornmentLayer graticuleAdornmentLayer = new GraticuleAdornmentLayer();
            ManagedProj4Projection proj4 = new ManagedProj4Projection();
            proj4.InternalProjectionParametersString = ManagedProj4Projection.GetEpsgParametersString(4326);
            proj4.ExternalProjectionParametersString = ManagedProj4Projection.GetGoogleMapParametersString();
            proj4.Open();
            graticuleAdornmentLayer.Projection = proj4;
            LineStyle graticuleLineStyle = new LineStyle(new GeoPen(GeoColor.FromArgb(150, GeoColor.StandardColors.Navy), 1));
            graticuleAdornmentLayer.GraticuleLineStyle = graticuleLineStyle;
            graticuleAdornmentLayer.GraticuleTextFont = new GeoFont("Times", 12, DrawingFontStyles.Bold);
            return graticuleAdornmentLayer;
        }
 
        private LogoAdornmentLayer BuildLogoAdornmentLayer()
        {
            LogoAdornmentLayer logoAdornmentLayer = new LogoAdornmentLayer();
            string path = string.Format(@"{0}\Images\ThinkGeoLogo.png", baseDirectory);
            logoAdornmentLayer.Location = AdornmentLocation.UpperRight;
            logoAdornmentLayer.Image = new GeoImage(path);
 
            return logoAdornmentLayer;
        }
    }
}

AdornmentsType.cs

namespace Adornments
{
    public enum AdornmentsType
    {
        LogoAdornment = 1,
        ScaleBarAdornment = 2,
        ScaleLineAdornment = 3,
        ScaleTextAdornment = 4,
        GraticuleAdornment = 5,
        LegendAdornment = 6,
    }
}

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
 
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Adornments")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Adornments")]
[assembly: AssemblyCopyright("Copyright ©  2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
 
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
 
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("245a06fc-0975-4bd9-84cf-d09f0b7d682a")]
 
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Global.asax.cs

using System.Web.Http;
using System.Web.Mvc;
 
namespace Adornments_Openlayers
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }
}

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
 
[assembly: OwinStartup(typeof(Adornments_Openlayers.Startup))]
 
namespace Adornments_Openlayers
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

Startup.Auth.cs

using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OAuth;
using Owin;
using System;
 
namespace Adornments_Openlayers
{
    public partial class Startup
    {
        public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
 
        public static string PublicClientId { get; private set; }
 
        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
 
            PublicClientId = "self";
            OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/Token"),
                AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
                AllowInsecureHttp = true
            };
            app.UseOAuthBearerTokens(OAuthOptions);
        }
    }
}

WebApiConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;
 
namespace Adornments_Openlayers
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
 
            // Web API routes
            config.MapHttpAttributeRoutes();
 
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
 
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Adornments_Openlayers")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Adornments_Openlayers")]
[assembly: AssemblyCopyright("Copyright ©  2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
 
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
 
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3eb60d45-da23-4a42-83d2-7624659e67f7")]
 
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]