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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章