using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Da
using EditInfo;
using MapInfo.Mapping;
using MapInfo.Styles;
using MapInfo.Windows.Dialogs;
using MapInfo.Geometry;
using MapInfo.Da
using MapInfo.Engine;
using System.Collections.Generic;
namespace MapControlClass
{
public class MapControlLayer
{
private MapInfo.Windows.Controls.MapControl ControlMap;
public MapControlLayer()
{
ControlMap = MapMainControl.ControlMap;
Microsoft.Win32.RegistryKey key =
Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\TRANSGIS\Mapinfo\DA
string s = (string)key.GetValue("SampleDataSearchPath");
if (s != null && s.Length > 0)
{
if (s.EndsWith("\\") == false)
{
s += "\\";
}
}
else
{
s = Environment.CurrentDirectory;
}
key.Close();
MapInfo.Engine.Session.Current.TableSearchPath.Path = s;
//string geoSetName = "world.gst";
//try
//{
// ControlMap.Map.Load(new MapGeosetLoader(s + geoSetName));
//}
//catch (Exception)
//{
// MessageBox.Show("Geoset " + geoSetName + " not found.");
//}
}
/// <summary>
/// 显示标注图层
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="columnName">列名</param>
public void ShowLabelLayer(string tableName, string columnName)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[ControlMap.Map.Alias];
//新建标注图层
LabelLayer labelLayer = new LabelLayer();
myMap.Layers.Add(labelLayer);
//指定要标注的数据表
LabelSource labelSource = new LabelSource(MapInfo.Engine.Session.Current.Catalog.GetTable(tableName));
labelLayer.Sources.Append(labelSource);
//指定要标准字段所在的列
labelSource.DefaultLabelProperties.Caption = columnName;
//标注样式等属性
labelSource.DefaultLabelProperties.Visibility.Enabled = true;
labelSource.DefaultLabelProperties.Visibility.VisibleRangeEnabled = true;
labelSource.DefaultLabelProperties.Visibility.VisibleRange = new VisibleRange(0.01, 10, MapInfo.Geometry.DistanceUnit.Mile);
labelSource.DefaultLabelProperties.Visibility.AllowDuplicates = true;
labelSource.DefaultLabelProperties.Visibility.AllowOverlap = true;
labelSource.DefaultLabelProperties.Visibility.AllowOutOfView = true;
labelSource.Maximum = 50;
//source.DefaultLabelProperties.Layout.UseRelativeOrientation = true;
//source.DefaultLabelProperties.Layout.RelativeOrientation = MapInfo.Text.RelativeOrientation.FollowPath;
//source.DefaultLabelProperties.Layout.Angle = 33.0;
//source.DefaultLabelProperties.Priority.Major = "index";
//source.DefaultLabelProperties.Layout.Offset = 7;
//source.DefaultLabelProperties.Layout.Alignment = MapInfo.Text.Alignment.BottomRight;
//Font font = new Font("黑体", 10);
//font.ForeColor = System.Drawing.Color.DarkBlue;
//source.DefaultLabelProperties.Style.Font = font;
}
/// <summary>
/// 向图层中添加点
/// Design by Glacier
/// 2008年8月6日
/// <param name="tempLayerTableName">表名</param>
/// <param name="tempLayerName">图层名</param>
/// <param name="dPoint">点坐标</param>
/// </summary>
public void AddPointToLayer(string tempLayerTableName, string tempLayerName, DPoint dPoint)
{
EditAppoint appoint = new EditAppoint();
if (appoint.ShowDialog() == DialogResult.OK)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[ControlMap.Map.Alias];
//获取图层和表
FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[tempLayerName];
MapInfo.Da
//创建点图元及其样式
FeatureGeometry pgPoint = new MapInfo.Geometry.Point(workLayer.CoordSys, dPoint);
MapInfo.Styles.SimpleVectorPointStyle spsPoint = new MapInfo.Styles.SimpleVectorPointStyle(37, System.Drawing.Color.Red, 20);
MapInfo.Styles.CompositeStyle csPoint = new MapInfo.Styles.CompositeStyle(spsPoint);
MapInfo.Da
ptPoint.Geometry = pgPoint;
ptPoint.Style = csPoint;
ptPoint["index"] = appoint.ID;
ptPoint["name"] = appoint.Name;
//将点图元加入图层
workLayer.Table.InsertFeature(ptPoint);
}
}
/// <summary>
/// 向图层中添加线段
/// Design by Glacier
/// 2008年8月6日
/// <param name="tempLayerTableName">表名</param>
/// <param name="tempLayerName">图层名</param>
/// <param name="startPoint">线段起点坐标</param>
/// <param name="endPoint">线段终点坐标</param>
/// </summary>
public void AddLineToLayer(string tempLayerTableName, string tempLayerName, DPoint startPoint, DPoint endPoint)
{
EditAppoint appoint = new EditAppoint();
if (appoint.ShowDialog() == DialogResult.OK)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[ControlMap.Map.Alias];
//获取图层和表
FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[tempLayerName];
MapInfo.Da
//创建线图元及其样式
FeatureGeometry pgLine = MultiCurve.CreateLine(workLayer.CoordSys, startPoint, endPoint);
MapInfo.Styles.SimpleLineStyle slsLine = new MapInfo.Styles.SimpleLineStyle(new LineWidth(3, LineWidthUnit.Pixel), 2, System.Drawing.Color.OrangeRed);
MapInfo.Styles.CompositeStyle csLine = new MapInfo.Styles.CompositeStyle(slsLine);
MapInfo.Da
ptLine.Geometry = pgLine;
ptLine.Style = csLine;
ptLine["index"] = appoint.ID;
ptLine["name"] = appoint.Name;
//将线图元加入图层
workLayer.Table.InsertFeature(ptLine);
}
}
/// <summary>
/// 向图层中添加多个点
/// 2008年8月6日
/// <param name="tempLayerTableName">表名</param>
/// <param name="tempLayerName">图层名</param>
/// <param name="dPoint">点坐标数组</param>
/// </summary>
public void AddMultiPoint(string tempLayerTableName, string tempLayerName, DPoint[] dPoints)//MultiPoint 包含了无序和不连续的 Point 集且可用于在多个点上执行多个操作。其中 pointArray 是 DPoints 的数组。
{
CreateTempLayer(tempLayerName, tempLayerName);
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[ControlMap.Map.Alias];
//获取图层和表
FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[tempLayerName];
MapInfo.Da
//创建点图元及其样式
//MultiPoint multiPointGeometry = new MultiPoint(longLatNad83, pointArray);
FeatureGeometry pgPoint = new MapInfo.Geometry.MultiPoint(workLayer.CoordSys, dPoints);
// FeatureGeometry pgPoint = new MapInfo.Geometry.Point(workLayer.CoordSys, dPoint);
MapInfo.Styles.SimpleVectorPointStyle spsPoint = new MapInfo.Styles.SimpleVectorPointStyle(37, System.Drawing.Color.Red, 20);
MapInfo.Styles.CompositeStyle csPoint = new MapInfo.Styles.CompositeStyle(spsPoint);
MapInfo.Da
ptPoint.Geometry = pgPoint;
ptPoint.Style = csPoint;
ptPoint["index"] = "1";
ptPoint["name"] = "gg";
//将点图元加入图层
workLayer.Table.InsertFeature(ptPoint);
}
/// <summary>
/// LineString 是以线性方式连接的连续点的有方向集合。LineString 中任意两个连续点都由直线连接
/// </summary>
/// <param name="tempLayerTableName">临时表名</param>
/// <param name="tempLayerName">临时图层名</param>
/// <param name="dPoints">点数组</param>
public void AddLineString(string tempLayerTableName, string tempLayerName, DPoint[] dPoints)
{
//折线
CreateTempLayer(tempLayerTableName, tempLayerName);
Catalog Cat = MapInfo.Engine.Session.Current.Catalog;
Table tblTemp = Cat.GetTable(tempLayerTableName);
MapInfo.Geometry.DPoint[] point = new DPoint[3];
tblTemp.BeginAccess(TableAccessMode.Write);
Feature ftr = new Feature(tblTemp.TableInfo.Columns);
CoordSys coordSys = this.ControlMap.Map.GetDisplayCoordSys();
point[0] = new DPoint(Convert.ToDouble("103"), Convert.ToDouble("36"));
point[1] = new DPoint(Convert.ToDouble("104"), Convert.ToDouble("43"));
point[2] = new DPoint(107.22751833, 33.127);
MapInfo.Geometry.CurveSegment seg = new MapInfo.Geometry.LineString(coordSys, dPoints);
FeatureGeometry geometry = new MapInfo.Geometry.MultiCurve(coordSys, seg.CurveSegmentType, dPoints);
ftr.Geometry = geometry;
//ftr["LineId"] = flag;
tblTemp.InsertFeature(ftr);
tblTemp.EndAccess();
}
/// <summary>
/// 添加矩形
/// </summary>
/// <param name="tempLayerTableName">临时表名</param>
/// <param name="tempLayerName">临时图层名</param>
/// <param name="dPoints">点数组</param>
public void AddRectangle(string tempLayerTableName, string tempLayerName, DPoint[] dPoints)
{
CreateTempLayer(tempLayerTableName, tempLayerName);
Catalog Cat = MapInfo.Engine.Session.Current.Catalog;
Table tblTemp = Cat.GetTable(tempLayerTableName);
tblTemp.BeginAccess(TableAccessMode.Write);
Feature ftr = new Feature(tblTemp.TableInfo.Columns);
CoordSys coordSys = this.ControlMap.Map.GetDisplayCoordSys();
MapInfo.Geometry.CurveSegment seg = new MapInfo.Geometry.LineString(coordSys, dPoints);
MapInfo.Geometry.MultiPolygon mulp = new MultiPolygon(coordSys, seg.CurveSegmentType, dPoints);
ftr.Geometry = mulp;
//ftr["LineId"] = flag;
tblTemp.InsertFeature(ftr);
tblTemp.EndAccess();
}
/// <summary>
/// 添加圆角矩形
/// </summary>
/// <param name="tempLayerTableName">临时表名</param>
/// <param name="tempLayerName">临时图层名</param>
/// <param name="x1">坐标</param>
/// <param name="y1">坐标</param>
/// <param name="x2">坐标</param>
/// <param name="y2">坐标</param>
/// <param name="radius">转交半径20</param>
public void AddRoundedRectangle(string tempLayerTableName, string tempLayerName, double x1,double y1,double x2,double y2,double radius)
{
CreateTempLayer(tempLayerTableName, tempLayerName);
Catalog Cat = MapInfo.Engine.Session.Current.Catalog;
Table tblTemp = Cat.GetTable(tempLayerTableName);
tblTemp.BeginAccess(TableAccessMode.Write);
Feature ftr = new Feature(tblTemp.TableInfo.Columns);
CoordSys coordSys = this.ControlMap.Map.GetDisplayCoordSys();
//创建线图元及其样式
DRect rect = new DRect(x1, y1, x2, y2);
DistanceUnit unit = DistanceUnit.Mile;
DistanceType type = DistanceType.Spherical;
RoundedRectangle roundedRectangle = new RoundedRectangle(coordSys, rect, radius, unit, type);
ftr.Geometry = roundedRectangle;
//ftr["LineId"] = flag;
tblTemp.InsertFeature(ftr);
tblTemp.EndAccess();
}
/// <summary>
/// 添加椭圆形
/// </summary>
/// <param name="tempLayerTableName">临时表名</param>
/// <param name="tempLayerName">临时图层名</param>
/// <param name="x1">中心点</param>
/// <param name="y1">中心点</param>
/// <param name="xRadius">x轴半径</param>
/// <param name="yRadius">Y轴半径</param>
public void AddEllipse(string tempLayerTableName, string tempLayerName, double x1, double y1, double xRadius, double yRadius)
{
CreateTempLayer(tempLayerTableName, tempLayerName);
Catalog Cat = MapInfo.Engine.Session.Current.Catalog;
Table tblTemp = Cat.GetTable(tempLayerTableName);
tblTemp.BeginAccess(TableAccessMode.Write);
Feature ftr = new Feature(tblTemp.TableInfo.Columns);
CoordSys coordSys = this.ControlMap.Map.GetDisplayCoordSys();
//创建线图元及其样式
DPoint center = new DPoint(x1, y1);
//double xRadius = 100;
//double yRadius = 135;
DistanceUnit unit = DistanceUnit.Mile;
Ellipse ellipse = new Ellipse(coordSys, center, xRadius, yRadius, unit, DistanceType.Spherical);
ftr.Geometry = ellipse;
//ftr["LineId"] = flag;
tblTemp.InsertFeature(ftr);
tblTemp.EndAccess();
}
/// <summary>
/// 添加扇形图元
/// </summary>
/// <param name="x">中心点的坐标</param>
/// <param name="y">中心点的坐标</param>
/// <param name="middle">中线</param>
/// <param name="angle">角度</param>
/// <param name="radius">radius是半径</param>
/// <returns>返回构造好的扇形</returns>
public MapInfo.Geometry.Geometry DrawSector(double x, double y, int middle, int angle, double radius)
{
if ((angle == 0) || (radius == 0))
return null;
MapInfo.Geometry.DPoint center = new MapInfo.Geometry.DPoint(x, y);
//创建一段弧线
double beginAngle, endAngle;
if (middle < 90)
{
beginAngle = 90 - middle - angle / 2;
endAngle = 90 - middle + angle / 2;
}
else if (middle == 90)
{
beginAngle = 360 - angle / 2;
endAngle = angle / 2;
}
else if (middle == 360)
{
beginAngle = 0;
endAngle = 360;
}
else
{
beginAngle = 450 - middle - angle / 2;
endAngle = 450 - middle + angle / 2;
}
CoordSys coordSys = this.ControlMap.Map.GetDisplayCoordSys();
MapInfo.Geometry.LegacyArc sector = new LegacyArc(coordSys, center, radius, radius,
MapInfo.Geometry.DistanceUnit.Kilometer, MapInfo.Geometry.DistanceType.Spherical, beginAngle, endAngle);
MultiCurve mc = sector.CreateMultiCurve(100);
MapInfo.Geometry.DPoint[] tmpPoints = mc[0][0].SamplePoints();
MapInfo.Geometry.DPoint[] dPoints = new MapInfo.Geometry.DPoint[3];
dPoints[0] = mc[0].EndPoint;
dPoints[1] = new MapInfo.Geometry.DPoint(x, y);
dPoints[2] = mc[0].StartPoint;
MapInfo.Geometry.DPoint[] mPoints = new MapInfo.Geometry.DPoint[tmpPoints.Length - 1 + 3];
for (int i = 0, j = 0; i < tmpPoints.Length - 1 + 3; i++)
{
if (i < tmpPoints.Length - 1)
mPoints[i] = tmpPoints[i];
else
mPoints[i] = dPoints[j++];
}
MapInfo.Geometry.MultiPolygon mp = new MultiPolygon(coordSys, CurveSegmentType.Linear, mPoints);
mp.EditingComplete();
CreateTempLayer("Sector", "Sector");
Catalog Cat = MapInfo.Engine.Session.Current.Catalog;
Table tblTemp = Cat.GetTable("Sector");
tblTemp.BeginAccess(TableAccessMode.Write);
Feature ftr = new Feature(tblTemp.TableInfo.Columns);
ftr.Geometry = mp;
//ftr["LineId"] = flag;
tblTemp.InsertFeature(ftr);
tblTemp.EndAccess();
return mp;
}
/// <summary>
/// 重点应用,获取图元的所有图层名称
/// </summary>
/// <returns>返回List类型的字符列表</returns>
public List<string> GetDefaultSelectLayerName()//获取当前选中的图元的所有图层名称
{
MapLayerEnumerator enumer = MapMainControl.ControlMap.Map.Layers.GetMapLayerEnumerator();//得到所有图层枚举
List<string> ls = new List<string>();
while (enumer.MoveNext())
{
if (MapMainControl.ControlMap.Map.Layers[enumer.Current.Alias].GetType().ToString() == "MapInfo.Mapping.FeatureLayer")//判断是否为图元图层
{
ls.Add(enumer.Current.Name);
}
}
return ls;
}
/// <summary>
/// 创建动态轨迹图层
/// kongliang
/// 2008年8月7日
/// <param name="trackLayerTableName">图层表名</param>
/// <param name="trackLayerName">图层名</param>
/// <param name="firstPoint">点初始坐标</param>
/// </summary>
protected void CreateTrackLayer(string trackLayerTableName, string trackLayerName, DPoint firstPoint)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[ControlMap.Map.Alias];
//创建临时图层
MapInfo.Da
MapInfo.Da
if (tblTemp != null)
{
MapInfo.Engine.Session.Current.Catalog.CloseTable(trackLayerTableName);
}
tblInfoTemp.Columns.Add(MapInfo.Da
tblInfoTemp.Columns.Add(MapInfo.Da
tblTemp = MapInfo.Engine.Session.Current.Catalog.CreateTable(tblInfoTemp);
string AnimationLayerName = "test";
FeatureLayer workLayer = new FeatureLayer(tblTemp, AnimationLayerName, AnimationLayerName);
myMap.Layers.Add(workLayer);
//向临时图层中添加初始点
FeatureGeometry pfg = new MapInfo.Geometry.Point(workLayer.CoordSys, firstPoint.x, firstPoint.y) as FeatureGeometry;
MapInfo.Styles.CompositeStyle cstyle = new MapInfo.Styles.CompositeStyle(new MapInfo.Styles.SimpleVectorPointStyle(52, System.Drawing.Color.Blue, 30));
MapInfo.Da
pft.Geometry = pfg;
pft.Style = cstyle;
workLayer.Table.InsertFeature(pft);
}
/// <summary>
/// 把删除原有点并向图层中添加新点以实现动态轨迹
/// Glacier
/// 2008年8月7日
/// <param name="trackLayerTableName">图层表名</param>
/// <param name="newPoint">点的新坐标</param>
/// </summary>
protected void UpdateTrack(string trackLayerTableName, DPoint newPoint)
{
MapInfo.Da
if (altb == null)
{
return;
}
//Delete the existed feature and create a new on
SearchInfo si = MapInfo.Da
Feature dftr = MapInfo.Engine.Session.Current.Catalog.SearchForFeature(altb, si);
if (dftr == null)
{
return;
}
altb.DeleteFeature(dftr);
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[ControlMap.Map.Alias];
FeatureGeometry pfg = new MapInfo.Geometry.Point(myMap.GetDisplayCoordSys(), new DPoint(newPoint.x, newPoint.y)) as FeatureGeometry;
MapInfo.Styles.CompositeStyle cstyle = new MapInfo.Styles.CompositeStyle(new MapInfo.Styles.SimpleVectorPointStyle(52, System.Drawing.Color.Blue, 30));
MapInfo.Da
pft.Geometry = pfg;
pft.Style = cstyle;
altb.InsertFeature(pft);
}
public void CreateTempLayer(string tempLayerTableName, string tempLayerName)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[ControlMap.Map.Alias];
//指定表名建立表信息
MapInfo.Da
//确保当前目录下不存在同名表
MapInfo.Da
if (tblTemp != null)
{
MapInfo.Engine.Session.Current.Catalog.CloseTable(tempLayerTableName);
}
//向表信息中添加可绘图列
tblInfoTemp.Columns.Add(MapInfo.Da
tblInfoTemp.Columns.Add(MapInfo.Da
//向表信息中添加自定义列
tblInfoTemp.Columns.Add(MapInfo.Da
tblInfoTemp.Columns.Add(MapInfo.Da
//根据表信息创建临时表
tblTemp = MapInfo.Engine.Session.Current.Catalog.CreateTable(tblInfoTemp);
//指定表,图层名和图层别名创建临时图层
FeatureLayer tempLayer = new FeatureLayer(tblTemp, tempLayerName, tempLayerName);
myMap.Layers.Add(tempLayer);
}
public DPoint[] GetFeatureRing(string TableName, string Express)
{
MapInfo.Da
MapInfo.Geometry.DPoint[] d=null;
table = MapInfo.Engine.Session.Current.Catalog.GetTable("面地区界");
if (table != null)
{
MapInfo.Da
MapInfo.Da
Feature ftr = irfc[0];
MapInfo.Geometry.MultiPolygon mPoly = (MapInfo.Geometry.MultiPolygon)ftr.Geometry;
MapInfo.Geometry.Polygon poly = (MapInfo.Geometry.Polygon)mPoly[0];
MapInfo.Geometry.Ring r = poly.Exterior;
d = r.SamplePoints(22116, DistanceUnit.Kilometer, DistanceType.Spherical);
}
return d;
}
}
}