Automatic georeferencing

306
1
01-10-2011 02:15 AM
yashikasareen1
New Contributor
I have a shapefile which gives the values of the points that are required to georeference an image i.e upperleft.x,upperleft.y,upperright.x...etc.

on tool click my image gets georeferenced but the points it get georeferenced are not exactly the same as in the shape file.It is georeferenced sumwhat nearby those points.
Moreover the georeferenced image when added to arcmap to verify the result shows its spatial reference not set.although my mxd is already set to wgs84

can any1 help me.
My code is:



namespace Auto_Rectify
{
    [Guid("1c6bc303-e3b0-4fca-b9cf-22255393d05e")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Auto_Rectify.Class1")]
    public class Class1:ICommand
    {
        [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;
        //IActiveView activeView;//to create application display object

        IApplication m_application;
        IActiveView activeView;
        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 pageLayout;
        //private System.Windows.Forms.Button button1;
        public IFeatureClass featureClass;
        private bool m_enabled;
        double target_uleft_x;
        double target_uleft_y;
        double target_uright_x;
        double target_uright_y;
        double target_lright_x;
        double target_lright_y;
        double target_lleft_x;
        double target_lleft_y;

        #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 "AutoRectify";
                //return null;
            }

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

        public string Name
        {
            get
            {

                return "AutoRectify";
                //return null;
            }
        }


        public void OnClick()
        {
           
            ILayer layer;
            IWorkspaceFactory wpf = new RasterWorkspaceFactory();
            IRasterWorkspace raswp;
            raswp = wpf.OpenFromFile("E:\\rectification tool\\", 0) as IRasterWorkspace;
            IRasterLayer psourcerl;
            IRasterLayer pextentrl;





            IRaster pSourceRaster;
            IRaster pExtentRaster ;
            layer = m_map.get_Layer(0);
            IFeatureLayer pfl = (IFeatureLayer)layer;
            IFeatureClass featureclass = pfl.FeatureClass;
            IFeatureCursor featurecursor = featureclass.Search(null, true);
            IFeature feat = featurecursor.NextFeature();
            while (feat != null)
            {
                target_uleft_x = Convert.ToDouble(feat.get_Value(12));
                target_uleft_y = Convert.ToDouble(feat.get_Value(13));
                target_uright_x = Convert.ToDouble(feat.get_Value(14));
                target_uright_y = Convert.ToDouble(feat.get_Value(15)); ;
                target_lright_x = Convert.ToDouble(feat.get_Value(16));
                target_lright_y = Convert.ToDouble(feat.get_Value(17));
                target_lleft_x = Convert.ToDouble(feat.get_Value(18));
                target_lleft_y = Convert.ToDouble(feat.get_Value(19));
               //feat = featurecursor.NextFeature();
               feat = null;
            }

            //psourcerl=m_map.get_Layer(0) as IRasterLayer;
            //pSourceRaster = psourcerl.Raster;
            pextentrl = m_map.get_Layer(1) as IRasterLayer;
            pExtentRaster = pextentrl.Raster;


            IRasterProps prasterprops;
            prasterprops = pExtentRaster as IRasterProps;
            IEnvelope penv;
            penv = prasterprops.Extent;
            double uleft_x = prasterprops.Extent.UpperLeft.X;
            double uleft_y = prasterprops.Extent.UpperLeft.Y;
            double uright_x = prasterprops.Extent.UpperRight.X;
            double uright_y = prasterprops.Extent.UpperRight.Y;
            double lright_x = prasterprops.Extent.LowerRight.X;
            double lright_y = prasterprops.Extent.LowerRight.Y;
            double lleft_x = prasterprops.Extent.LowerLeft.X;
            double lleft_y = prasterprops.Extent.LowerLeft.Y;

         
            IRasterDataset2 rasterDataset;
            rasterDataset = raswp.OpenRasterDataset("ESVALL038035NeighObliq89W_060824.jpg") as IRasterDataset2;
            //rasterDataset.OpenFromFile("E:\\rectification tool\\ESVALL038035NeighObliq89W_060824.jpg");
            //rasterDataset.BasicOpenFromFile("E:\\rectification tool\\ESVALL038035NeighObliq89W_060824.jpg");
            //MessageBox.Show(rasterDataset.CompleteName);
            IPointCollection pointcollection_target = (IPointCollection)new PolygonClass();
            object o = Type.Missing;
          
            //TARGET POINTS
            IPoint ppoint = new PointClass();
            ppoint.X = target_uleft_x;
            ppoint.Y = target_uleft_y;
            pointcollection_target.AddPoint(ppoint,ref o,ref o);
            IPoint ppoint1 = new PointClass();
            ppoint1.X = target_uright_x;
            ppoint1.Y = target_uright_y;
            pointcollection_target.AddPoint(ppoint1, ref o, ref o);
            IPoint ppoint2 = new PointClass();
            ppoint2.X = target_lright_x;
            ppoint2.Y = target_lright_y;
            pointcollection_target.AddPoint(ppoint2, ref o, ref o);
            IPoint ppoint3 = new PointClass();
            ppoint3.X = target_lleft_x;
            ppoint3.Y = target_lleft_y;
            pointcollection_target.AddPoint(ppoint3, ref o, ref o);
            //SOURCE POINTS

            IPointCollection pointcollection_source=(IPointCollection) new PolygonClass();
            IPoint ppoint4 = new PointClass();
            ppoint4.X = uleft_x;
            ppoint4.Y = uleft_y;
            pointcollection_source.AddPoint(ppoint4, ref o, ref o);
            IPoint ppoint5 = new PointClass();
            ppoint5.X = uright_x;
            ppoint5.Y = uright_y;
            pointcollection_source.AddPoint(ppoint5, ref o, ref o);
            IPoint ppoint6 = new PointClass();
            ppoint6.X = lright_x;
            ppoint6.Y = lright_y;
            pointcollection_source.AddPoint(ppoint6, ref o, ref o);
            IPoint ppoint7 = new PointClass();
            ppoint7.X = lleft_x;
            ppoint7.Y = lleft_y;
            pointcollection_source.AddPoint(ppoint7, ref o, ref o);

            IRasterGeometryProc rasterPropc = new RasterGeometryProcClass();
            IRaster raster = rasterDataset.CreateFullRaster();
            //MessageBox.Show(raster.ToString());
            rasterPropc.PointsTransform(pointcollection_source, true, raster);
            rasterPropc.PointsTransform(pointcollection_target, true, raster);
            rasterPropc.Warp(pointcollection_source, pointcollection_target, esriGeoTransTypeEnum.esriGeoTransPolyOrder1, raster);
            //MessageBox.Show(rasterPropc.ToString());
            rasterPropc.Register(raster);

            //rasterPropc.Rectify(@"D:\\ESVALL038035NeighObliq89W_060824_rectify.jpg", "JPEG Image", raster);
            MessageBox.Show("Done");

        }
0 Kudos
1 Reply
yashikasareen1
New Contributor
I got the reason why the image is getting shifted.It is due to the image pixels.I tried doing manual georeferencing of the same image by 4 points and it gave the image at the same place as the tool is giving.Let suppose i cannot go on every image to match feature to feature.is there any other way???
0 Kudos