====== Source Code WebAPIEditionSample projection.zip ======
====Global.asax.cs====
using System.Web.Http;
namespace Projection
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
}
}
====WebApiConfig.cs====
using System.Web.Http;
namespace Projection
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//Enable RouteAttribute
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//Enable RouteAttribute, if delete this line, will throw other exceptions
config.EnsureInitialized();
}
}
}
====ProjectionController.cs====
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
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 Projection.Controllers
{
[RoutePrefix("Projection")]
public class ProjectionController : ApiController
{
private static readonly string baseDirectory;
private static readonly ManagedProj4Projection decimalDegreeToMeter;
private static readonly ManagedProj4Projection decimalDegreeToEpsg2163;
private static readonly LayerOverlay customProjectionOverlay;
private static readonly LayerOverlay rotaionProjectionOverlay;
static ProjectionController()
{
baseDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data");
// Initialize projection from decimal degree to meter.
decimalDegreeToMeter = new ManagedProj4Projection();
decimalDegreeToMeter.InternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString();
decimalDegreeToMeter.ExternalProjectionParametersString = Proj4Projection.GetSphericalMercatorParametersString();
decimalDegreeToMeter.Open();
// Initialize projection from decimal degree to EPSG2163.
decimalDegreeToEpsg2163 = new ManagedProj4Projection();
decimalDegreeToEpsg2163.InternalProjectionParametersString = Proj4Projection.GetDecimalDegreesParametersString();
decimalDegreeToEpsg2163.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(2163);
decimalDegreeToEpsg2163.Open();
// Initialize custom and rotation projection overlay.
customProjectionOverlay = InitializeCustomProjectionOverlay();
rotaionProjectionOverlay = InitializeRotaionProjectionOverlay();
}
///
/// Loads countries layer with different GeographyUnit.
///
[Route("LoadCountriesLayer/{geographyUnit}/{z}/{x}/{y}")]
[HttpGet]
public HttpResponseMessage LoadCountriesLayer(int z, int x, int y, string geographyUnit)
{
string countriesFilePath = string.Format(@"{0}/Countries02.shp", baseDirectory);
ShapeFileFeatureLayer countriesLayer = new ShapeFileFeatureLayer(countriesFilePath);
countriesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen = new GeoPen(GeoColor.SimpleColors.Green, 2);
countriesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay layerOverlay = new LayerOverlay();
layerOverlay.Layers.Add(countriesLayer);
// Change projection by map unit.
GeographyUnit mapUnit = GeographyUnit.DecimalDegree;
if (geographyUnit == "Mercator")
{
countriesLayer.FeatureSource.Projection = decimalDegreeToMeter;
mapUnit = GeographyUnit.Meter;
}
return DrawTileImage(layerOverlay, mapUnit, z, x, y);
}
///
/// Loads countries layer with rotation projection.
///
[Route("LoadRotationLayers/{angle}/{coordinateX}/{coordinateY}/{z}/{x}/{y}")]
[HttpGet]
public HttpResponseMessage LoadRotationLayers(int z, int x, int y, double angle, double coordinateX, double coordinateY)
{
// Creates rotation projection.
RotationProjection projection = new RotationProjection(angle);
projection.PivotCenter = new PointShape(coordinateX, coordinateY);
projection.Open();
UpdateRotationProjection(projection);
return DrawTileImage(rotaionProjectionOverlay, GeographyUnit.Meter, z, x, y);
}
///
/// Gets projection information.
///
[Route("InformationDataRequest/{epsgId}")]
[HttpGet]
public Dictionary InformationDataRequest(int epsgId)
{
Dictionary respond = new Dictionary();
// Get projection's Unit and Proj4String.
string epsgParameters = ManagedProj4Projection.GetEpsgParametersString(epsgId);
string unit = ManagedProj4Projection.GetGeographyUnitFromProj4(epsgParameters).ToString();
respond.Add("Unit", unit);
respond.Add("Proj4String", epsgParameters);
return respond;
}
///
/// Loads countries layer with custom projection EPSG2163.
///
[Route("LoadCustomProjectionLayer/{z}/{x}/{y}")]
[HttpGet]
public HttpResponseMessage LoadCustomProjectionLayer(int z, int x, int y)
{
return DrawTileImage(customProjectionOverlay, GeographyUnit.Meter, z, x, y);
}
///
/// Initialize custom projection overlay.
///
///
private static LayerOverlay InitializeCustomProjectionOverlay()
{
LayerOverlay layerOverlay = new LayerOverlay();
// Add background lyer.
BackgroundLayer backgroundLayer = new BackgroundLayer(WorldMapKitAreaStyles.Water().FillSolidBrush);
layerOverlay.Layers.Add(backgroundLayer);
// Add custom projection layer.
string countriesFilePath = string.Format(@"{0}/Countries02.shp", baseDirectory);
ShapeFileFeatureLayer countriesLayer = new ShapeFileFeatureLayer(countriesFilePath);
countriesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = WorldMapKitAreaStyles.BaseLand();
countriesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen = new GeoPen(GeoColor.SimpleColors.Green, 2);
countriesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
countriesLayer.FeatureSource.Projection = decimalDegreeToEpsg2163;
layerOverlay.Layers.Add(countriesLayer);
return layerOverlay;
}
///
/// Initialize ratation projection overlay.
///
///
private static LayerOverlay InitializeRotaionProjectionOverlay()
{
LayerOverlay layerOverlay = new LayerOverlay();
// Add background lyer.
BackgroundLayer backgroundLayer = new BackgroundLayer(WorldMapKitAreaStyles.Water().FillSolidBrush);
layerOverlay.Layers.Add(backgroundLayer);
// Add countries layer.
string countriesFilePath = string.Format(@"{0}/Countries.shp", baseDirectory);
ShapeFileFeatureLayer countriesLayer = new ShapeFileFeatureLayer(countriesFilePath);
countriesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = WorldMapKitAreaStyles.BaseLand();
countriesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen = new GeoPen(GeoColor.SimpleColors.Green, 2);
countriesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
layerOverlay.Layers.Add(countriesLayer);
// Add lake layer.
ShapeFileFeatureLayer lakeLayer = new ShapeFileFeatureLayer(Path.Combine(baseDirectory, "Lake.shp"));
lakeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = WorldMapKitAreaStyles.Water();
lakeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
layerOverlay.Layers.Add(lakeLayer);
// Add highway layer.
ShapeFileFeatureLayer highwayNetworkShapeLayer = new ShapeFileFeatureLayer(Path.Combine(baseDirectory, "USHighwayNetwork.shp"));
highwayNetworkShapeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = WorldMapKitLineStyles.Highway(1);
highwayNetworkShapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
layerOverlay.Layers.Add(highwayNetworkShapeLayer);
// Add major cities layer
ShapeFileFeatureLayer majorCitiesLayer = new ShapeFileFeatureLayer(Path.Combine(baseDirectory, "USMajorCities.shp"));
majorCitiesLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = WorldMapKitTextStyles.Poi("AREANAME", 7, 7);
majorCitiesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
layerOverlay.Layers.Add(majorCitiesLayer);
return layerOverlay;
}
///
/// Update the layer's projection by rotation projection.
///
///
private void UpdateRotationProjection(RotationProjection projection)
{
foreach (var layer in rotaionProjectionOverlay.Layers.OfType())
{
layer.FeatureSource.Projection = projection;
}
}
///
/// Draws the map and return the image back to client in an HttpResponseMessage.
///
private HttpResponseMessage DrawTileImage(LayerOverlay layerOverlay, GeographyUnit geographyUnit, 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);
geoCanvas.BeginDrawing(bitmap, boundingBox, geographyUnit);
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;
}
}
}
}
====AssemblyInfo.cs====
using System.Reflection;
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("Projection")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Projection")]
[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("af94b25b-1796-4810-bfd2-57d88009213f")]
// 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 Projection
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
}
}
====WebApiConfig.cs====
using System.Web.Http;
namespace Projection
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnsureInitialized();
}
}
}
====AssemblyInfo.cs====
using System.Reflection;
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("Projection")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Projection")]
[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("528dcbdc-8985-4904-8949-2d811dc0202b")]
// 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")]