ArcEnging 导入shp文件后自定义图层中各要素的颜色

经过前面一段时间狂乱的baidu&google,目前ArcEngine开的项目算是完成了基本的外观,这次解决的问题是从CAD导成shp文件后,先前在CAD中定义的颜色也是可以读取到的,只不过它的Color值是采用的 ACI 规范,需要转换成RGB我们才方便使用。当然主要的问题是MapControl导入shp文件时,它给每个图层中要素的颜色是随机的,不是原图中所定义的颜色,所以我们需要这样在google中搜索"ArcEngine 修改 图层颜色","ArcEngine 自定义要素颜色",又是一阵狂乱的搜索,当然也能找到一些信息,但最后还是在看那个E文的MSDN帮助时找到了解决办法。上面直接有一例子,这里做了一些修改,贴出来:

 

 public static void DefineFeatureColor(IGeoFeatureLayer pGeoFeatureLayer, string fieldName)
        {
            //Make the renderer.
            IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass();

            //These properties should be set prior to adding values.
            pUniqueValueRenderer.FieldCount = 1;
            pUniqueValueRenderer.set_Field(0, fieldName);

            IDisplayTable pDisplayTable = pGeoFeatureLayer as IDisplayTable;
            IFeatureCursor pFeatureCursor = pDisplayTable.SearchDisplayTable(null, false) as IFeatureCursor;
            IFeature pFeature = pFeatureCursor.NextFeature();

            bool ValFound;
            int fieldIndex;

            IFields pFields = pFeatureCursor.Fields;
            fieldIndex = pFields.FindField(fieldName);
            while (pFeature != null)
            {
                string classValue = pFeature.get_Value(fieldIndex).ToString();

                IColor color =  ColorUtilities.GetColorByACI(Int16.Parse(classValue));
                IRgbColor rgb = color as  IRgbColor;
                //System.Console.WriteLine("KVL:" + classValue+"-->RGB"+ rgb.Red + "," + rgb.Green + "," + rgb.Blue);

                ISymbol pClassSymbol = getSymbolByShapType(pGeoFeatureLayer.FeatureClass.ShapeType, color);

                //Test to see if this value was added
                //to the renderer. If not, add it.
                ValFound = false;
                for (int i = 0; i <= pUniqueValueRenderer.ValueCount - 1; i++)
                {
                    if (pUniqueValueRenderer.get_Value(i) == classValue)
                    {
                        ValFound = true;
                        break; //Exit the loop if the value was found.
                    }
                }
                //If the value was not found, it is new and it will be added.
                if (ValFound == false)
                {
                    pUniqueValueRenderer.AddValue(classValue, fieldName, pClassSymbol as ISymbol);
                    pUniqueValueRenderer.set_Label(classValue, classValue);
                    pUniqueValueRenderer.set_Symbol(classValue, pClassSymbol as ISymbol);
                }
                pFeature = pFeatureCursor.NextFeature();
            }

            //'** If you didn't use a predefined color ramp
            //'** in a style, use "Custom" here. Otherwise,
            //'** use the name of the color ramp you selected.
            pUniqueValueRenderer.ColorScheme = "Custom";
            ITable pTable = pDisplayTable as ITable;
            bool isString = pTable.Fields.get_Field(fieldIndex).Type == esriFieldType.esriFieldTypeString;
            pUniqueValueRenderer.set_FieldType(0, isString);
            pGeoFeatureLayer.Renderer = pUniqueValueRenderer as IFeatureRenderer;

            //This makes the layer properties symbology tab
            //show the correct interface.
            IUID pUID = new UIDClass();
            pUID.Value = "{683C994E-A17B-11D1-8816-080009EC732A}";
            pGeoFeatureLayer.RendererPropertyPageClassID = pUID as UIDClass;

        }

 

上面的代码大部分来自帮助文档中的例子,做了一些逻辑上的修改,就是把原例子中的Random颜色换成读取要素Color转成RGB再渲染。参数fieldName其实可以不用要,我这里就是"Color"这个字段名。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue.js 是一个流行的 JavaScript 框架,用于构建用户界面。它支持通过导入外部的 JavaScript 包来扩展功能。 要在 Vue 展示 SHP 文件,您可以使用第三方库来处理和解析 SHP 格式。其一种常用的库是 `shpjs`,它是一个基于纯 JavaScript 实现的 SHP 文件解析器。 首先,您需要在 Vue 项目安装 `shpjs`。可以使用 npm 或 yarn 命令来安装,例如:`npm install shpjs`。 在 Vue 组件,您可以通过 `import` 语句导入 `shpjs`,并使用它来解析 SHP 文件。以下是一个简单的示例: ```vue <template> <div> <input type="file" @change="handleFileChange" /> <div id="map"></div> </div> </template> <script> import shp from 'shpjs'; export default { methods: { handleFileChange(event) { const file = event.target.files[0]; const reader = new FileReader(); reader.onload = (e) => { const buffer = e.target.result; shp(buffer).then((geojson) => { // 处理解析得到的 GeoJSON 数据 console.log(geojson); // 在地图上展示 GeoJSON 数据 // 使用您喜欢的地图库,如 Leaflet、Mapbox 或 OpenLayers }); }; reader.readAsArrayBuffer(file); }, }, }; </script> ``` 在这个示例,我们创建了一个包含文件上传功能和地图展示区域的 Vue 组件。当用户选择了一个 SHP 文件后,`handleFileChange` 方法会被调用。在该方法,我们使用 `FileReader` 对象来读取文件内容,并将其传递给 `shp` 函数进行解析。解析完成后,您可以在控制台打印解析得到的 GeoJSON 数据,并使用地图库将其展示在地图上。 需要注意的是,这只是一个基本的示例,您还需要使用适合您项目需求的地图库,以及根据您的具体业务逻辑对解析得到的数据进行处理和展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值