Arcgis Engine(ae)接口详解(7):地图(map)操作

            IMap map = hookHelper.FocusMap;
//跟map同一层次的activeView对象,他们都是“地图”的对象,map管理地图内容,activeView管理显示内容
IActiveView activeView = map as IActiveView;

//获取当前地图显示范围
IEnvelope extent = activeView.Extent;
//设置当前地图显示范围,相当于用代码实现缩放到某个范围
activeView.Extent = extent;

//获取地图全图范围
//地图浏览功能中的全图,他的范围就是这样获取
IEnvelope fullExtent = activeView.FullExtent;

//获取地图比例尺,同时可同于设置比例尺
double mapScale = map.MapScale;

//获取和设置地图单位
esriUnits unit = map.MapUnits;
//获取和设置地图的显示单位
esriUnits unit2 = map.DistanceUnits;

//刷新地图
activeView.Refresh();

//屏幕长度(单位是像素)转地图实际长度
double mapLength = MapHelper.PixelsToMapUnits(activeView, 5);

//获取Map的所有FeatureLayer
List<IFeatureLayer> lstFeatureLayer = MapHelper.GetAllFeatureLayerInMap(map);

if (1 == 2)
{
//在Map中通过真实表名获取图层,对于sde的表名不能带用户名
IFeatureLayer featureLayer = MapHelper.GetFeatureLayerByDatasetName(map, "roadLine");

}

foreach (IFeatureLayer featureLayer in lstFeatureLayer)
{
//图层相关说明

//IFeatureLayer代表矢量图层,而ILayer代表图层,也是所有类型图层接口都实现了ILayer,因此IFeatureLayer可以as到ILayer
ILayer layer = featureLayer as ILayer;

//获取和设置图层的可视状态(就是图层树界面里图层左边的checkbox)
bool visible = layer.Visible;
//获取或设置图层名称
string name = layer.Name;

//通过图层获取featureClass,是获取featureClass的方法之一。另一种方法是通过workspace获取
//注意IFeatureLayer和IFeatureClass是两个东西,虽然平时会都叫“图层”。IFeatureClass是指物理表,而IFeatureLayer指物理表加载到地图上形成的图层,前者更多指数据,后者只图层在地图的展示设置,如样式,标注等
IFeatureClass featureClass = featureLayer.FeatureClass;
}
调用的封装函数如下
        public static double PixelsToMapUnits(IActiveView activeView, double pixelLength)
{
tagRECT tagRect = activeView.ScreenDisplay.DisplayTransformation.get_DeviceFrame();
int width = tagRect.right - tagRect.left;

if (width < 0.00001) return pixelLength;
double scale = activeView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width / width;

return (pixelLength * scale);
}

public static List<IFeatureLayer> GetAllFeatureLayerInMap(IMap map)
{
List<IFeatureLayer> result = new List<IFeatureLayer>();

UID uid = new UIDClass();
uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; //FeatureLayer
IEnumLayer enumLayer = map.get_Layers(uid, true);
//对图层转换版本
ILayer layer = null;
while ((layer = enumLayer.Next()) != null)
{
IFeatureLayer featureLayer = layer as IFeatureLayer;
result.Add(featureLayer);
}

return result;
}

public static IFeatureLayer GetFeatureLayerByDatasetName(IMap map, string tableName)
{
List<IFeatureLayer> lstFeatureLayer = GetAllFeatureLayerInMap(map);

foreach (IFeatureLayer featureLayer in lstFeatureLayer)
{
if (featureLayer.FeatureClass != null)
{
string featureClassName = FeatureClassHelper.GetDatasetNameWithOutSDEUser(featureLayer.FeatureClass as IDataset);
if (string.Equals(featureClassName, tableName, StringComparison.OrdinalIgnoreCase))
{
return featureLayer;
}
}
}

return null;
}

public static string GetDatasetNameWithOutSDEUser(IDataset dataset)
{
if (dataset == null) return "";

string tDatasetName = dataset.Name;
if (tDatasetName.Contains("."))
{
tDatasetName = tDatasetName.Substring(tDatasetName.LastIndexOf(".") + 1);
}

return tDatasetName;
}

1 个评论

史上最好、最经典的ArcGIS Engine和ArcGIS Python开发学习教程重磅出炉了,观看地址: http://i.youku.com/gisxiaotian

下载地址:https://gisxiaotian.taobao.com/

要回复文章请先登录注册