Select to view content in your preferred language

New to .NET, need help getting bounding coordinates from a raster

1349
11
09-06-2011 11:41 AM
PatrickGross
Emerging Contributor
Hey everyone,

I'm working with a guy in my office to develop an AutoCAD solution to automatically load the appropriate topographic maps into an active AutoCAD window.

Our solution requires getting the bounding coordinates for each raster and writing them out to a table so when the main code runs, it knows which rasters lie in the given map extent.  We already have the code necessary to do coordinate translation, but I just cant figure out what ArcMap function to use to retrieve the X & Y min & max.  Any ideas?

Thanks in advance!
0 Kudos
11 Replies
MelitaKennedy
Esri Notable Contributor
Just taking a stab here--IRasterInfo::Extent?

(I don't work with rasters at the ArcObjects level)
0 Kudos
PatrickGross
Emerging Contributor
Okay, so I'll need a function to return the four values I need. So:

In the original code...
'Dimension the actual raster to be evaluated
Dim rasterDataset As IRasterDataset = RasterWorkspace.OpenRasterDataset(datasetName)
.
.
.
GetBoundingCoords(rasterDataset)


And in the function...
Function GetBoundingCoords(ByRef pRaster As IRasterDataset)
    
     'Dimension the Extent Variable
     Dim objExtent As IRasterInfo.Extent


From there, I'm just not sure how to assign the four bounding variables.  Do I need to use IEnvelope somewhere?  I'm so lost!!! 😕
0 Kudos
AlexanderGray
Honored Contributor
The extent of the rasterinfo is an IEnvelope.
Are you using vba or vb.net?
RasterInfo might be a little more than what you need.  The .net help has an example on how to get it from one of the raster bands.
If all you need is the corner coordinates from the rasterdataset, it might be easier to type cast (directcast in vb.net) that to an IGeodataset.  IGeodataset has a property called extent which also happens to be of a type IEnvelope.  This is all read only of course, editing a raster is whole different thing.
0 Kudos
PatrickGross
Emerging Contributor
The extent of the rasterinfo is an IEnvelope.
Are you using vba or vb.net?
RasterInfo might be a little more than what you need.  The .net help has an example on how to get it from one of the raster bands.
If all you need is the corner coordinates from the rasterdataset, it might be easier to type cast (directcast in vb.net) that to an IGeodataset.  IGeodataset has a property called extent which also happens to be of a type IEnvelope.  This is all read only of course, editing a raster is whole different thing.


I'm using vb.net to do this.  And yes, all I need is the corner coordinates from a given raster and stuff them in to Xmin, Xmax, Ymin, and Ymax variables for later use.  I just don't know how to get them.  I have the raster filenames in a datatable elsewhere in the program and I'd get the filepaths and filenames from that.
0 Kudos
AlexanderGray
Honored Contributor
Ok, well in the sample code you had opened the rasterdataset, that is a good start.
If you look at the developer help for Irasterdataset, you will notice this is an interface on the rasterdataset class.  So you actually have a rasterdataset object, you are just grabbing it through its IRasterDataset interface.  If you look at the rasterdataset class (not interface) you will notice it implements IGeodataset as well as IRasterDataset, and IGeodataset has an extent property.  So you need to cast your IRasterDataset variable to an IGeodataset variable.
dim geoDs as IGeodataset = directcast(rasterdataset, Igeodataset)
Then access the extent property and you have everything you need.
0 Kudos
PatrickGross
Emerging Contributor
Ok, well in the sample code you had opened the rasterdataset, that is a good start.
If you look at the developer help for Irasterdataset, you will notice this is an interface on the rasterdataset class.  So you actually have a rasterdataset object, you are just grabbing it through its IRasterDataset interface.  If you look at the rasterdataset class (not interface) you will notice it implements IGeodataset as well as IRasterDataset, and IGeodataset has an extent property.  So you need to cast your IRasterDataset variable to an IGeodataset variable.
dim geoDs as IGeodataset = directcast(rasterdataset, Igeodataset)
Then access the extent property and you have everything you need.


I forgot to mention that I'm working on this in Microsoft VB 2010 Express, so it's using the 4.0 framework.  I dunno if that's going to change anything.

I just realized that "RasterWorkspace.OpenRasterDataset" is highlighted and gives me the error message "'OpenRasterDataset' is not a member of ESRI.ArcGIS.DataSourcesRaster.RasterWorkspace" when I hover over the text.

Thanks for helping me understand IRasterDataset, though 🙂
0 Kudos
PatrickGross
Emerging Contributor
Sorry, I was getting ahead of myself.  Here's what I have now:

Sub GetBoundingCoords(ByRef pRaster As IRasterDataset, ByVal Filepath As String)

        Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New RasterWorkspaceFactoryClass()
        Dim rasterWorkspace As ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace = CType(workspaceFactory.OpenFromFile(Filepath, 0), ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace)
        Dim rasterDataset As IRasterDataset = rasterWorkspace.OpenRasterDataset(Filepath)
        Dim geoDs As IGeoDataset = DirectCast(rasterDataset, IGeoDataset)

    End Sub

So now, rasterWorkspace.OpenRasterDataset isn't underlined as an error because I've dimensioned rasterWorkspace.  However, "New RasterWorkspaceFactoryClass()" is underlined.
0 Kudos
PatrickGross
Emerging Contributor
So, here's my current code:

Public Function GetBoundingCoords(ByVal Filepath As String, ByVal Filename As String) As IGeoDataset

        Dim coordXMax As Double
        Dim coordYMax As Double
        Dim coordXMin As Double
        Dim coordYMin As Double

        Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New RasterWorkspaceFactory()
        Dim rasterWorkspace As ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace = CType(workspaceFactory.OpenFromFile(Filepath, 0), ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace)
        Dim rasterDataset As IRasterDataset = rasterWorkspace.OpenRasterDataset(Filename)
        Dim geoDs As IGeoDataset = DirectCast(rasterDataset, IGeoDataset)

        'Assign extents to variables
        coordXMax = geoDs.Extent.XMax
        coordYMax = geoDs.Extent.YMax
        coordXMin = geoDs.Extent.XMin
        coordYMin = geoDs.Extent.YMin

        'Test
        MsgBox(coordXMax)

    End Function




And this is the error that gets thrown on the Dim workspaceFactory line:

COMException occurred

Retrieving the COM class factory for component with CLSID {4C91D963-3390-11D2-8D25-0000F8780535} failed due to the following error: 80040111 ClassFactory cannot supply requested class (Exception from HRESULT: 0x80040111 (CLASS_E_CLASSNOTAVAILABLE)).


Any ideas?
0 Kudos
PatrickGross
Emerging Contributor
This might be the solution:

http://blogs.esri.com/Dev/blogs/arcgisdesktop/archive/2009/12/01/Migrating-to-Engine-9.4-and-using-t...

and this:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Overview/00460000002w000...



So it seems I have to bind my application to an ArcGIS product.  I'm having trouble figuring out the syntax, though.
0 Kudos