Select to view content in your preferred language

ISpatialReferenceFactory.ExportESRISpatialReferenc eToPRJFile

1526
8
04-06-2010 10:00 PM
SenthilkumarM_S
Emerging Contributor
Hi All

I am new to ArcGIS Programming. Right now we are working with Raster Image Georeferencing. I need to create PRJ files for every Raster Image. I am not getting the Method for Using ISpatialReferenceFactory.ExportESRISpatialReferenceToPRJFile. Using this Function i need to create the PRJ file for selected Raster Image. Please help to slove this Issues.

Thanks

Senthilkumar
India
0 Kudos
8 Replies
FrankPerks
Emerging Contributor
Hi,

If you just need to create a prj for every raster you can do it very very easily in python:
"""
    Takes a workspace and creates prj files for rasters =)
    sys.argv[1]         ds dir
    -Frank Perks
"""
import os
import sys
import arcgisscripting
if __name__ == '__main__':
    gp = arcgisscripting.create()
    gp.workspace = sys.argv[1]
    ds_list = gp.listrasters()
    for ds in iter(lambda: ds_list.next(), None):
        prj_str = gp.describe(ds).SpatialReference.exporttostring()        
        open(os.path.join(gp.workspace, '%s.prj' % ds), 'w').write(prj_str)


This will create a .prj file for each raster in the directory you have given it.

If your looking for actual arcobjects code, then all i have is C++

HRESULT ExportPrjToFile(__in const IRasterPtr& ipSrcRaster, __in const ATL::CComBSTR& outputPath)
{
    HRESULT hr;
    IRasterPropsPtr ipRastProps = ipSrcRaster;
    ISpatialReferencePtr ipSrcSp;
    hr = pRastProps->get_SpatialReference(&ipSrcSp);
    if(FAILED(hr))
    {
        std::cerr << "[ERROR]: Cannot extract Spatial Reference from Raster" << std::endl;
        return hr;
    }
    
    ISpatialReferenceFactoryPtr ipSpFact(CLSID_SpatialReferenceEnvironment);
    
    // Get Spatial Ref store to file
    return ipSpFact->ExportESRISpatialReferenceToPrjFile(outputPath, ipSrcSp);
    
}


There might be a few syntax mistakes in that code, since i did it from memory. I skipped the steps about how to get an IRaster. Heres the overall overview of what you need:

0. Initialize ArcObjects
1. IRasterWorkspaceFactory creates a IRasterWorkspace in the specified directory
2. IWorkspace->OpenRasterDataset() creates IRasterDataset
3. IRasterDataset->CreateFullRaster() creates the IRaster
4. IRasterProps = IRaster
5. IRasterProps->get_SpatialRefernece() gets spatial reference object ISpatialRefernence
6. Create ISpatialReferenceFactory
7. ISpatialReferenceFactory->ExportESRISpatialReferenceToPrjFile(ISpatialReference)
8. Go to set 1 keep looping till all rasters are done
0 Kudos
SenthilkumarM_S
Emerging Contributor
Hi Frank Perks

I have tried you method, but Still I am not getting the PRJ Files. can you give me the details code for that.

Thanks

Senthilkumar
0 Kudos
FrankPerks
Emerging Contributor
Hi Frank Perks

I have tried you method, but Still I am not getting the PRJ Files. can you give me the details code for that.

Thanks

Senthilkumar


Did you try both methods?

Can you give me a bit more details, what API are you using, are there are hresult failure codes?

Also what kind of rasters are you using this on?
0 Kudos
SenthilkumarM_S
Emerging Contributor
Hi Frank Perks

I am working with Georeferenced JPG, PNG and TIF Files and i need to create PRJ for individual files with its respective name. I have tried the first method. while running Python i am getting "IndexError: list index out of range". But i don't know C++. so i have not tried. can you give me the detail VBA code, that will be great for me.

Thanks

Senthilkumar
0 Kudos
FrankPerks
Emerging Contributor
Hi Frank Perks

I am working with Georeferenced JPG, PNG and TIF Files and i need to create PRJ for individual files with its respective name. I have tried the first method. while running Python i am getting "IndexError: list index out of range". But i don't know C++. so i have not tried. can you give me the detail VBA code, that will be great for me.

Thanks

Senthilkumar


For the python error:

import os
import sys
import arcgisscripting
if __name__ == '__main__':
    gp = arcgisscripting.create()
    gp.workspace = r"PUT YOUR FOLDER HERE"
    ds_list = gp.listrasters()
    for ds in iter(lambda: ds_list.next(), None):
        prj_str = gp.describe(ds).SpatialReference.exporttostring()        
        open(os.path.join(gp.workspace, '%s.prj' % os.path.splitext(ds)[0]), 'w').write(prj_str)


Okay lets say i have the files cat.jpg, mat.tif, hat.png, etc. and i want to make projection strings out of them. All of these files are in "C:\drseuss\rasters" (this path will be where your rasters are located, for this example i will use what i have written here)

Before running the script we need to change this line:
    gp.workspace = r"PUT YOUR FOLDER HERE"

To something that makes sense (always use forword slashes, it'll make stuff easier)
    gp.workspace = r"C:/drseuss/rasters"

So it will look like this:

import os
import sys
import arcgisscripting
if __name__ == '__main__':
    gp = arcgisscripting.create()
    gp.workspace = r"C:/drseuss/rasters"
    ds_list = gp.listrasters()
    for ds in iter(lambda: ds_list.next(), None):
        prj_str = gp.describe(ds).SpatialReference.exporttostring()        
        open(os.path.join(gp.workspace, '%s.prj' % os.path.splitext(ds)[0]), 'w').write(prj_str)


Now save the script, and run it!

Now once its done: you will now have cat.prj, mat.prj, hat.prj files in C:/drseuss/rasters folder.

I unfortunately don't know any sort of VBA. Try the modified script above. if that doesn't work them hopefully someone can convert the C++ code to VBA
0 Kudos
SenthilkumarM_S
Emerging Contributor
Hi Frank Perks

The script is working and its creating PRJ files for each files. but i am getting below error while opening the PRJ files in ARC Catalog.

Error:
Edit Failed

Failed to create the Spatial reference system from the specified PRJ file. the input is not a workstation PRJ files.

Thanks

Senthilkumar
0 Kudos
FrankPerks
Emerging Contributor
Hi Frank Perks

The script is working and its creating PRJ files for each files. but i am getting below error while opening the PRJ files in ARC Catalog.

Error:
Edit Failed

Failed to create the Spatial reference system from the specified PRJ file. the input is not a workstation PRJ files.

Thanks

Senthilkumar


Theres a bug. in exporttostring() for spatial references. ironically using exporttostring() on a featurepoly, results in a correct projection string!

import os
import sys
import arcgisscripting

def fix_proj_str(proj):
    """
        Correct a projection strings
        
        exporttostring uses single quotes instead of double! 
        
        BUG!
    """
    return proj.replace("'", '"')

if __name__ == '__main__':
    gp = arcgisscripting.create()
    gp.workspace = r"CHANGE THIS TO YOUR PATH"
    ds_list = gp.listrasters()
    for ds in iter(lambda: ds_list.next(), None):
        prj_str = fix_proj_str(gp.describe(ds).SpatialReference.exporttostring())
        open(os.path.join(gp.workspace, '%s.prj' % os.path.splitext(ds)[0]), 'w').write(prj_str)


Same deal as before (change gp.workspace line). This creates the correct projection files. I double checked.
0 Kudos
SenthilkumarM_S
Emerging Contributor
Thanks Frank Perks

Now the script is working fine.
0 Kudos