ArcMap DayDreamInGIS数据处理工具 插件之 搜狗词库生成

插件下载地址:见置顶 https://blog.csdn.net/u012839776/article/details/105910588

设计思路:将空间数据中某些字段唯一值提取出来,生成搜狗txt格式的词库,再导入搜狗输入法,方便使用。

使用方式:点击工具,在弹出的界面中配置

(1)图层代表需要提取的空间数据

(2)待提取字段中勾选需要提取的图层字段,此处示例勾选 河流名称字段

(3)保存路径 配置生成的词库文件的存储位置。

配置完成后,点击生成,即可在相应路径生成搜狗输入法格式的词库文件。

生成之后,在搜狗输入法中,按照如下方式导入即可。

导入成功后如下图所示,之后即可在搜狗输入法中使用。

实现思路:

这个插件思路比较简单,对空间数据的某个字段做唯一值统计,之后将统计完的结果,按照搜过输入法要求的词库格式写入txt文本即可。

搜狗输入法要求的词库格式为 每行一词。

核心源码

界面XAML:

<Window x:Class="DayDreamInGISTool.SouGouDicHelper.frmSGDicSet"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
        ResizeMode="NoResize"
            Title="词库生成"
            Width="500" Height="450" MaxWidth="500" MaxHeight="500">
    <Viewbox Name="RootViewBox" Stretch="None">
        <Grid Name="RootGrid">
            <Label Content="图层" HorizontalAlignment="Left" Margin="-211,-154,0,0" VerticalAlignment="Top"/>
            <ComboBox HorizontalAlignment="Left" Name="cmbLayer" Margin="-172,-153,-201.8,0" VerticalAlignment="Top" Width="417" RenderTransformOrigin="0.107,-0.721" SelectionChanged="cmbLayer_SelectionChanged"/>
            <GroupBox Header="待提取字段" HorizontalAlignment="Left" Margin="-213,-123,-149.8,-55.8" VerticalAlignment="Top" Width="406" Height="266">
                <ListView SelectionChanged="listview_SelectionChanged" HorizontalAlignment="Left" Height="243.2" Name="listview" VerticalAlignment="Top" Width="393.6">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="序号" Width="40" DisplayMemberBinding="{Binding Id}"/>
                            <GridViewColumn Header="字段名称" Width="100" DisplayMemberBinding="{Binding FieldName}"></GridViewColumn>
                            <GridViewColumn Header="别名" Width="100" DisplayMemberBinding="{Binding AliasName}"></GridViewColumn>
                            <GridViewColumn Header="选定" Width="100">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Tag="{Binding Id}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>

                        </GridView>
                    </ListView.View>
                </ListView>
            </GroupBox>
            <Button Content="全选" Name="btnAll" HorizontalAlignment="Left" Margin="200,7,-211.8,0" VerticalAlignment="Top" Width="55" Click="btnAll_Click" IsEnabled="False"/>
            <Button Content="全不选" Name="btnNotAll" HorizontalAlignment="Left" Margin="200,52,-211.8,0" VerticalAlignment="Top" Width="55" Click="btnNotAll_Click" IsEnabled="False"/>
            <Button Content="反选" Name="btnInverse" HorizontalAlignment="Left" Margin="200,93,-211.8,-24.8" VerticalAlignment="Top" Width="55" Click="btnInverse_Click" IsEnabled="False"/>
            <Label Content="保存路径" HorizontalAlignment="Left" Margin="-211,160,0,-98.8" VerticalAlignment="Top"/>
            <TextBox Name="txt_pth" HorizontalAlignment="Left" Height="23" Margin="-149,161,-140.8,-96.8" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="333"/>
            <Button Content="打开" Name="btnOpen" HorizontalAlignment="Left" Margin="200,161,-201.8,-96.8" VerticalAlignment="Top" Width="45" Height="23" Click="btnOpen_Click"/>
            <Button Content="生成" Name="btnOK" HorizontalAlignment="Left" Margin="-149,202,0,-145.8" VerticalAlignment="Top" Width="80" Height="31" Click="btnOK_Click"/>
            <Button Content="取消" Name="btnCancel" HorizontalAlignment="Left" Margin="83,202,-119.8,-145.8" VerticalAlignment="Top" Width="80" Height="31" Click="btnCancel_Click"/>

        </Grid>
    </Viewbox>

</Window>

界面交互:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using Microsoft.Win32;

namespace DayDreamInGISTool.SouGouDicHelper
{
    /// <summary>
    /// frmSGDicSet.xaml 的交互逻辑
    /// </summary>
    public partial class frmSGDicSet : Window
    {
        private string savedpth = string.Empty;

        public string Savedpth
        {
            get { return savedpth; }
            set { savedpth = value; }
        }

        private IFeatureLayer pftlyr = null;

        public IFeatureLayer Pftlyr
        {
            get { return pftlyr; }
            set { pftlyr = value; }
        }

        private List<string> selectedFieldList = new List<string>();

        public List<string> SelectedFieldList
        {
            get { return selectedFieldList; }
            set { selectedFieldList = value; }
        }

        private IFields pFields = null;

        IMap pMap = null;
        public frmSGDicSet()
        {
            InitializeComponent();
            pMap = ArcMap.Document.FocusMap;
            GISCommonHelper.CartoLyrHelper.setFeatureLyrCombox(ref cmbLayer, pMap, esriGeometryType.esriGeometryAny);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (pftlyr == null)
            {
                MessageBox.Show("请设置图层");
                return;
            }

            savedpth = txt_pth.Text;
            if (string.IsNullOrEmpty(savedpth))
            {
                MessageBox.Show("请设置词库保存路径");
                return;
            }

            if (selectedFieldList.Count == 0)
            {
                MessageBox.Show("请选择字段");
                return;
            }

            this.DialogResult = true;
        }

        private void cmbLayer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbLayer.SelectedIndex != -1) 
            {
                this.listview.Items.Clear();

                pftlyr = cmbLayer.SelectedValue as IFeatureLayer;

                pFields = pftlyr.FeatureClass.Fields;
                List<fddef> list = new List<fddef>();
                for (int i = 0; i < pFields.FieldCount; i++) 
                {
                    IField pf = pFields.get_Field(i);
                    fddef f = new fddef(pf);
                    f.Id = i + 1;
                    list.Add(f);
                }
                this.listview.ItemsSource = list;
            }
        }

        private void btnAll_Click(object sender, RoutedEventArgs e)
        {
            //全选
            var source = this.listview.ItemsSource as List<fddef>;
            var list = this.listview.Items;
            var it = list[0];

            source.ForEach(p => {
                p.IsChecked = true;
            });
        }

        private void btnNotAll_Click(object sender, RoutedEventArgs e)
        {
            //全不选
            var source = this.listview.ItemsSource as List<fddef>;
            source.ForEach(p =>
            {
                p.IsChecked = true;
            });
        }

        private void btnInverse_Click(object sender, RoutedEventArgs e)
        {
            //反选
            var source = this.listview.ItemsSource as List<fddef>;
            source.ForEach(p =>
            {
                p.IsChecked = !p.IsChecked;
            });
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            //选择保存路径
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "存储路径";
            if (pftlyr != null)
            {
                dlg.FileName = pftlyr.Name + "词库.txt"; // Default file name
            }
            else
            {
                dlg.FileName ="词库.txt"; // Default file name
            }
            dlg.DefaultExt = ".txt"; // Default file extension
            dlg.Filter = "text|*.txt"; // Filter files by extension

            // Process save file dialog box results
            if (dlg.ShowDialog() == true)
            {
                this.txt_pth.Text = dlg.FileName;
            }
            else
            {
                
            }
        }

        private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (fddef item in e.RemovedItems)
            {
                if (selectedFieldList.Contains(item.FieldName)) 
                {
                    selectedFieldList.Remove(item.FieldName);
                }
            }

            foreach (fddef item in e.AddedItems)
            {
                if (!selectedFieldList.Contains(item.FieldName))
                {
                    selectedFieldList.Add(item.FieldName);
                }

            }
        }

        //string selectedLineOfBusinessTag = string.Empty;
        //private void cbclick(object sender, RoutedEventArgs e)
        //{
        //    CheckBox ck = sender as CheckBox;
        //    if (ck.IsChecked==true)
        //    {
                
        //    }
        //}
    }

    public class fddef
    {
        public int Id { get; set; }
        public string FieldName { get; set; }
        public string AliasName { get; set; }

        public bool IsChecked { get; set; }

        public fddef(IField pfd) 
        {
            this.FieldName = pfd.Name;
            this.AliasName = pfd.AliasName;
            this.IsChecked = false;
        }
    }
}

核心处理:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System.Windows;

namespace DayDreamInGISTool.SouGouDicHelper
{
    public class btnSouGouDic : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public btnSouGouDic()
        {
        }

        protected override void OnClick()
        {
            frmSGDicSet fgs = new frmSGDicSet();
            if (fgs.ShowDialog()==true)
            {
                //写入
                using (FileStream fs = new FileStream(fgs.Savedpth, FileMode.OpenOrCreate, FileAccess.Write)) 
                {
                    using (StreamWriter sw = new StreamWriter(fs,Encoding.UTF8)) 
                    {
                        IFeatureLayer pftlyr = fgs.Pftlyr;
                        List<string> fieldlist = fgs.SelectedFieldList;
                        ITable pTable = pftlyr.FeatureClass as ITable;
                        fieldlist.ForEach(p =>
                        {
                            List<string> result = GISCommonHelper.FieldHelper.getAllUniquesValues(pTable, p);
                            result.ForEach(m =>
                            {
                                sw.WriteLine(m);
                            });
                        });
                    }
                }
                MessageBox.Show("生成成功");
            }
        }

        protected override void OnUpdate()
        {
        }
    }
}

GISCommonHelper.FieldHelper.getAllUniquesValues方法:

#region 统计某个字段所有的值
        /// <summary>
        /// 统计某个字段所有值,没有对字段类型进行判断,调用处需自己行处理
        /// </summary>
        /// <param name="pTable"></param>
        /// <param name="sField"></param>
        /// <returns></returns>
        public static List<string> getAllUniquesValues(ITable pTable, string sField)
        {
            IDataStatistics pds = new DataStatisticsClass();
            ICursor pCursor = pTable.Search(null, false);
            pds.Cursor = pCursor;
            pds.Field = sField;
            IEnumerator pEnumerator = pds.UniqueValues;
            pEnumerator.Reset();
            List<string> uv = new List<string>();
            while (pEnumerator.MoveNext())
            {
                uv.Add(pEnumerator.Current.ToString());
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);
            return uv;
        }

        /// <summary>
        /// 获取某个字段的统计值
        /// </summary>
        /// <param name="pCursor"></param>
        /// <param name="fd"></param>
        /// <returns></returns>
        public static IStatisticsResults GetStatistics(ICursor pCursor, string fd)
        {
            IDataStatistics dataStatistics = new DataStatisticsClass();
            dataStatistics.Field = fd;
            dataStatistics.Cursor = pCursor;
            
            //Get the result statistics
            IStatisticsResults statisticsResults = dataStatistics.Statistics;

            return statisticsResults;
        }
        #endregion

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值