📄 mapcomm.cs
字号:
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using MapInfo.Mapping;
using MapInfo.Data;
using MapInfo.WebControls;
using MapInfo.Mapping.Thematics;
using MapInfo.Mapping.Legends;
/// <summary>
/// mapComm 的摘要说明
/// </summary>
public class mapComm
{
public mapComm()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
[Serializable]
public class Info : MapInfo.WebControls.MapBaseCommand
{
/// <summary>
/// Key to be used to get the pixel tolerance parameter value from the URL.
/// </summary>
protected const string PixelToleranceKey = "PixelTolerance";
protected const string InfoCommand = "Info";
/// <summary>
/// Constructor for Info class
/// </summary>
public Info()
{
Name = InfoCommand;
}
/// <summary>
/// Override the Execute method in MapBasicCommand class to not save state, because
/// for info tool, which does not change map state, so there is no need to save map state.
/// </summary>
public override void Execute()
{
StateManager sm = StateManager.GetStateManagerFromSession();
if (sm == null)
{
if (StateManager.IsManualState())
{
throw new NullReferenceException("Cannot find instance of StateManager in the ASP.NET session.");
}
}
ParseContext();
if (sm != null)
{
PrepareStateManagerParamsDictionary(sm);
sm.RestoreState();
}
Process();
}
/// <summary>
/// method to do the real server side process for info tool.
/// </summary>
public override void Process()
{
//get pixel tolerance from url of client side.
int pixelTolerance = System.Convert.ToInt32(HttpContext.Current.Request[PixelToleranceKey]);
MapControlModel model = MapControlModel.GetModelFromSession();
model.SetMapSize(MapAlias, MapWidth, MapHeight);
MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
//extract points from url of client side.
System.Drawing.Point[] points = ExtractPoints(DataString);
MapInfo.Geometry.DPoint pt;
map.DisplayTransform.FromDisplay(points[0], out pt);
MapInfo.Data.SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchNearest(pt,map.GetDisplayCoordSys(), new MapInfo.Geometry.Distance(200, MapInfo.Geometry.DistanceUnit.Kilometer));
si.QueryDefinition.Columns = null;
MapInfo.Data.Table t = MapInfo.Engine.Session.Current.Catalog.GetTable("world");
if (t != null)
{
MapInfo.Data.IResultSetFeatureCollection irfc = MapInfo.Engine.Session.Current.Catalog.Search(t, si);
HttpContext.Current.Response.Output.Write(irfc[0]["Country"].ToString());
}
else
{
HttpContext.Current.Response.Output.Write("当前区域不位于陆地!");
}
}
}
[Serializable]
public class getImage : MapInfo.WebControls.MapBaseCommand
{
/// <summary>
/// Constructor for ZoomValue class
/// </summary>
public getImage()
{
Name = "getImage";
}
/// <summary>
/// Override the Execute method in MapBasicCommand class to NOT save state, because
/// for this command, which does not change map state, so there is no need to save map state.
/// </summary>
public override void Execute()
{
StateManager sm = StateManager.GetStateManagerFromSession();
if (sm == null)
{
if (StateManager.IsManualState())
{
throw new NullReferenceException("Cannot find instance of StateManager in the ASP.NET session.");
}
}
ParseContext();
if (sm != null)
{
PrepareStateManagerParamsDictionary(sm);
sm.RestoreState();
}
Process();
}
public override void Process()
{
MapControlModel model = MapControlModel.GetModelFromSession();
//get map object from map model
MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
MapInfo.Data.Table t = MapInfo.Engine.Session.Current.Catalog.GetTable("world");
MapInfo.Geometry.DPoint pt = new MapInfo.Geometry.DPoint(8600000, 3900000);//asia
MapInfo.Geometry.DPoint pt1 = new MapInfo.Geometry.DPoint(2000000, 800000);//14300//Africa
MapInfo.Geometry.DPoint pt2 = new MapInfo.Geometry.DPoint(370000, 5920000);//7320//Europe
MapInfo.Geometry.DPoint pt3 = new MapInfo.Geometry.DPoint(-8200000, 5000000);//11300//North America
MapInfo.Geometry.DPoint pt4 = new MapInfo.Geometry.DPoint(-5600000, -1800000);//12600//South America
MapInfo.Geometry.DPoint pt5 = new MapInfo.Geometry.DPoint(13100000, -3000000);//6510//Australia
MapInfo.Data.SearchInfo si = null;
MapInfo.Data.IResultSetFeatureCollection irfc = null;
string whereCause = "";
string continent = HttpContext.Current.Request.QueryString["continent"].ToString();
switch (continent)
{
case "亚洲":
whereCause ="continent = 'Asia'";
map.Center = pt;
break;
case "非洲":
whereCause = "continent = 'Africa'";
map.Center = pt1;
break;
case "欧洲":
whereCause = "continent = 'Europe'";
map.Center = pt2;
break;
case "北美":
whereCause = "continent = 'North America'";
map.Center = pt3;
break;
case "南美":
whereCause = "continent = 'South America'";
map.Center = pt4;
break;
case "大洋":
whereCause = "continent = 'Australia'";
map.Center = pt5;
break;
}
if (t != null)
{
IMapLayer im = map.Layers["Continent Layer"];
if (im != null)
{
map.Layers.Remove(im);
}
si = MapInfo.Data.SearchInfoFactory.SearchWhere(whereCause);
irfc = MapInfo.Engine.Session.Current.Catalog.Search(t, si);
MapInfo.Mapping.FeatureLayer fl = new FeatureLayer(irfc.Table,"Continent Layer");
map.Layers.Add(fl);
map.SetView(fl);
map.Layers["world"].Enabled = false;
}
MemoryStream ms = model.GetMap(MapAlias, MapWidth, MapHeight, ExportFormat);
StreamImageToClient(ms);
MapInfo.WebControls.StateManager.GetStateManagerFromSession().SaveState();
}
}
[Serializable]
public class getThemeImage : MapInfo.WebControls.MapBaseCommand
{
/// <summary>
/// Constructor for ZoomValue class
/// </summary>
public getThemeImage()
{
Name = "getThemeImage";
}
/// <summary>
/// Override the Execute method in MapBasicCommand class to NOT save state, because
/// for this command, which does not change map state, so there is no need to save map state.
/// </summary>
public override void Execute()
{
StateManager sm = StateManager.GetStateManagerFromSession();
if (sm == null)
{
if (StateManager.IsManualState())
{
throw new NullReferenceException("Cannot find instance of StateManager in the ASP.NET session.");
}
}
ParseContext();
if (sm != null)
{
PrepareStateManagerParamsDictionary(sm);
sm.RestoreState();
}
Process();
}
public override void Process()
{
MapControlModel model = MapControlModel.GetModelFromSession();
//get map object from map model
MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
MapInfo.Mapping.FeatureLayer fl = map.Layers["world"] as FeatureLayer;
string item1 = HttpContext.Current.Request.QueryString["item1"].ToString();
switch (item1)
{
case "人口":
getTheme(fl,themeType.RangedTheme, "Pop_1994","人口");
break;
case "GDP":
getTheme(fl, themeType.DotDensityTheme, "Pop_1994","GDP");
break;
}
MemoryStream ms = model.GetMap(MapAlias, MapWidth, MapHeight, ExportFormat);
StreamImageToClient(ms);
MapInfo.WebControls.StateManager.GetStateManagerFromSession().SaveState();
}
private enum themeType
{
RangedTheme,
BarTheme,
DotDensityTheme
}
private void getTheme(FeatureLayer lyr, themeType ty, string column, string legendTitle)
{
lyr.Modifiers.Clear();
lyr.Map.Legends.Clear();
FeatureStyleModifier theme = null;
switch (ty)
{
case themeType.BarTheme:
break;
case themeType.DotDensityTheme:
theme = new DotDensityTheme(lyr, column, "", System.Drawing.Color.Red, DotDensitySize.Large);
break;
case themeType.RangedTheme:
theme = new RangedTheme(lyr, column, "PopDensity", 5, DistributionMethod.EqualCountPerRange);
break;
}
lyr.Modifiers.Append(theme);
LegendFactory lgdFactory = lyr.Map.Legends;
Legend lgd = lgdFactory.CreateLegend("legend1","legend2",new System.Drawing.Size(200, 280));
ThemeLegendFrame lgFrame = LegendFrameFactory.CreateThemeLegendFrame(theme as MapInfo.Mapping.Thematics.ITheme);
lgd.Frames.Append(lgFrame);
//modify legend frame style
lgFrame.BackgroundBrush = new SolidBrush(Color.AliceBlue);
lgFrame.Title = "世界 " +legendTitle+ "统计";
lgFrame.SubTitle = " ";
MapInfo.Styles.Font titleFont = new MapInfo.Styles.Font("Arial", 10);
titleFont.ForeColor = Color.DarkBlue;
titleFont.FontWeight = MapInfo.Styles.FontWeight.Bold;
lgFrame.TitleStyle = titleFont;
MapInfo.Styles.Font rowTextStyle = new MapInfo.Styles.Font("Arial", 8);
rowTextStyle.FontWeight = MapInfo.Styles.FontWeight.Bold;
lgFrame.RowTextStyle = rowTextStyle;
LegendExport legendExp = new LegendExport(lyr.Map, lgd);
legendExp.Format = MapInfo.Mapping.ExportFormat.Jpeg;
legendExp.Export(HttpContext.Current.Server.MapPath(".")+"\\images\\legendImage.jpg");
legendExp.Dispose();
}
}
[Serializable]
public class GetLegend : MapInfo.WebControls.MapBaseCommand
{
/// <summary>
/// Constructor for CreateTheme class
/// </summary>
public GetLegend()
{
Name = "GetLegend";
}
/// <summary>
/// Override the Execute method in MapBasicCommand class to not save state, because
/// for legend control, which does not change map state, so there is no need to save map state.
/// </summary>
public override void Execute()
{
StateManager sm = StateManager.GetStateManagerFromSession();
if (sm == null)
{
if(StateManager.IsManualState())
{
throw new NullReferenceException("Cannot find instance of StateManager in the ASP.NET session.");
}
}
ParseContext();
if(sm != null)
{
PrepareStateManagerParamsDictionary(sm);
sm.RestoreState();
}
Process();
}
public override void Process()
{
//MapControlModel model = MapControlModel.GetModelFromSession();
//MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
//if(map.Legends.Count == 0)
// return;
//Legend legend = map.Legends[0];
//LegendExport legendExp = new LegendExport(map, legend);
//legendExp.Format = MapInfo.Mapping.ExportFormat.Jpeg;
////export Legend to memorystream
//MemoryStream stream = new MemoryStream();
//legendExp.Export(stream);
//stream.Position = 0;
//legendExp.Dispose();
////stream legend image back to client
//StreamImageToClient(stream);
System.Drawing.Bitmap bp = new Bitmap(HttpContext.Current.Server.MapPath(".")+"\\images\\legendImage.jpg");
bp.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
/// <summary>
/// Stream legend image in memory stream back to client.
/// </summary>
/// <param name="ms">memory stream holding legend image</param>
public override void StreamImageToClient(MemoryStream ms)
{
if (ms != null)
{
BinaryReader reader = new BinaryReader(ms);
int length = (int)ms.Length;
HttpContext.Current.Response.ContentType = "image/Jpeg";
HttpContext.Current.Response.OutputStream.Write(reader.ReadBytes(length), 0, length);
reader.Close();
ms.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -