compare 2 shapefiles

431
2
10-18-2010 12:53 AM
YashikaSareen
New Contributor
Hi every1,

My task is to compare the attributes of two shape files.

my code(which is shown below)works fine on small data.no issues but if i have shapefiles of lets say 5000 records it takes a very long time.I want my tool to work on lacs of records with a very fast speed.can any1 help me with the solution.

my code is:

//using System;
//using System.Collections.Generic;
//using System.Text;

//namespace blobcompare
//{
//    public class Class1
//    {
//    }
//}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;
using System.IO;

namespace blobcompare
{
    [Guid("cc38d134-fe98-41e5-989f-dc36d8d9caba")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("blobcompare.Class1")]
    [ComVisible(true)]
    public class Class1 : ICommand
    {
        //private IHookHelper m_hookHelper = null;
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        static extern bool DeleteObject(IntPtr hObject);

        #region "Component Category Registration"
        [ComRegisterFunction()]
        static void Reg(string regKey)
        {
            MxCommands.Register(regKey);
        }

        [ComUnregisterFunction()]
        static void Unreg(string regKey)
        {
            MxCommands.Unregister(regKey);
        }
        #endregion

        public ILayer layer1;
        public ILayer layer2;
        public IMxApplication mxApplication;  //to create application display object

        IApplication m_application;

        private IntPtr m_hBitmap;
        public IApplication m_app1;
        public IApplication m_app;
        public IMxDocument m_mxDoc;
        public MxDocument m_mxDocument;
        public Map m_map;
        public PageLayout m_pageLayout;
        //private System.Windows.Forms.Button button1;
        public IFeatureClass featureClass;
        //public TextWriter txt = new StreamWriter("abc.txt");
        private bool m_enabled;

        #region ICommand Members

        public string Message
        {
            get
            {
                // Set the message string that appears in the statusbar of the
                // application when the mouse passes over the command.
                return "Compare";
                //return null;
            }

        }
        public int HelpContextID
        {
            get
            {
                return 0;
            }
        }

        public string Name
        {
            get
            {

                return "Compare";
                //return null;
            }
        }


        public void OnClick()
        {
            IActiveView activeView = m_mxDoc.ActiveView;
            int count = 0;

            layer1 = m_map.get_Layer(0);

            layer2 = m_map.get_Layer(1);


            try
            {

                IFeatureLayer pfl = (IFeatureLayer)layer1;

                IFeatureLayer pfl1 = (IFeatureLayer)layer2;
                IFeatureClass featureClass = pfl.FeatureClass;

                IFeatureClass featureClass1 = pfl1.FeatureClass;

                IFeatureCursor featureCursor = featureClass.Search(null, true);

              
                IFeature Feat = featureCursor.NextFeature();

            
                int count1 = 0;

                int feildval1 = featureCursor.FindField("BUILDINGID");
                int feildval2 = featureCursor.FindField("BLOCKID");
                int feildval3 = featureCursor.FindField("ROOFTYPE");
                int feildval4 = featureCursor.FindField("ROOFSUB");
                int feildval5 = featureCursor.FindField("ARCHTYPE");
                int feildval6 = featureCursor.FindField("RIDGE_HT");
                int feildval7 = featureCursor.FindField("EAVE_HT");
                int feildval8 = featureCursor.FindField("Area");
                int feildval9 = featureCursor.FindField("minZ");
               
                string errvals = "FID=";
                while (Feat != null)
                {
                    count = 0;
                    IFeatureCursor featureCursor1 = featureClass1.Search(null, true);
                    IFeature Feat1 = featureCursor1.NextFeature();
                    string buid = Feat.get_Value(feildval1).ToString();
                    int blkid = Convert.ToInt16(Feat.get_Value(feildval2));
                    int rftp = Convert.ToInt32(Feat.get_Value(feildval3));
                    int rfsub = Convert.ToInt32(Feat.get_Value(feildval4));
                    string arc = Feat.get_Value(feildval5).ToString();
                    double ridge = Convert.ToDouble(Feat.get_Value(feildval6));
                    double eave = Convert.ToDouble(Feat.get_Value(feildval7));
                    double area = Convert.ToDouble(Feat.get_Value(feildval8));
                    double minz = Convert.ToDouble(Feat.get_Value(feildval9));
                    while (Feat1 != null)
                    {
                       
                     
                        string buid1 = Feat1.get_Value(feildval1).ToString();
                       
                        int blkid1 = Convert.ToInt16(Feat1.get_Value(feildval2));
                      
                        int rftp1 = Convert.ToInt32(Feat1.get_Value(feildval3));
                     
                        int rfsub1 = Convert.ToInt32(Feat1.get_Value(feildval4));
                       
                        string arc1 = Feat1.get_Value(feildval5).ToString();
                      

                        double ridge1 = Convert.ToDouble(Feat1.get_Value(feildval6));
                       
                      
                        double eave1 = Convert.ToDouble(Feat1.get_Value(feildval7));
                      
                        double area1 = Convert.ToDouble(Feat1.get_Value(feildval8));
                      
                        double minz1 = Convert.ToDouble(Feat1.get_Value(feildval9));
                      

                        if ((buid == buid1) && (blkid == blkid1) && (rftp == rftp1) && (arc == arc1) && (ridge == ridge1) && (eave == eave1) && (minz == minz1) && (area==area1))
                     
                        {
                            count++;
                            //MessageBox.Show("Count " + count.ToString());

                            break;
                        }
                        else
                        {
                          
                            errvals += Feat1.OID.ToString() + " or FID=";
                           
                          
                        }
                        Feat1 = featureCursor1.NextFeature();
                           
                        }
                    
                        if (count < 1)
                        {
                            //txt.WriteLine("values did not match    " + Feat.get_Value(feildval1).ToString());
                            m_map.SelectFeature(layer1, Feat);
                            //m_map.SelectFeature(layer1, Feat);

                        }
                       
               
                    Feat = featureCursor.NextFeature();
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                  
                }
                                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
            }
            catch
            {
                MessageBox.Show("Error");
            }
            MessageBox.Show("done");
        }


        public int Bitmap
        {
            get
            {
                return m_hBitmap.ToInt32();

            }
        }
        public void OnCreate(object hook)
        {
            m_app = hook as IApplication;
            m_mxDoc = m_app.Document as IMxDocument;
            m_mxDocument = m_app.Document as MxDocument;
            m_map = m_mxDoc.FocusMap as Map;



        }


        public string Caption
        {
            get
            {

                return "Compare";
                //return null;
            }
        }

        public string Tooltip
        {
            get
            {
                // Set the string that appears in the screen tip.
                return "Compare";
                //return null;
            }
        }
        public bool Checked
        {
            get
            {
                return false;
            }
        }

        public bool Enabled
        {
            get
            {
                // Add some logic here to specify when the command should
                // be enabled. In this example, the command is always enabled.
                return true;
            }
        }
        public string HelpFile
        {
            get
            {
                return null;
            }
        }

        public string Category
        {
            get
            {
                // Set the category of this command. This determines where the
                // command appears in the Commands panel of the Customize dialog.
                return "Compare";
                //return null;
            }
        }

        #endregion



    }
}
0 Kudos
2 Replies
YashikaSareen
New Contributor
please help.it is realy urgent
0 Kudos
JamesCrandall
MVP Frequent Contributor
If all you are doing is comparing attribute information and not doing much spatial analysis, then ideally you should be doing the the attribute analysis in a database, using more traditional database query logic.  The .dbf file is not really going to be the best choice, and as a developer you will need to make alternative decisions on how to proceed.

This means maintaining attributes somewhere like SQLServer, SQLServerExpress or even Microsoft Access, with the option of managing the location info as well in these db's (either with ArcSDE or a Personal GDB) is going to be a better option.  These two db's will allow you to devlop T-SQL for the data processing, or even using ADO.NET in the application tier, rather than clumsily processing the attribute data with  ArcObjects.
0 Kudos