AnsweredAssumed Answered

reproject featureclass

Question asked by navjohn on Aug 6, 2015
Latest reply on Aug 11, 2015 by Dan_Patterson

I am trying to reproject a featureclass using the IGeometry.Project method.

 

When I use it to change a featureclass from Lambert to GDA94, nothing changes.

I may be making a fundamental error here as the finer points of reprojection have always exited my head after a cup of tea.

 

Can someone tell me what I'm doing wrong?

 

I am using a Windows 7 machine with ArcGIS 10.1. The application is a standalone .exe one.

 

My code is:

 

   public static string reProject(string sFromWorkspace, string sFromFC, string sToProjectionFile, ref ToolStripProgressBar pProgress)

        {

            // Reproject sFromFC using the projection in file sToProjectionFile

            string sError = "";

            IWorkspaceFactory pFromWSFactory = null;

            IWorkspace pFromWS = null;

            IFeatureWorkspace pFromFWS = null;

            IFeatureClass pFromFC = null;

            IFeatureCursor pFromFCur = null;

            IFeature pFromFeat = null;

            IGeometry pGeom;

            IQueryFilter pQ;

            ISpatialReference pSpRef;

            ISpatialReferenceFactory pSpRefFact = null;

            bool bProgress = false;

            int iCount;

 

 

            try

            {

                if (File.Exists(sToProjectionFile) == false)

                {

                    sError = "File: " + sToProjectionFile + " does not exist";

                    return sError;

                }

                bProgress = pProgress != null;

 

 

                // From Workspace

                pFromWSFactory = workspaceFactoryGet(sFromWorkspace, ref sError);

                if (sError.Length > 0)

                {

                    sError = "reProject\r\n" + sError;

                    return sError;

                }

                pFromWS = pFromWSFactory.OpenFromFile(sFromWorkspace, 0);

                pFromFWS = (IFeatureWorkspace)pFromWS;

             

                // Spatial reference

                Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");

                System.Object obj = Activator.CreateInstance(factoryType);

                pSpRefFact = obj as ISpatialReferenceFactory;

                pSpRef = pSpRefFact.CreateESRISpatialReferenceFromPRJFile(sToProjectionFile);

 

 

                // Open the featureclass

                pFromFC = pFromFWS.OpenFeatureClass(sFromFC);

                iCount = pFromFC.FeatureCount(null);

                if (iCount == 0) return sError;

                if (bProgress == true)

                {

                    pProgress.Visible = true;

                    pProgress.Maximum = iCount;

                }

                iCount = 0;

           

                pFromFCur = pFromFC.Update(null, false);

                pFromFeat = pFromFCur.NextFeature();

                while (pFromFeat != null)

                {

                    pGeom = pFromFeat.ShapeCopy;

                    if (pGeom.IsEmpty == false)

                    {

                        pGeom.Project(pSpRef);

                     

                        pFromFeat.Shape = pGeom;

                        pFromFeat.Store();

                    }

                    Marshal.ReleaseComObject(pFromFeat);

                    pFromFeat = pFromFCur.NextFeature();

                    if (bProgress == true)

                    {

                        pProgress.Value = iCount;

                        iCount += 1;

                    }

                }

 

 

                return sError;

            }

            catch (Exception ex)

            {

                System.Diagnostics.StackTrace pStack = new System.Diagnostics.StackTrace(ex, true);

                System.Diagnostics.StackFrame pFrame = pStack.GetFrame(pStack.FrameCount - 1);

                int iLineNo = pFrame.GetFileLineNumber();

                sError = "Error: reProject; Line: " + iLineNo + " \r\n" + ex.Message;

                return sError;

            }

            finally

            {

                if (pFromFeat != null) Marshal.ReleaseComObject(pFromFeat);

                if (pFromFCur != null) Marshal.ReleaseComObject(pFromFCur);

                if (pFromFC != null) Marshal.ReleaseComObject(pFromFC);

                if (pFromWSFactory != null) pFromWSFactory = null;

                if (pFromWS != null) pFromWS = null;

                if (pFromFWS != null) pFromFWS = null;

                if (pSpRefFact != null) pSpRefFact = null;

                if (bProgress == true) pProgress.Visible = false;

            }

        }

 

Thanks,

 

John

Outcomes