class DotspatialHelper
{
//FunctionMode.Pan //拖动
//FunctionMode.Select //选择
//FunctionMode.Info //信息
//FunctionMode.None //关闭全部工具
//map1.GeoMouseMove += map1_GeoMouseMove;//这个因为需要取鼠标位置,所以不使用MouseMove
//map1.MouseClick += map1_GeoMouseClick;
//map1.MouseDown += map1_MouseDown;
 
 
/// <summary>
/// 初始化map
/// </summary>
public static void InitMap(Map map1, string mapFile)
{
map1 = new Map()
{
Name = "map1"
};
 
//map1.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;
//map1.GeoMouseMove += mapCtrl_GeoMouseMove;
//map1.MouseClick += mapCtrl_GeoMouseClick;
//map1.MouseDown += mapCtrl_MouseDown;
}
 
/// <summary>
/// 添加layer
/// </summary>
public static void AddLayer(Map map1, string mapFile)
{
map1.AddLayer(mapFile);
}
/// <summary>
/// 添加栅格数据,设置投影
/// </summary>
public static void AddRasterLayer(Map map1, string mapFile, ProjectionInfo projection)
{
 
IRaster iRaster = DataManager.DefaultDataManager.OpenRaster(mapFile);
//设置投影坐标系
iRaster.Projection = projection;
map1.Layers.Add(iRaster);
}
 
/// <summary>
/// 添加bmp,tiff等数据,最好是tif,设置投影
/// </summary>
public static void AddTifLayer(Map map1, string mapFile, ProjectionInfo projection)
{
 
IDataSet iDataSet = DataManager.DefaultDataManager.OpenFile(mapFile);
//设置投影坐标系
iDataSet.Projection = projection;
map1.Layers.Add(iDataSet);
}
 
/// <summary>
/// 添加layer,shp格式,设置投影
/// </summary>
public static void AddLayer(Map map1, string mapFile, ProjectionInfo projectionInfo)
{
var shp = Shapefile.OpenFile(mapFile);
shp.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;
var layer = map1.Layers.Add(shp) as MapLineLayer;
layer.Symbolizer = new DotSpatial.Symbology.LineSymbolizer(Color.FromArgb(0x33, 0x33, 0x33), 1);
}
/// <summary>
/// 清空layer
/// </summary>
public static void ClearLayer(Map map1)
{
map1.ClearLayers();
}
 
/// <summary>
/// 全幅
/// </summary>
public static void Zoom2MaxExtent(Map map1)
{
map1.ZoomToMaxExtent();
map1.Refresh();
}
 
/// <summary>
/// 输入坐标定位
/// </summary>
public static void Zoom2Coordinates(Map map1)
{
ZoomToCoordinatesDialog xxx = new ZoomToCoordinatesDialog(map1);
xxx.ShowDialog();
}
 
/// <summary>
/// 创建点图层
/// </summary>
public static void CreateNewPointLayer(Map map1, double x, double y)
{
FeatureSet fs = new FeatureSet(FeatureType.Point);
DotSpatial.Topology.Point point0 = new DotSpatial.Topology.Point(x, y);
fs.AddFeature(point0);
MapPointLayer layer = new MapPointLayer(fs)
{
LegendText = "点"
};
map1.Layers.Add(layer);
}
 
/// <summary>
/// 创建线
/// </summary>
public static void CreateNewLineLayer(Map map1)
{
FeatureSet fs = new FeatureSet(FeatureType.Line);
List<Coordinate> coords = new List<Coordinate>()
{
new Coordinate(0,0),
new Coordinate(10,10),
new Coordinate(20,15)
};
LineString line = new LineString(coords);
fs.AddFeature(line);
MapLineLayer layer = new MapLineLayer(fs)
{
LegendText = "线"
};
map1.Layers.Add(layer);
}
 
/// <summary>
/// 创建面
/// </summary>
public static void CreateNewPolygonLayer(Map map1)
{
FeatureSet fs = new FeatureSet(FeatureType.Polygon);
List<Coordinate> coords = new List<Coordinate>()
{
new Coordinate(25,25),
new Coordinate(35,25),
new Coordinate(35,35),
new Coordinate(25,35)
};
Polygon polygon = new Polygon(coords);
fs.AddFeature(polygon);
MapPolygonLayer layer = new MapPolygonLayer(fs)
{
LegendText = "面"
};
map1.Layers.Add(layer);
}
 
/// <summary>
/// 基于“SelectByAttribute”方法在Map上显示特定的状态,即选中
/// </summary>
/// 参数filterExpression 表达式
public static string FilterByStateName(Map map1, int index, string filterExpression)
{
//Check the number of layers from MapControl
if (map1.Layers.Count > 0)
{
IFeatureLayer stateLayer = default(IFeatureLayer);
stateLayer = (IFeatureLayer)map1.Layers[index];
if (stateLayer == null)
{
return ("The layer is not a polygon layer.");
}
else
{
stateLayer.SelectByAttribute(filterExpression);
}
}
else
{
return ("Please add a layer to the map.");
}
 
return string.Empty;
}
 
/// <summary>
/// 展示属性文本
/// </summary>
/// 参数expression 字段
public static string DisplayStateName(Map map1, int index, string expression, Font font, Color color)
{
//Check the number of layers from MapControl
if (map1.Layers.Count > 0)
{
IFeatureLayer stateLayer = default(IFeatureLayer);
 
//TypeCast the first layer from MapControl to MapPolygonLayer.
//Layers are 0 based, therefore 0 is going to grab the first layer from the MapControl
stateLayer = (IFeatureLayer)map1.Layers[index];
 
//Check whether stateLayer is polygon layer or not
if (stateLayer == null)
{
return ("The layer is not a polygon layer.");
}
else
{
//add StateName as labels on the stateLayer
stateLayer.AddLabels(expression, font, color);
}
}
else
{
return ("Please add a layer to the map.");
}
 
return string.Empty;
}
 
/// <summary>
/// 为属性表中的多个属性实现筛选操作。在本例中,将过滤两个属性,结果将在映射上突出显示.
/// </summary>
/// 参数filter 过滤条件表达式,如"[BOU2_4M_] > 10 OR [NAME] = '河南省'"
public static string FilterByPopState(Map map1, int index, PolygonCategory category, string filter, string legendText)
{
if (map1.Layers.Count > 0)
{
//Delacre a MapPolygonLayer
IFeatureLayer stateLayer = default(IFeatureLayer);
stateLayer = (IFeatureLayer)map1.Layers[index];
if (stateLayer == null)
{
return ("The layer is not a polygon layer.");
 
}
else
{
stateLayer.DataSet.FillAttributes();
//Create a new PolygonScheme
PolygonScheme scheme = new PolygonScheme();
 
//Create a new PolygonCategory
//PolygonCategory category = new PolygonCategory(Color.Yellow, Color.Red, 1);
 
category.FilterExpression = filter;
 
//Set the Legend Text
category.LegendText = legendText;
 
//Add the PolygonCategory in to the PolygonScheme
scheme.AddCategory(category);
 
//Set the scheme in to the MapPolygonLayer's symbology
stateLayer.Symbology = scheme;
 
}
}
else
{
return ("Please add a layer to the map.");
}
return string.Empty;
}
 
/// <summary>
/// 基于属性表列的唯一值设置随机颜色.
/// </summary>
/// 参数filter 过滤条件表达式,如"[BOU2_4M_] > 10 OR [NAME] = '河南省'"
public static string RandomColors(Map map1, int index, string fieldName)
{
if (map1.Layers.Count > 0)
{
//Delacre a MapPolygonLayer
IFeatureLayer stateLayer = default(IFeatureLayer);
 
//Type cast the FirstLayer of MapControl to MapPolygonLayer
stateLayer = (IFeatureLayer)map1.Layers[index];
 
//Check the MapPolygonLayer ( Make sure that it has a polygon layer)
if (stateLayer == null)
{
return ("The layer is not a polygon layer.");
}
else
{
//Create a new PolygonScheme
PolygonScheme scheme = new PolygonScheme();
 
//Set the ClassificationType for the PolygonScheme via EditorSettings
scheme.EditorSettings.ClassificationType = ClassificationType.UniqueValues;
//Set the UniqueValue field name
//Here STATE_NAME would be the Unique value field
scheme.EditorSettings.FieldName = fieldName;
 
//create categories on the scheme based on the attributes table and field name
//In this case field name is STATE_NAME
scheme.CreateCategories(stateLayer.DataSet.DataTable);
 
//Set the scheme to stateLayer's symbology
stateLayer.Symbology = scheme;
}
}
else
{
return ("Please add a layer to the map.");
}
return string.Empty;
}
 
/// <summary>
/// 根据条件获取元素对象.
/// </summary>
/// /// 参数filter 过滤条件表达式,如"[NAME] = '河南省'"
public static List<IFeature> GetFeature(Map map1, int index, string filter)
{
IFeatureLayer stateLayer = default(IFeatureLayer);
stateLayer = (IFeatureLayer)map1.Layers[index];
return stateLayer.DataSet.SelectByAttribute(filter);
 
}
/// <summary>
/// 设置图层的选择高亮显示效果
/// </summary>
/// <param name="tempLayer">Color.Red, Color.DarkRed</param>
public static void SetHighLightColor(FeatureLayer tempLayer, Color fillColor, Color borderColor, double width = 2)
{
try
{
switch (tempLayer.FeatureSet.FeatureType)
{
case FeatureType.Line:
 
tempLayer.SelectionSymbolizer = new LineSymbolizer(fillColor, borderColor, width, System.Drawing.Drawing2D.DashStyle.Solid
, System.Drawing.Drawing2D.LineCap.Round);
 
break;
 
case FeatureType.MultiPoint:
case FeatureType.Point:
 
tempLayer.SelectionSymbolizer = new PointSymbolizer(fillColor, DotSpatial.Symbology.PointShape.Star, width);
 
break;
 
case FeatureType.Polygon:
 
tempLayer.SelectionSymbolizer = new PolygonSymbolizer(fillColor, borderColor, width);
 
 
break;
default:
break;
}
}
catch (Exception ex)
{
 
throw ex;
}
}
 
/// <summary>
/// 遍历要素,并进行删除
/// </summary>
public static void RemoveShape(Map map1)
{
//添加图层后,定义图层,并获取图层
 
//遍历要素,并进行删除
FeatureSet fs = null;
fs = (FeatureSet)map1.Layers[0].DataSet;
//要素删除后,index值会变化,所有从大往小处理
for (int i = fs.Features.Count - 1; i >= 0; i--)
{
fs.RemoveShapeAt(i);
}
fs.Save();
map1.Refresh();
}
 
/// <summary>
/// 设置类别,包括颜色等
/// </summary>
public static void SetCategory(Map map1, int index, string filter, IFeatureCategory category)
{
IFeatureLayer stateLayer = default(IFeatureLayer);
stateLayer = (IFeatureLayer)map1.Layers[index]; ;
IFeature ifeature = stateLayer.DataSet.SelectByAttribute(filter)[0];
//PolygonCategory category = new PolygonCategory(Color.Yellow, Color.Red, 5);
stateLayer.SetCategory(ifeature, category);
map1.ResetBuffer();
}
/// <summary>
/// 改变类别,包括颜色等
/// </summary>
/// //FeatureCategoryControl fc = new FeatureCategoryControl(stateLayer);//IFeatureScheme featureScheme = stateLayer.Symbology; //featureScheme.RemoveCategory();
public static void SetCategoryBylayer(ILayer iLayer, ILayer iLayerRefer, FeatureSet featureSet, string filter,string name, IFeatureCategory category)
{
IFeatureLayer stateLayer = default(IFeatureLayer);
stateLayer = (IFeatureLayer)iLayer; ;
IFeature currentFeature;
if (stateLayer.DataSet.SelectByAttribute(filter).Count <= 0)
{
IFeatureLayer stateLayerRefer = default(IFeatureLayer);
stateLayerRefer = (IFeatureLayer)iLayerRefer; ;
IFeature ifeature = stateLayerRefer.DataSet.SelectByAttribute(filter)[0];
currentFeature = featureSet.AddFeature(ifeature);
currentFeature.DataRow["NAME"] = name;
}
else
{
currentFeature = stateLayer.DataSet.SelectByAttribute(filter)[0];
}
IFeatureScheme featureScheme = stateLayer.Symbology;
featureScheme.RemoveCategory(stateLayer.GetCategory(currentFeature));
stateLayer.SetCategory(currentFeature, category);
 
 
 
}
 
/// <summary>
///更改特性方案
/// </summary>
public static void ApplyScheme(IFeatureLayer stateLayer, IFeatureScheme featureScheme)
{
stateLayer.ApplyScheme(featureScheme);
}
 
/// <summary>
///保存形状文件.
/// </summary>
public static void SaveShapeFile(FeatureSet featureSet, string fielName, bool overwrite)
{
featureSet.SaveAs(fielName, overwrite);
}
 
 
/// <summary>
/// 添加空白图层
/// </summary>
public static void AddEmptyLayer(Map map1, string columnName, string legendText)
{
FeatureSet featureSet = new FeatureSet(FeatureType.Polygon);
featureSet.Projection = map1.Projection;
DataColumn column = new DataColumn(columnName);
featureSet.DataTable.Columns.Add(column);
IMapFeatureLayer layer = (IMapFeatureLayer)map1.Layers.Add(featureSet);
layer.LegendText = legendText;
}
/// <summary>
/// 添加空白图层
/// </summary>
public static void AddEmptyLayer(Map map1, FeatureSet featureSet, string dataname, string columnName, string legendText)
{
 
featureSet.Projection = map1.Projection;
DataColumn column = new DataColumn(columnName);
featureSet.DataTable.Columns.Add(column);
IMapFeatureLayer layer = (IMapFeatureLayer)map1.Layers.Add(featureSet);
layer.LegendText = legendText;
layer.DataSet.Name = dataname;
 
}
 
/// <summary>
/// 添加带样式的图层
/// </summary>
public static void AddSymbolizerLayer(Map map1, FeatureSet featureSet, string dataname, string columnName, string legendText, IFeatureSymbolizer symbol)
{
 
featureSet.Projection = map1.Projection;
DataColumn column = new DataColumn(columnName);
featureSet.DataTable.Columns.Add(column);
IMapFeatureLayer layer = (IMapFeatureLayer)map1.Layers.Add(featureSet);
layer.LegendText = legendText;
layer.DataSet.Name = dataname;
 
//PointSymbolizer symbol = new PointSymbolizer(Color.Red, DotSpatial.Symbology.PointShape.Ellipse, 3);
//Set the symbolizer to the point layer
layer.Symbolizer = symbol;
}
/// <summary>
/// 添加空白文本图层
/// </summary>
public static void AddEmptyLabelLayer(Map map1, FeatureSet featureSet, string dataname, string columnName1, string columnName2, string legendText)
{
 
featureSet.Projection = map1.Projection;
DataColumn column1 = new DataColumn(columnName1);
featureSet.DataTable.Columns.Add(column1);
DataColumn column2 = new DataColumn(columnName2);
featureSet.DataTable.Columns.Add(column2);
IMapFeatureLayer layer = (IMapFeatureLayer)map1.Layers.Add(featureSet);
layer.LegendText = legendText;
layer.DataSet.Name = dataname;
}
/// <summary>
/// 添加带样式的图层
/// </summary>
public static void AddColorLayer(Map map1, string dataname, string columnName, string legendText)
{
FeatureSet featureSet = new FeatureSet(FeatureType.Point);
featureSet.Projection = map1.Projection;
DataColumn column = new DataColumn(columnName);
featureSet.DataTable.Columns.Add(column);
IMapFeatureLayer layer = (IMapFeatureLayer)map1.Layers.Add(featureSet);
layer.LegendText = legendText;
layer.DataSet.Name = dataname;
 
PointSymbolizer symbol = new PointSymbolizer(Color.Red, DotSpatial.Symbology.PointShape.Ellipse, 3);
//Set the symbolizer to the point layer
layer.Symbolizer = symbol;
}
/// <summary>
/// 图层添加对象
/// </summary>
public static void AddEmptyLayer(Map map1, int index, string columnName, string legendText)
{
FeatureSet featureSet = new FeatureSet(FeatureType.Polygon);
 
IFeatureLayer stateLayer = default(IFeatureLayer);
stateLayer = (IFeatureLayer)map1.Layers[index];
 
IFeature ifeature = stateLayer.DataSet.SelectByAttribute("[NAME] = '黑龙江省'")[0];
IFeature currentFeature = featureSet.AddFeature(ifeature);
 
//currentFeature.DataRow.BeginEdit();
currentFeature.DataRow["NAME"] = "黑龙江省";
//currentFeature.DataRow.EndEdit();
 
map1.ResetBuffer();
}
/// <summary>
/// 图层删除对象
/// </summary>
public static void RemoveFeature(Map map1, int index, string columnName, string legendText)
{
FeatureSet featureSet = new FeatureSet(FeatureType.Polygon);
IFeatureLayer stateLayer = default(IFeatureLayer);
stateLayer = (IFeatureLayer)map1.Layers[index]; ;
IFeature ifeature = stateLayer.DataSet.SelectByAttribute("[NAME] = '黑龙江省'")[0];
featureSet.Features.Remove(ifeature);
map1.ResetBuffer();
}
/// <summary>
/// 添加点图层
/// </summary>
public static void AddPointLayer(Map map1, System.Drawing.Point pointL)
{
FeatureSet featureSet = new FeatureSet(FeatureType.Point);
Coordinate coord = map1.PixelToProj(pointL);
 
//Create a new point
//Input parameter is clicked point coordinate
DotSpatial.Topology.Point point = new DotSpatial.Topology.Point(coord);
 
//Add the point into the Point Feature
//assigning the point feature to IFeature because via it only we can set the attributes.
 
IFeature currentFeature = featureSet.AddFeature(point);
 
}
/// <summary>
/// 增加字段
/// </summary>
public static void AddColumn()
{
 
//面状要素
FeatureSet fs = new FeatureSet(FeatureType.Polygon);
//增加字段
fs.DataTable.Columns.Add(new DataColumn("ID", typeof(int)));
fs.DataTable.Columns.Add(new DataColumn("Text", typeof(string)));
}
/// <summary>
/// 根据dataset的name寻找layer,name需要唯一
/// </summary>
public static ILayer FindLayerByName(Map map1, string name)
{
//FeatureLayer fealayer = (FeatureLayer)mapCtrl.Layers[0];
//DotspatialHelper.SetHighLightColor(fealayer, Color.Red, Color.DarkRed, 2);
 
ILayer ilayer1 = map1.GetAllLayers().Find(delegate(ILayer ilayer) { return ilayer.DataSet.Name == name; });
return ilayer1;
 
}
 
 
/// <summary>
/// 在一个图层layer上选择与元素相交的其他元素(也可以是包含等关系)
/// </summary>
public static IEnvelope SelectFeatures(PolygonLayer pLayer, Feature feature)
{
IEnvelope pEnvelope = null;
pLayer.Select(null, feature.Envelope, DotSpatial.Symbology.SelectionMode.Intersects, out pEnvelope);
return pEnvelope;
}
 
/// <summary>
/// 元素选择器,放在mapCtrl.Layers.SelectionChanged事件里面,不过这个事件会执行两次,所以代码中判断了count
/// </summary>
public static object SelectFeature(Map map1, int index, string name)
{
PolygonLayer pLayer = map1.Layers[index] as PolygonLayer;
FeatureSet fs = null;
fs = (FeatureSet)map1.Layers[index].DataSet;
if (pLayer.Selection.Count == 0)
{
//MessageBox.Show("无选中记录", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
 
foreach (Feature feature in pLayer.Selection.ToFeatureList())
{
//IEnvelope pEnvelope = null;
//pLayer.Select(null, feature.Envelope, DotSpatial.Symbology.SelectionMode.Intersects, out pEnvelope);
 
//PolygonCategory category = new PolygonCategory(Color.Yellow, Color.Red, 5);
//pLayer.SetCategory(feature, category);//报错
return feature.DataRow[name];
}
return null;
}
 
/// <summary>
/// 在地图上添加坐标点
/// </summary>
public static void AddPointToMap(Map map1, string columnName, string legendText)
{
FeatureSet featureSet = new FeatureSet(FeatureType.Point);
featureSet.Projection = map1.Projection;
DataColumn column = new DataColumn(columnName);
featureSet.DataTable.Columns.Add(column);
IMapFeatureLayer layer = (IMapFeatureLayer)map1.Layers.Add(featureSet);
layer.LegendText = legendText;
 
PointSymbolizer symbol = new PointSymbolizer(Color.Red, DotSpatial.Symbology.PointShape.Ellipse, 3);
//Set the symbolizer to the point layer
layer.Symbolizer = symbol;
 
 
 
//Create a new point
//Input parameter is clicked point coordinate
DotSpatial.Topology.Point point = new DotSpatial.Topology.Point(113.692753, 34.737820);
 
//Add the point into the Point Feature
//assigning the point feature to IFeature because via it only we can set the attributes.
 
IFeature currentFeature = featureSet.AddFeature(point);
 
 
map1.ResetBuffer();
}
 
/// <summary>
/// 选出与点相交的面
/// </summary>
public static void SelectPolygonByPoint(Map map1, string columnName, string legendText)
{
FeatureSet featureSet = new FeatureSet(FeatureType.Point);
featureSet.Projection = map1.Projection;
DataColumn column = new DataColumn("Name");
featureSet.DataTable.Columns.Add(column);
IMapFeatureLayer layer = (IMapFeatureLayer)map1.Layers.Add(featureSet);
layer.LegendText = "tt";
 
PointSymbolizer symbol = new PointSymbolizer(Color.Red, DotSpatial.Symbology.PointShape.Ellipse, 3);
//Set the symbolizer to the point layer
layer.Symbolizer = symbol;
 
 
 
//Create a new point
//Input parameter is clicked point coordinate
DotSpatial.Topology.Point point = new DotSpatial.Topology.Point(113.692753, 34.737820);
 
//Add the point into the Point Feature
//assigning the point feature to IFeature because via it only we can set the attributes.
 
IFeature currentFeature = featureSet.AddFeature(point);
 
 
IEnvelope pEnvelope = null;
map1.Layers[0].Select(null, currentFeature.Envelope, DotSpatial.Symbology.SelectionMode.Intersects, out pEnvelope);
 
map1.ResetBuffer();
}
 
/// <summary>
/// 打开地图打印窗口
/// </summary>
public static void OpenLayoutForm(Map map1)
{
DotSpatial.Controls.LayoutForm frm = new DotSpatial.Controls.LayoutForm();
frm.MapControl = map1;
frm.Show();
}
/// <summary>
/// 打开图层属性窗口
/// </summary>
public static void OpenLayerForm(IFeatureLayer layer)
{
FeatureLayerActions XXXXX = new FeatureLayerActions();
 
XXXXX.ShowAttributes(layer);
}
 
 
}