Select to view content in your preferred language

Altering the SpatialReference of a file-based raster

1132
2
01-24-2014 11:48 PM
BillHoney
Occasional Contributor
Hi,

I am upgrading a 9.2 Arcobjects console application to 10.2 and I am having some problems with changing the spatialReference of rasters.

My application looks at a TIFF file (plain TIFF) , plus an associated .TFW & .PRJ file.
In the 9.3 version of the application, it made changes to the header tags of the .TIF file,  writing the spatial reference directly into the TIFF

In my upgraded 10.2 application, this part no longer works - the TIFF does not get altered & ArcMap still thinks the TIFF has unknown spatial references, and I can't figure out why. 

Can anyone Help?

Bill

Version 10.2 code.
            
            string pathName = Directory.GetCurrentDirectory(); 

            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine);
            
            //// Force ESRI to pick up the new Spatial reference from the .prj file 
            ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(prjFileName);

            // open the directory as a worekspace 
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
            IWorkspace workspace = workspaceFactory.OpenFromFile(pathName, 0);
            IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspace;

            // open the raster file 
            IRasterDataset r = (IRasterDataset)rasterWorkspace.OpenRasterDataset(tifFileName);
            r.BasicOpenFromFile(tifFileName); // not sure if this line is needed 
            IGeoDataset igs = r as IGeoDataset; 
            IGeoDatasetSchemaEdit2 gds = r as IGeoDatasetSchemaEdit2;

            gds.AlterSpatialReference( pSpatialReference) ;
            igs.SpatialReference.Changed(); // tell it that we have changed the rasters's spatial reference 


Original 9.2 Code (which no longer compiles under 10.2


 // Force ESRI to pick up the new Spatial reference 
 ISpatialReferenceFactory  pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass(); 
 ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(prjFileName);
   
            // Open the Raster & change its SpartialReference  
            RasterDatasetClass r = new RasterDatasetClass(); 
            r.BasicOpenFromFile(tifFileName); 
            r.AlterSpatialReference(pSpatialReference);
0 Kudos
2 Replies
BillHoney
Occasional Contributor
p.s.  I meant to say that the 9.2 code no  longer RUNS under 10.2.  It compiles but crashes on the line
r.AlterSpatialReference(pSpatialReference);

With an access violation
0 Kudos
MiguelSevilla
Deactivated User

Hi Bill, were you able to resolve your issue?  We have similar 9.3 code that is throwing an AccessViolationException in 10.2.2.  So I was wondering if you were able to find a fix for 10.x  Thank you!

0 Kudos