====== Map Suite WMS Server Edition Quick Start Guide ====== {{section>upgrade_map_suite_to_10.0}}
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WmsServerEdition;
Now let's look at the sample code to bring this concept to fruition. For the purposes of our demo application, we'll use map data covering the entire world in the "Countries02" Shapefile. We've included this sample data in the attached sample zip file; you can find it in the sample project folder at //\HelloWmsPlugin\AppData//.
{{wmsserveredition:QSG_WMSServerEdition_Img04.png}}
===== Implementing a Sample Plugin =====
WmsLayerPlugin is an abstract class in the WmsServerEdition assembly. It is the main class for you to use when you implement your own WMS Server. Let's make our Countries02Plugin class inherited from WmsPlugin by adding the following code:
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WmsServerEdition;
namespace HelloWmsPlugin
{
public class Countries02Plugin: WmsLayerPlugin
{
}
}
Then, right-click on the abstract class name "WmsLayerPlugin" to show the menu, and then click "Implement Interface" to generate the code below automatically. It looks like the image that follows:
{{wmsserveredition:QSG_WMSServerEdition_Img05.png}}
The next step is to implement all of the abstract methods. The majority of our sample code is placed in the ''GetMapConfigurationCore''method. In this sample, we will add two layers: BackgroundLayer and WorldLayer. The following code shows how we accomplish this.
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WmsServerEdition;
namespace HelloWmsPlugin
{
public class Countries02Plugin : WmsLayerPlugin
{
protected override RectangleShape GetBoundingBoxCore(string crs)
{
return new RectangleShape(-126.826171875, 57.104766845702, -70.83984375, 18.960235595702);
}
protected override MapConfiguration GetMapConfigurationCore(string style, string crs)
{
// NOTE: Please make sure you are using the absolute file path name, also you can use the relative file path name,
// but please remeber the path is relative to the built .dll of the plugin
string worldLayerFilePath = @"C:\**\Web\HelloWmsPlugin\HelloWmsPlugin\AppData\Countries02.shp";
BackgroundLayer backgroundLayer = new BackgroundLayer(new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean));
ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(worldLayerFilePath);
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
MapConfiguration mapConfiguration = new MapConfiguration();
mapConfiguration.Layers.Add("BackgroundLayer", backgroundLayer);
mapConfiguration.Layers.Add("WorldLayer", worldLayer);
return mapConfiguration;
}
protected override string GetNameCore()
{
return "Display A Simple Map";
}
protected override System.Collections.ObjectModel.Collection GetProjectionsCore()
{
return new System.Collections.ObjectModel.Collection { "EPSG:4326" };
}
}
}
Now, press F6 to compile and the "HelloWmsPlugin.dll" will be generated in the project's //\bin\debug// directory. This is your first WmsLayerPlugin which we will use later for a WMS Server.
===== Managing Your Plugins =====
Add the existing WmsPluginService Project where you can download it from
using System;
using System.Collections.ObjectModel;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WmsServerEdition;
namespace HelloWmsPlugin
{
public class WorldMapKitPlugin : WmsLayerPlugin
{
protected override RectangleShape GetBoundingBoxCore(string crs)
{
RectangleShape extent = new RectangleShape(-131.22, 55.05, -54.03, 16.91);
if (crs.Equals("EPSG:900913", StringComparison.OrdinalIgnoreCase))
{
extent = new RectangleShape(-13939426.6371, 6701997.4056, -7812401.86, 2626987.386962);
}
return extent;
}
protected override MapConfiguration GetMapConfigurationCore(string style, string crs)
{
WorldMapKitLayer worldMapKitLayer = new WorldMapKitLayer();
switch (crs)
{
case "EPSG:4326":
break;
case "EPSG:900913":
worldMapKitLayer.Projection = WorldMapKitProjection.SphericalMercator;
break;
}
MapConfiguration mapConfiguration = new MapConfiguration();
mapConfiguration.Layers.Add("worldMapKitLayer", worldMapKitLayer);
return mapConfiguration;
}
protected override GeographyUnit GetGeographyUnitCore(string crs)
{
GeographyUnit geographyUnit = GeographyUnit.DecimalDegree;
switch (crs)
{
case "EPSG:4326":
break;
case "EPSG:900913":
geographyUnit = GeographyUnit.Meter;
break;
}
return geographyUnit;
}
protected override string GetNameCore()
{
return "Map Suite World Map Kit";
}
protected override Collection GetProjectionsCore()
{
Collection projections = new Collection();
projections.Add("EPSG:4326");
projections.Add("EPSG:900913");
return projections;
}
}
}
After adding this class, rebuild the project and upload the new "HelloWmsPlugin.dll" to the //WmsLayerPlugins// folder mentioned in the previous section. Then, refresh the WMS Server management page and you will see two plugin items on the list. Mouse over each item's "Preview" link and you will see a small preview image on the left that gives you a general idea of what the plugin will look like.
{{wmsserveredition:QSG_WMSServerEdition_Img12.png}}
===== Consuming the WMS on a Client App =====
The WMS Server now has been set up correctly. If you would like to consume it on a client application, please refer the code below.
**Example 1:** Using the default projection "EPSG: 4326" with "DEFAULT" style in Map Suite Web Edition.
using System;
using System.Web.UI;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;
namespace WmsWebSmaple
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
Map1.CurrentExtent = new RectangleShape(-131.22, 55.05, -54.03, 16.91);
Map1.MapUnit = GeographyUnit.DecimalDegree;
WmsOverlay wmsOverlay = new WmsOverlay("WMS Overlay");
wmsOverlay.Parameters.Add("LAYERS", "Map Suite World Map Kit");
wmsOverlay.Parameters.Add("STYLES", "DEFAULT");
//Here you add your WMS Server uri
wmsOverlay.ServerUris.Add(new Uri("http://localhost:62626/WmsHandler.axd"));
Map1.CustomOverlays.Add(wmsOverlay);
}
}
}
}
**Example 2:** Using the projection "EPSG: 900913" with "DEFAULT" style in Map Suite Web Edition.
using System;
using System.Web.UI;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;
namespace WmsWebSmaple
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
Map1.CurrentExtent = new RectangleShape(-13939426.6371, 6701997.4056, -7812401.86, 2626987.386962);
Map1.MapUnit = GeographyUnit.Meter;
WmsOverlay wmsOverlay = new WmsOverlay("WMS Overlay");
wmsOverlay.Parameters.Add("LAYERS", "Map Suite World Map Kit");
wmsOverlay.Parameters.Add("STYLES", "DEFAULT");
wmsOverlay.Parameters.Add("SRS", "EPSG:900913");
//Here you add your WMS Server uri
wmsOverlay.ServerUris.Add(new Uri("http://localhost:62626/WmsHandler.axd"));
Map1.CustomOverlays.Add(wmsOverlay);
}
}
}
}
**Example 3:** Using the projection "EPSG: 4326" with "DEFAULT" style in Map Suite Desktop Edition.
using System;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.DesktopEdition;
namespace WMSDesktopEditionSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
winformsMap1.CurrentExtent = new RectangleShape(-126.826171875, 57.104766845702, -70.83984375, 18.960235595702);
TiledWmsLayer tiledWmsLayer = new TiledWmsLayer(new Uri("http://localhost:62626/WmsHandler.axd"));
tiledWmsLayer.ActiveLayerNames.Add("Display A Simple Map");
tiledWmsLayer.ActiveStyleNames.Add("DEFAULT");
LayerOverlay staticOverlay = new LayerOverlay();
staticOverlay.Layers.Add("SimpleWmsLayerPlugin", tiledWmsLayer);
winformsMap1.Overlays.Add(staticOverlay);
winformsMap1.Refresh();
}
}
}
===== Summary =====
You have now explored the basics of using Map Suite WMS Server Edition and are able to start adding this functionality into your own applications. Let's recap what we have learned about the object relationships and how the pieces of Map Suite work together:
- A WMS plugin assembly contains one or more public classes, and each of them should inherit from the WmsLayerPlugin abstract class. A WMS plugin assembly should have a name that ends with "plugin".
- You must implement the ''GetMapConfigurationCore'' method, which is used to define your own layers.
- You must implement the ''GetBoundingBoxCore'' method, which returns the boundingBox of all the layers you defined.
- You need to implement the ''GetNameCore'' method to specify the name of a WMS layer.
- You need to implement the ''GetProjectionsCore'' method, which is used to add the projection string code (such as "EPSG: 4326") that you want to support.
- Each public class inherited from the WmsLayerPlugin abstract class represents one Layer element in the WMS specification.
- All custom WmsPlugin DLLs need to be copied to a specified directory, which can be changed in the "Web.config" file.