%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="KitMap.aspx.cs"
Inherits="Errma.KitMap" %>
<%@ Register Assembly="WebEdition" Namespace="ThinkGeo.MapSuite.WebEdition" TagPrefix="cc1" %>
Crossing Illustrator
using System;
using System.Configuration;
using System.Drawing.Imaging;
using System.Web.UI;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;
using System.Collections.ObjectModel;
using System.Text;
using System.Globalization;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Collections.Specialized;
namespace Errma
{
public partial class KitMap : System.Web.UI.Page
{
private SqlConnection _connection;
private SqlCommand _command;
private SqlDataReader _reader;
public DataTable MapData;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromArgb(255, 242, 239, 233));
Map1.MapUnit = GeographyUnit.DecimalDegree;
string dataPath = ConfigurationManager.AppSettings.Get("WorldMapKitDataFolder");
RenderMap renderMap = new RenderMap(dataPath);
renderMap.LoadLayers(Map1.StaticOverlay.Layers, Map1.DynamicOverlay.Layers);
Map1.CurrentExtent = RenderMap.DefaultExtent;
Map1.StaticOverlay.WebImageFormat = WebImageFormat.Jpeg;
//Map1.StaticOverlay.ServerCache.CacheDirectory = ConfigurationManager.AppSettings.Get("ImageCacheFolder");
//Map1.StaticOverlay.ClientCache.Duration = new TimeSpan(1, 0, 0, 0);
//Map1.StaticOverlay.ClientCache.CacheId = "ThinkGeoClientCache";
Map1.MapTools.LoadingImage.Enabled = true;
Map1.MapTools.KeyboardMapTool.Enabled = true;
Map1.MapTools.MiniMap.Enabled = false;
Map1.MapTools.ScaleLine.Enabled = true;
Map1.MapTools.MouseCoordinate.Enabled = true;
Map1.MapTools.PanZoomBar.Enabled = true;
//RunDemoCode();
DataTable dt = new DataTable();
dt = SetupDataSet();
DrawMap(dt);
Timer1.Enabled = true;
}
}
//////////////////////////////////////////////////
// Production code
// Initial query
// Load points to define extent
// Draw in memory markers
// ReDraw in memory markers on update if changed
/// /////////////////////////////////////////////
private DataTable SetupDataSet()
{
string connect = WebConfigurationManager.ConnectionStrings["AsyncConnection"].ConnectionString;
_connection = new SqlConnection(connect);
_connection.Open();
_command = new SqlCommand("SelectSiteDisplayDataByObserverID", _connection);
_command.Parameters.AddWithValue("@ObserverID", Convert.ToInt32(Session["ObserverID"]));
_reader = _command.ExecuteReader();
DataTable dt = new DataTable("Sites");
//dt = CreateSitesDataTable();
dt = CreateUpdateDataTableSchema();
//load the DataTable
while (_reader.Read())
{
DataRow workRow = dt.NewRow();
workRow["lSiteID"] = _reader["lSiteID"];
workRow["ObserverID"] = Session["Municipality"].ToString();
workRow["lLatitude"] = _reader["lLatitude"];
workRow["lLongitude"] = _reader["lLongitude"];
workRow["szChannelName"] = _reader["szChannelName"];
workRow["szSiteDesc"] = _reader["szSiteDesc"];
workRow["DisplayState"] = _reader["DisplayState"];
workRow["DisplayText"] = _reader["DisplayText"];
dt.Rows.Add(workRow);
MapData = dt;
}
_reader.Close();
// Cache the initialized DataTable per municipality.
string id = Session["Municipality"].ToString();
string name = id + "Sites";
Cache[name] = (object)dt;
return dt;
}
public static DataTable CreateUpdateDataTableSchema()
{
//lSiteID
//ObserverID
//lLatitude
//lLongitude
//szChannelName
//szSiteDesc
//DisplayState
//DisplayText
//Ticks
try
{
// Create a new DataTable.
DataTable table = new DataTable("MapItems");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
// Create new DataColumns, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "lSiteID";
column.ReadOnly = true;
column.Unique = false;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
//Continue...
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ObserverID";
column.ReadOnly = true;
column.Unique = false;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.Decimal");
column.ColumnName = "lLatitude";
column.AutoIncrement = false;
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.Decimal");
column.ColumnName = "lLongitude";
column.AutoIncrement = false;
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "szChannelName";
column.AutoIncrement = false;
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "szSiteDesc";
column.AutoIncrement = false;
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "DisplayState";
column.AutoIncrement = false;
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "DisplayText";
column.AutoIncrement = false;
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Ticks";
column.AutoIncrement = false;
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
DataColumn[] keys = new DataColumn[2];
keys[0] = table.Columns["lSiteID"];
table.PrimaryKey = keys;
return table;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
throw ex;
}
}
private void DrawMap(DataTable dt)
{
//Draw points for extent
InMemoryFeatureLayer shapeLayer = new InMemoryFeatureLayer();
shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle =
PointStyles.CreateSimplePointStyle(PointSymbolType.Circle, GeoColor.StandardColors.Blue, 5);
shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
Map1.DynamicOverlay.Layers.Add("pointShapeLayer", shapeLayer);
//Create the points at lat/lon for BoundingBox
DrawPointsLayer(dt, shapeLayer);
//configure extent
shapeLayer.Open();
Map1.CurrentExtent.ScaleUp(60);
Map1.CurrentExtent = shapeLayer.GetBoundingBox();
Map1.RestrictedExtent = Map1.CurrentExtent;
Map1.CurrentExtent = Map1.CurrentExtent;
shapeLayer.Close();
//Add value markers based on state of rail crossing
//0 = unoccupied
//1 = occupied
//2 = unknown
ValueMarkerStyle currValueStyle = new ValueMarkerStyle("Crossings");
DrawMarkerOverlays(dt, currValueStyle);
//DrawMarkerOverlays(dt);
//apply MarkerLayer
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.CustomMarkerStyle = currValueStyle;
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
//align bottom of marker with point previously set
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.DefaultMarkerStyle.WebImage.ImageOffsetX = -10.5f;
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.DefaultMarkerStyle.WebImage.ImageOffsetY = -25f;
Map1.MarkerOverlay.FeatureSource.Open();
Map1.MarkerOverlay.Columns.Add(new FeatureSourceColumn("Crossings"));
Map1.MarkerOverlay.Columns.Add(new FeatureSourceColumn("szSiteDesc"));
Map1.MarkerOverlay.FeatureSource.Close();
}
private void DrawMapUpdate(DataTable dt)
{
//Remove existing Layer
Map1.DynamicOverlay.Layers.Remove("pointShapeLayer");
//Draw points for extent
InMemoryFeatureLayer shapeLayer = new InMemoryFeatureLayer();
shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle =
PointStyles.CreateSimplePointStyle(PointSymbolType.Circle, GeoColor.StandardColors.Blue, 5);
shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
Map1.DynamicOverlay.Layers.Add("pointShapeLayer", shapeLayer);
//Create the points at lat/lon for BoundingBox
DrawPointsLayer(dt, shapeLayer);
//configure extent
shapeLayer.Open();
Map1.CurrentExtent.ScaleUp(60);
Map1.CurrentExtent = shapeLayer.GetBoundingBox();
Map1.RestrictedExtent = Map1.CurrentExtent;
Map1.CurrentExtent = Map1.CurrentExtent;
shapeLayer.Close();
Map1.DynamicOverlay.Redraw();
//Add value markers based on state of rail crossing
//0 = unoccupied
//1 = occupied
//2 = unknown
Map1.MarkerOverlay.Features.Clear();
ValueMarkerStyle currValueStyle = new ValueMarkerStyle("Crossings");
DrawMarkerOverlays(dt, currValueStyle);
//DrawMarkerOverlays(dt);
//apply MarkerLayer
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.CustomMarkerStyle = currValueStyle;
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
//align bottom of marker with point previously set
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.DefaultMarkerStyle.WebImage.ImageOffsetX = -10.5f;
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.DefaultMarkerStyle.WebImage.ImageOffsetY = -25f;
Map1.MarkerOverlay.FeatureSource.Open();
Map1.MarkerOverlay.Columns.Add(new FeatureSourceColumn("Crossings"));
Map1.MarkerOverlay.Columns.Add(new FeatureSourceColumn("szSiteDesc"));
Map1.MarkerOverlay.FeatureSource.Close();
}
private void DrawPointsLayer(DataTable dt, InMemoryFeatureLayer myShapeLayer)
{
//=======================================================
//Create the points at lat/lon for BoundingBox
DataView dv = dt.DefaultView;
IEnumerator iterator = dv.GetEnumerator();
DataRowView drv;
System.Int32 i = 0;
string lon = string.Empty;
string lat = string.Empty;
while (iterator.MoveNext())
{
drv = (DataRowView)iterator.Current;
lon = drv.Row["lLongitude"].ToString();
lat = drv.Row["lLatitude"].ToString();
if (VerifyLatLon(lat, lon))
{
CreatePoints(myShapeLayer, lon, lat);
}
i++;
}
}
private bool VerifyLatLon(string lat, string lon)
{
if (lon != string.Empty)
{
if (lat != string.Empty)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private void DrawMarkerOverlays(DataTable dt, ValueMarkerStyle myValueStyle)
{
DataView dv = dt.DefaultView;
IEnumerator iterator = dv.GetEnumerator();
DataRowView drv;
System.Int32 i = 0;
string lon = string.Empty;
string lat = string.Empty;
Double dlon = 0.0;
Double dlat = 0.0;
string desc = string.Empty;
string lSiteID = string.Empty;
while (iterator.MoveNext())
{
drv = (DataRowView)iterator.Current;
lon = drv.Row["lLongitude"].ToString();
lat = drv.Row["lLatitude"].ToString();
lSiteID = drv.Row["lSiteID"].ToString();
if (VerifyLatLon(lat, lon))
{
dlon = Convert.ToDouble(drv.Row["lLongitude"]);
dlat = Convert.ToDouble(drv.Row["lLatitude"]);
desc = drv.Row["szSiteDesc"].ToString();
CreateValueMarker((int)drv.Row["DisplayState"],
myValueStyle,
lSiteID,
dlon,
dlat);
AppendFeatures(Map1.MarkerOverlay,
drv.Row["DisplayState"].ToString(),
dlon,
dlat,
desc);
}
i++;
}
}
private void CreateValueMarker(Int32 state, ValueMarkerStyle vmStyle, string key, double lon, double lat)
{
switch (state)
{
case 0:
vmStyle.ValueItems.Add(new MarkerValueItem(state.ToString(),
new PointMarkerStyle(new WebImage("/CrossingsMap/theme/default/img/marker_green.gif", 25, 25, -11.5F, -25F),
CreatePopup(key),
CreateContext(key, lon, lat))));
break;
case 1:
vmStyle.ValueItems.Add(new MarkerValueItem(state.ToString(),
new PointMarkerStyle(new WebImage("/CrossingsMap/theme/default/img/marker.gif", 25, 25, -11.5F, -25F),
CreatePopup(key),
CreateContext(key, lon, lat))));
break;
case 2:
vmStyle.ValueItems.Add(new MarkerValueItem(state.ToString(),
new PointMarkerStyle(new WebImage("/CrossingsMap/theme/default/img/marker_grey.gif", 25, 25, -11.5F, -25F),
CreatePopup(key),
CreateContext(key, lon, lat))));
break;
default:
break;
}
}
private void AppendFeatures(InMemoryMarkerOverlay markerOverlay, string state, double lon, double lat, string hoverText)
{
{
Feature feature = new Feature(lon, lat);
feature.ColumnValues.Add("Crossings", state);
feature.ColumnValues.Add("szSiteDesc", hoverText);
markerOverlay.Features.Add(feature);
}
}
private void UpdateTrackShape()
{
string ticks = string.Empty;
var proxy = new UpdateSvc();
object[] objValues = proxy.GetMyUpdate(Session["Municipality"].ToString(), ticks);
if (objValues != null)
{
//update/redraw the dynamic map layer
//if there is data, there has been a change
//1. update the dataset with datarow
//2. find the point to redraw
//3. redraw the map
//retrieve the cached datatable object per municipality
string id = Session["Municipality"].ToString();
string name = id + "Sites";
DataTable updateTable = (DataTable)Cache[name];
//convert to datarow
DataRow dr = updateTable.NewRow();
dr.ItemArray = objValues;
//update Datatable
string lSiteID = dr["lSiteID"].ToString();
ticks = dr["ticks"].ToString();
DataRow foundRow = updateTable.Rows.Find(lSiteID);
try
{
if (foundRow != null)
{
foreach (DataRow item in updateTable.Rows)
{
//found a row so delete it and add new
if (item["lSiteID"].ToString() == lSiteID)
{
item.Delete();
break;
}
}
updateTable.AcceptChanges();
updateTable.ImportRow(foundRow);
}
else
{
updateTable.ImportRow(foundRow);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
// Cache the updated DataTable per municipality.
Cache[name] = (object)updateTable;
DrawMapUpdate(updateTable);
}
//ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateMarker", "UpdateMarkers()", true);
}
protected void Timer1_Tick(object sender, EventArgs e)
{
UpdateTrackShape();
}
private void CreatePoints(InMemoryFeatureLayer layer, string lon, string lat)
{
//could use the id param to identify
Feature pointFeature = new Feature(new PointShape(
double.Parse(lon, CultureInfo.InvariantCulture), double.Parse(lat, CultureInfo.InvariantCulture)));
layer.InternalFeatures.Add(pointFeature.Id, pointFeature);
}
private ContextMenu CreateContext(string key, double lon, double lat)
{
//Map1.MarkerOverlay.AutoRefreshInterval = timespan;
try
{
ContextMenu menuOnMarker = new ContextMenu(key, 180);
ContextMenuItem redirectItem = new ContextMenuItem("Send Poll?");
ContextMenuItem zoomOutItem = new ContextMenuItem("Zoom in
");
//ContextMenuItem zoomOutItem = new ContextMenuItem("Zoom in
");
ContextMenuItem zoomInItem = new ContextMenuItem("Zoom out
");
//ContextMenuItem centerItem = new ContextMenuItem("Center map here
");
menuOnMarker.MenuItems.Add(redirectItem);
menuOnMarker.MenuItems.Add(zoomOutItem);
menuOnMarker.MenuItems.Add(zoomInItem);
//menuOnMarker.MenuItems.Add(centerItem);
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.DefaultMarkerStyle.ContextMenu = menuOnMarker;
return menuOnMarker;
}
catch
{
return null;
}
}
private CustomPopup CreatePopup(string lSiteID)
{
try
{
CustomPopup popup = new CustomPopup(lSiteID);
StringBuilder contentHtml = new StringBuilder();
contentHtml.Append("");
popup.ContentHtml = contentHtml.ToString();
popup.AutoSize = true;
popup.BorderWidth = 1;
popup.IsVisible = false;
popup.AutoPan = true;
Map1.MarkerOverlay.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
return popup;
}
catch
{
return null;
}
}
//////////////////////////////////////////////////
// End Production code
/// /////////////////////////////////////////////
}
}