zoukankan      html  css  js  c++  java
  • ArcGIS Engine Style文件操作

    对于一个GISer来说,地图,符号这些都应该有着比别人更深刻的理解和认识,作为平台软件都会提供一套自己的符号库,符号库里面根据类别和种类进行区分,因为点,线,面的自然存在和固有属性是不肯能让你用面状符号去渲染点和线的,学GIS的人,一般都会有分类的意识。
    
    说我是一个ArcGIS 的用户,其实算不上,只能说是ArcGIS 软件的一个粉丝和使用者而已,我工作的大部分时间都是和ArcGIS 打交道,虽然跟一个东西时间长了,了解的也就越多,这句话是不错,但是对我来说还远远不够,我在告诉别人ArcGIS 的某一个功能是怎么用的时候,还总想告诉别人这是为什么,这点其实并不容易,但是我从来没放弃过,只要有时间,我自己也会去思考一些问题,思考如何能给 别人说的清楚,说的通俗易懂,不断的去学习,不断的看ArcGIS的英文资料,也不断的用鼠标在ArcGIS上点来点去,总希望发现奇迹,前几天用ArcGIS Engine 做一些符号化的功能,学写了一些,也有了自己的一些心得,希望能和大家分享。遇到了很多问题,比如经常在打开style文件的时候报错,有的时候不报错,有的时候报错,折腾了好长时间,最后发现,每次用完之后,需要将和符号相关的几个对象释放掉。现在开始看几个和符号化相关的接口:
    
    
    
    IStyleGalleryStorage
    
    
    
    该接口被ServerStyleGalleryClass和StyleGalleryClass实现,其中前者是用来针对ArcGIS Engine的serverstyle文件,后者是针对ArcGIS Desktop的style文件的。IStyleGalleryStorage 用来管理可以使用IStyleGallery编辑的Styles文件,我们可以将IStyleGalleryStorage看做一个存放Style文件的一个大的容器,每一个style文件由IStyleGalleryItems组成,通过IStyleGalleryStorage 我们可以添加、删除、更新对应的style文件。ESRI为我们提供了很多的默认的style文件,这些style文件的默认的存放路径C:Program FilesArcGISStyles下。该路径下有两种类型的文件,一种是文件扩展名称为.ServerStyle的文件,一种是文件扩展名称为style的文件。ServerStyle文件用于AE的开发,Style文件是ArcMap可以使用的样式文件。ESRI自带的工具里面有一种工具可以将style类型的文件转换为ServerStyle类型的文件,然后提供给ArcGIS Engine使用。
    
    下面主要说明一下IStyleGalleryStorage对象的几个主要成员
    
    AddFile; 
    
    语法:Public Void AddFile(string path); 
    
    Path:表示文件在本地的存放位置,C:Program FilesArcGISStyles Business.ServerStyle文件
    
    该函数的意思是将一个指定位置的style文件添加到这个指定的容器里面,然后就可以通过IStyleGallery来对这些容器中的IStyleGalleryItem对象进行调用、处理等基本操作。
    
    2)DefaultStylePath: 是一个只读类型的属性,她返回的是ESRI中style文件默认的存放路径,这个跟ArcGIS软件的安装路径有关。返回值是一个String类型的变量,她的默认值可以是C:Program FilesArcGISStyles 这个是我本地计算机的DefualtStylePath的返回,因为我的ArcGIS软件安装在C盘。
    
    3)File属性  The file at the given index.  返回指定索引值的style文件的全路径名称、或者文件名称。
    
    4)FileCount :The number of files in the Style Gallery  返回 IStyleGalleryStorage中所包含的style文件的数量。
    
    5)RemoveFile(string path) 返回指定路径的style文件。
    
    6)TargetFile   The target output file for adding, updating and removing items. 这个是个可读、可写的属性。这个属性用来设置要编辑的目标文件,这个需要给定一个style文件的路径,如果本地计算机在指定的位置存在这个一个文件,那么系统就修改这个一个文件,如果本地计算机不存在这个一个文件,那么系统就将会在给定的位置创建一个新的style文件。当我们需要为IStyleGallery添加IStyleGalleryItem示例的时候,就需要指定这个属性,用来设置要编辑的IstyleGalleryStoragy中的某一个相对应的style文件。比如,在创建一个新的IstyleGalleryStoragy实例的时候,指定TargetFile属性,这个Style文件中就将默认的包含两个style文件,
    
    一个是C:UsersAdministratorAppDataRoamingESRIArcMapAdministrator.style文件,
    
    一个是C:Program FilesArcGISStylesESRI.style文件
    
    每一个文件都是由IStyleGalleryItem项组成的。并且每一个.style文件或.ServerStyle文件中的IStyleGalleryItem中的Item属性都有一个Class类型,然后根据不同的Class类型,添加后划分到对应的Class类型中,如下图所示:(关于Class类后面还有介绍) 
    
      
    
    如上图所示的ESRI.style文件中,有很多类型的IstyleGalleryItem项,比如:Reference Systems/Maplex Labels/Shadows/Area Patches/Line Patches等等。每一个类都有若干数量的IStyleGalleryItem组成,比如上图中的Color Ramps项就是由右边的IStyleGalleryItem项组成的。
    
    2:IStyleGallery 
    
    IStyleGallery可以看成是ISyleGalleryItem的容器,我们在上一节中已经提到我们可以把IStyelGalleryStorage看做一个.style和.ServerStyle文件的容器,并管理这些style文件,而这些style文件内是由ISyleGalleryItem项组成的。但是IStyleGaleryStorage却没有提供管理这些Style文件内的IStyleGalleryItem的借口,所以不能管理这些IstyleGalleryItem,这个时候就出现了IStyleGallery借口,由他来管理IStyleGalleryStorage容器内所有的IstyleGalleryItem项,比如,添加、删除、更新等操作。
    
    下面接下来讲解一下他的几个属性以及他的方法:
    
    ClassCount  只读属性,Number of classes in the Style Gallery。StyleGallery是有IStyleGalleryItem项组成的,这些IStyleGlleryItem分别属于不同的类,并且IStyleGalery默认的将IStyleGalleryItem分为12个类,如下图所示:
    
      
    
    每一个类都包含有若干个IStyleGaleryItem,如下图所示
    
      
    
    上图中,ESRI.Style文件包含22个类,当前我们选中North Arrows指北针类,然后右边就显示出来North Arrow类中所包含的IStyleGalleryItem项,可以看出有很多个这样的Item。
    
    ClassCount属性也就是返回当前的StyleGallery中所包含的类的数目。在IStyleGallery中,不管我们包含多少个IStyle文件,IStyleGallery中包含的类的种类的数量都是一定的,即22个。也就是说,每一个Style文件中包含的类的数量以及类的名称都是一样的,我们也可以这样理解,当我们创建一个.style文件的时候,系统就自动在style文件中创建22个类,尽管这个时候,每个类中的IStyleGalleryItem都为空。
    
    Class属性,通过调用.get_Class(int index) 来返回指定索引值的IStyleGaleryClass对象,请看下面一段代码
    
    int StyleGalleryClassCouont = styleGellery.ClassCount; 
    
                IStyleGalleryClass styleGaleryClass = null; 
    
                for (int i = 0; i < StyleGalleryClassCouont; i++) 
    
                { 
    
                    styleGaleryClass = styleGellery.get_Class(i); 
    
                    string name = styleGaleryClass.Name; 
    
                } 
    
    关于IStyleGalleryClass 我们可以在以后讲解,这里只需要知道通过这个属性可以返回一个IStyleGaleryItem类的一个示例。
    
    AddItem 语法如下所示:
    
    Public void AddItem(IstyleGalleryItem item); 
    
    描述:Adds an item to the target style file. 
    
    CateGories属性
    
    描述:The categories within the given class. 返回指定名称的Class中所包含的IStyleGallery所属的类别。
    
    每一个IStyleGalleryClass类都包含有多个IstyleGalleryItem项,为了大体上区别这些IstyleGalleryItem项,我们又给这些IstyleGalleryItem项归类,也就是说,每一个IstyleGalleryItem都有这个Cagegories(种类、类别)这个属性。如下图所示:
    
       
    
    
    
    5)Clear  方法
    
    语法:  Public void Clear(); 
    
    Discription:Removes all styles from the Style Gallery. 
    
    Items  返回满足指定参数的IstyleGalleryItems 
    
    Discription: The style items from the specified style file, in the specified class and category. The style set and category may be blank to return all items. 
    
    语法:IEnumStyleGalleryItem  get_Items(string className,string styleSet,string category) 
    
    className:表示,IStyleGalleryItem所属的类
    
    StyleSet:表示IstyleGalleryItem所存在的style文件
    
    Category:IstyleGalleryItem 所属的类别。
    
    参考代码:
    
                IStyleGallery styleGellery = new StyleGalleryClass(); 
    
                IStyleGalleryStorage styleGalleryStorage = styleGellery as IStyleGalleryStorage; 
    
                string tt = styleGalleryStorage.DefaultStylePath; 
    
                string iStyleFilePath = tt + "Civic.style"; 
    
                styleGalleryStorage.AddFile(iStyleFilePath); 
    
                string ClassName = "Color Ramps"; 
    
                string StyleCategory = "Default Ramps"; 
    
                IEnumStyleGalleryItem enumStyleGalleryItem = styleGellery.get_Items(ClassName, iStyleFilePath, StyleCategory); 
    
                IStyleGalleryItem StyleGalleryItemEnum = null; 
    
                ArrayList al = new ArrayList(); 
    
                while ((StyleGalleryItemEnum = enumStyleGalleryItem.Next()) != null) 
    
                { 
    
                    al.Add(StyleGalleryItemEnum.Name); 
    
                } 
    
                int itemCount = al.Count; 
    
    RemoveItem 
    
    Discription: Removes an item from the target style file. 
    
    这个方法挺简单,直接调用就可以了。
    
    SaveStyle 
    
    Discription: Saves the specified style to a file. If class is specified, only items in that class will be saved. 
    
    这里面的有一个参数:FileName不是很理解
    
    UpdateItem 
    
    Discription: Updates an existing item in target style file.(可以看出,这里需要指定IStyleGalery的TargetFile属性,用来更新的操作也是更新这个属性指定的文件中的Item成员) 
    
    
    
    IstyleGalleryItem 
    
    Defination: Provides access to members that define items in the Style Gallery. 
    
    Product Availability:Available with ArcGIS Engine, ArcGIS Desktop, and ArcGIS Server. 
    
    Description:Symbols and map elements are stored in the Style Gallery. Each symbol or map element has a unique ID that can be read from the item within the style gallery. A Name and Category are also properties of the items within the style gallery. These two fields, along with the Item itself, can be updated and changed as necessary. 
    
    主要方法:
    
    Category  :The category of the item. 
    
    ID: Id for the item in the Style Gallery. 
    
    Item: The symbol or map element to be stored in the Style Gallery item. 
    
    Name :The name of the item in the Style Gallery. 
    
    理解:IStyleGalleryItem项是IStyleGallery中最小的组成单元,即使是最小的单元,但是他也有她自己的属性,比如:Category、ID、Name等属性。我们也需要将IStyleGalleyItem理解成一个小的容器,因为每一个IStyleGalleryItem中都包含着一个属性Item属性,symbol或者地图都包含在这样一个IStyleGalleryItem项中。
    
    参考代码:
    
    IStyleGallery styleGallery = new StyleGallery(); 
    
    IEnumStyleGalleryItem styleGalleryItem = (IEnumStyleGalleryItem)styleGallery;//从这一行可以看出一个IStyleGallery实例可以直接的转换为一个IEnumStyleGaleryItem对象
    
    styleGalleryItem.Reset(); 
    
    string itemName = styleGalleryItem.Next().Name; 
    
    MessageBox.Show(itemName);
    
     
    
     注意:用AddItem、UpdateItem、RemoveItem之前,必须用到IStyleGalleryStorage接口的TargetFile属性。如果没有设置这个属性,就会报错,错误信息如下:
    
    "对 COM 组件的调用返回了错误 HRESULT E_FAIL。" 错误代码:"-2147467259"
    
    读取ServerStyle文件代码如下:
    
    private string styleName = "3D Basic.ServerStyle"; 
    
    public void ReadStyleServer() 
    
    { 
    
    IStyleGallery tStyleGallery = new ServerStyleGalleryClass(); 
    
    IStyleGalleryStorage tStyleGalleryStorage = tStyleGallery as IStyleGalleryStorage; 
    
    tStyleGalleryStorage.AddFile(styleName); 
    
    // tStyleGalleryStorage.TargetFile = styleName_TargetFile; 
    
    IEnumStyleGalleryItem tStyleGalleryItems = tStyleGallery.get_Items("Marker Symbols", styleName, ""); 
    
    tStyleGalleryItems.Reset(); 
    
    IStyleGalleryItem tStyleGalleryItem = tStyleGalleryItems.Next(); 
    
    int tIndex = 0; 
    
    try 
    
    { 
    
    while (tStyleGalleryItem != null) 
    
    { 
    
    string tName = tStyleGalleryItem.Name; 
    
    tStyleGalleryItem.Name = tName + tIndex; 
    
    tIndex++; 
    
    tStyleGallery.UpdateItem(tStyleGalleryItem);//这个地方报错 
    
    tStyleGalleryItem = tStyleGalleryItems.Next(); 
    
    } 
    
    } 
    
    catch (System.Runtime.InteropServices.COMException ex) 
    
    { 
    
    string tErrorMessage = ex.Message + 
    
    ex.ErrorCode; 
    
    } 
    
    finally 
    
    { 
    
    //释放这个接口,不然再次读取时会报错 
    
    ReleaseCom(tStyleGalleryItems); 
    
    ReleaseCom(tStyleGallery); 
    
    } 
    
    } 
    
    private void ReleaseCom(object o) 
    
    { 
    
    while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0) 
    
    { 
    
     
    
    } 
    
    } 
    
    要想更新,必须用到TargetFile属性。具体为什么必须用这个,原因不明,可能是文件独占。所以只能走一个弯。思路如下:
    
    (1)先把"3D Basic.ServerStyle"备份一个临时文件,备份的文件名称"Temp.ServerStyle"2)设置TargetFile,使其指向"Temp.ServerStyle"3)开始更新,更新完毕后,拷贝"Temp.ServerStyle"覆盖原来的"3D Basic.ServerStyle"
    
    代码如下(只写数据跟新,不写文件拷贝和覆盖)
    
    private string styleName_TargetFile = "Temp.ServerStyle"; 
    
    private string styleName = "3D Basic.ServerStyle"; 
    
    public void ReadStyleServer() 
    
    { 
    
    IStyleGallery tStyleGallery = new ServerStyleGalleryClass(); 
    
    IStyleGalleryStorage tStyleGalleryStorage = tStyleGallery as IStyleGalleryStorage; 
    
    tStyleGalleryStorage.AddFile(styleName); 
    
    tStyleGalleryStorage.TargetFile = styleName_TargetFile;
    
    IEnumStyleGalleryItem tStyleGalleryItems = tStyleGallery.get_Items("Marker Symbols", styleName, ""); 
    
    tStyleGalleryItems.Reset(); 
    
    IStyleGalleryItem tStyleGalleryItem = tStyleGalleryItems.Next(); 
    
    int tIndex = 0; 
    
    try
    
    { 
    
    while (tStyleGalleryItem != null) 
    
    { 
    
    string tName = tStyleGalleryItem.Name; 
    
    tStyleGalleryItem.Name = tName + tIndex; 
    
    tIndex++; 
    
    tStyleGallery.UpdateItem(tStyleGalleryItem); 
    
    tStyleGalleryItem = tStyleGalleryItems.Next(); 
    
    } 
    
    } 
    
    catch (System.Runtime.InteropServices.COMException ex) 
    
    { 
    
    string tErrorMessage = ex.Message + 
    
    ex.ErrorCode; 
    
    } 
    
    finally
    
    { 
    
    //释放这个接口,不然再次读取时会报错
    
    ReleaseCom(tStyleGalleryItems); 
    
    ReleaseCom(tStyleGallery); 
    
    } 
    
    } 
    
    private void ReleaseCom(object o) 
    
    { 
    
    while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0) 
    
    { 
    
     
    
    } 
    
    } 
    
    不再报错,顺利运行。AddItem、RemoveItem方法类似。AddItem的时候,如果目标文件不存在,接口会自动创建目标文件。
    
     
  • 相关阅读:
    module 和 component 的区别
    API、SDK、DLL有什么用?
    app基本控件
    PaaS是什么?
    js回调函数(callback)(转载)
    多语言 SEO
    axure rp 8.0
    整天看用户埋点数据,知道数据是咋来的吗?
    发现恶意ip大量访问 可使用命令进行封禁
    阿里云服务器迁移更改IP,导致网站挂掉
  • 原文地址:https://www.cnblogs.com/zuiyirenjian/p/3376372.html
Copyright © 2011-2022 走看看