Processing of special characters such as line breaks in ArcGIS attributes

In data processing, it is often necessary to associate the related attributes in Excel with spatial data. The data format in Excel is relatively free. Sometimes the data in Excel table contains special symbols such as line breaks. When the data containing line breaks is associated with the attribute table of ArcGIS, the interface does not support line breaks in the attribute table interface.

For example, the following two sample data show that the responsible unit is Taiping Street Office.

When edit mode is opened and double-clicked into this property, it appears as

At this point, in the Attributes window, the

In normal data processing, sometimes just to see the attributes of the data, it does not open the editing mode, nor does it necessarily click on the attributes. Therefore, this attribute is often treated as "Taiping Street Office".

In work, sometimes it is necessary to associate the information filled by users in other business databases with spatial data. When the data filled by users contain fields with more words such as notes, users often copy the whole content of other documents, which also causes this problem.

The above problems are difficult to solve. Firstly, the problem of line breaks in data is difficult to find. Secondly, even if it is found, the line breaks are difficult to delete because they are not displayed in ArcGIS. They are often seemingly deleted in the attribute window of ArcGIS, but they are not deleted in practice. In order to solve this problem, it is necessary to delete the line breaks directly in the attribute window of ArcGIS. It is often necessary to copy the above in the notebook and then copy it into the field.

To solve this problem, an ArcGIS plug-in is made, which traverses the fields of all character types in the layer and replaces the common special type of characters with other characters.

Main interface:

The code is as follows

Interface section:

<Window x:Class="WaterAssisterToolbar.AttrSpecialStrRemove.RemoveAttrSpecialStrFrm"
             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" 
             Title="Set up-Remove special characters"
        ResizeMode="NoResize"
             Height="300" Width="300" MaxWidth="300" MaxHeight="300">
    <Grid Name="RootGrid">
        <Label Content="layer" HorizontalAlignment="Left" Margin="12,8,0,0" VerticalAlignment="Top"/>
        <ComboBox Name="cmbLayer" HorizontalAlignment="Left" Margin="51,10,0,0" VerticalAlignment="Top" Width="229" SelectionChanged="cmbLayer_SelectionChanged"/>
        <GroupBox Header="Special characters to be removed" HorizontalAlignment="Left" Margin="12,41,0,0" VerticalAlignment="Top" Height="101" Width="268" Name="ToBeRemovedGroup">
            <Grid Height="90" Margin="0,0,0.2,-0.4" VerticalAlignment="Top">
                <CheckBox Name="chkEnter" IsChecked="True"  Content="Newline character \r\n" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
                <CheckBox Name="chkA" Content="Bell \a" IsChecked="True" HorizontalAlignment="Left" Margin="142,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.313,0.526"/>
                <CheckBox Name="chkF" IsChecked="True"  Content="Page change \f" HorizontalAlignment="Left" Margin="10,34,0,0" VerticalAlignment="Top"/>
                <CheckBox Name="chkT" IsChecked="True"  Content="Horizontal tabulation \t" HorizontalAlignment="Left" Margin="142,34,0,0" VerticalAlignment="Top"/>
                <CheckBox Name="chkV" IsChecked="True"  Content="Vertical tabulation \v" HorizontalAlignment="Left" Margin="142,58,0,0" VerticalAlignment="Top" Checked="chkV_Checked"/>

            </Grid>
        </GroupBox>
        <Label Content="Replace with" HorizontalAlignment="Left" Margin="12,151,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.155,0.477"/>
        <TextBox HorizontalAlignment="Left" Name="txtReplace" Height="23" Margin="64,151,0,0" TextWrapping="Wrap" Text="," VerticalAlignment="Top" Width="216"/>
        <Button Content="Determine" Name="btnOK" HorizontalAlignment="Left" Margin="49,220,0,0" VerticalAlignment="Top" Width="83" Height="30" Click="btnOK_Click"/>
        <Button Content="cancel" Name="btnCancel" HorizontalAlignment="Left" Margin="174,220,0,0" VerticalAlignment="Top" Width="83" RenderTransformOrigin="-0.557,1.094" Height="30" Click="btnCancel_Click"/>
        <CheckBox Content="Handle TOC All Layers in" Name="chkAll" HorizontalAlignment="Left" Margin="51,193,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

Interface part code:

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;

namespace Toolbar.AttrSpecialStrRemove
{
    /// <summary>
    /// Interaction logic of RemoveAttrSpecialStrFrm.xaml
    /// </summary>
    public partial class RemoveAttrSpecialStrFrm : Window
    {
        private IMap pMap = null;

        private IFeatureLayer pftlyr = null;

        public IFeatureLayer Pftlyr
        {
            get { return pftlyr; }
            set { pftlyr = value; }
        }
        private char[] specialCharArray;

        private bool isAll = false;

        public bool IsAll
        {
            get { return isAll; }
            set { isAll = value; }
        }

        private List<char> special_char;

        public List<char> Special_char
        {
            get { return special_char; }
            set { special_char = value; }
        }

        private string replace_str;

        public string Replace_str
        {
            get { return replace_str; }
            set { replace_str = value; }
        }

        public RemoveAttrSpecialStrFrm()
        {
            InitializeComponent();
            special_char = new List<char>();
            pMap = ArcMap.Document.FocusMap;
            GISCommonHelper.CartoLyrHelper.setFeatureLyrCombox(ref cmbLayer, pMap, esriGeometryType.esriGeometryAny);
        }

        private void chkV_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            replace_str = txtReplace.Text;
            isAll = chkAll.IsChecked.Value;

            if (chkA.IsChecked.Value)
            {
                special_char.Add('\a');
            }

            if (chkEnter.IsChecked.Value)
            {
                special_char.Add('\r');
                special_char.Add('\n');
            }

            if (chkF.IsChecked.Value)
            {
                special_char.Add('\f');
            }

            if (chkT.IsChecked.Value)
            {
                special_char.Add('\t');
            }

            if (chkV.IsChecked.Value)
            {
                special_char.Add('\v');
            }

            this.DialogResult = true;
        }

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

        private void cmbLayer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbLayer.SelectedIndex > -1)
            {
                pftlyr = cmbLayer.SelectedValue as IFeatureLayer;
            }
        }
    }
}

Core code:

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


namespace Toolbar.AttrSpecialStrRemove
{
    /// <summary>
    /// Remove special characters such as line breaks
    /// </summary>
    public class btnRemoveAttrSpecialStr : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public btnRemoveAttrSpecialStr()
        {
        }

        private List<char> special_char_list;
        private string replacestr=";";

        int cnt = 0;

        protected override void OnClick()
        {
            try
            {
                RemoveAttrSpecialStrFrm ras = new RemoveAttrSpecialStrFrm();
                if (ras.ShowDialog() == true)
                {
                    cnt = 0;
                    special_char_list = ras.Special_char;
                    replacestr = ras.Replace_str;

                    if (ras.IsAll)
                    {
                        List<IFeatureLayer> ftlyrlist=GISCommonHelper.CartoLyrHelper.getAllLayer<IFeatureLayer>(ArcMap.Document.FocusMap);
                        ftlyrlist.ForEach(p => {
                            Execute_specialStr_Remove(p);
                        });
                    }
                    else
                    {
                        Execute_specialStr_Remove(ras.Pftlyr);
                    }

                    MessageBox.Show("Processing completed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unknown anomalies occurred:"+ex.Message);
            }
            
        }

        /// <summary>
        /// Traversing elements, replacing special characters in each field of elements
        /// </summary>
        /// <param name="pftlyr"></param>
        /// <returns></returns>
        private int Execute_specialStr_Remove(IFeatureLayer pftlyr)
        {
            int cnt = 0;
            IWorkspaceEdit pwsEdit = (pftlyr.FeatureClass as IDataset).Workspace as IWorkspaceEdit;
            pwsEdit.StartEditing(false);
            pwsEdit.StartEditOperation();

            IFeatureCursor pftcursor = pftlyr.FeatureClass.Update(null, true);
            IFeature pFeature = pftcursor.NextFeature();

            while (pFeature != null)
            {
                System.Diagnostics.Debug.WriteLine(pFeature.OID);

                remove_feature_attr_specialStr(ref pFeature);
                pftcursor.UpdateFeature(pFeature);
                pFeature = pftcursor.NextFeature();
            }
            pwsEdit.StopEditOperation();
            pwsEdit.StopEditing(true);

            return cnt;
        }

        /// <summary>
        /// Replace special characters in all fields of elements
        /// </summary>
        /// <param name="pFeature"></param>
        private void remove_feature_attr_specialStr(ref IFeature pFeature)
        {
            for (int i = 0; i < pFeature.Fields.FieldCount; i++)
            {
                IField pfd = pFeature.Fields.get_Field(i);
                if (pfd.Type == esriFieldType.esriFieldTypeString)
                {
                    //Processing only character type values
                    string val = pFeature.get_Value(i).ToString();
                    string val_r = remove_specialStrInString(val);
                    if (!string.Equals(val, val_r))
                    {
                        pFeature.set_Value(i, val_r);
                    }
                }
            }
        }

        /// <summary>
        /// Replace special characters in strings
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string remove_specialStrInString(string str)
        {
            special_char_list.ForEach(p => {
                str=str.Replace(p.ToString(), replacestr);
            });

            return str;
        }

        protected override void OnUpdate()
        {
        }
    }
}

 

 

Keywords: Windows Attribute Excel

Added by madcat on Wed, 14 Aug 2019 16:08:07 +0300