get raster properties of each raster in mosaic dataset

4064
5
11-14-2013 11:06 PM
SteffenSchwarz1
New Contributor
Hello,
i have the following problem. i have a mosaic dataset with many raster data in it. Now, i want to address the properties of each raster dataset in this mosaic. i need to do this, because i want to control, which spatial reference system is defined in the metadata of the raster dataset.

Normally i would just query the properties of the raster dataset by requesting the file itself, but in my case, it is important, that i check the properties of the raster in the mosaic attribute table.

I already wrote some code but it didn't work.

 import arcpy
import os

arcpy.env.workspace = '...path to my fgdb...'
datasetList = arcpy.ListDatasets("*", "Mosaic")

for dataset in datasetList:
    print dataset
    arcpy.env.workspace = dataset
    rows = arcpy.SearchCursor(dataset,"","","Raster; Name") 
    for row in rows:
        print row.Name
        outraster = arcpy.Raster(row.Raster)


the raster dataset is stored in the attribute table in the column "Raster". i just want to store the raster data value in the attribute table in an arcpy.Raster object before i address the properties. But every time i run the script i get the error "TypeError: expected a raster or layer name". it seems, that python doesn't realize it, that this value is a raster file.

how do i get the raster properties of each raster in a mosaic dataset? thanks for your help.

regards
Tags (2)
0 Kudos
5 Replies
ModyBuchbinder
Esri Regular Contributor
Hi

There is no access in Python to the raster attributes �?? you must use ArcObjects.
A nice solution could be to use the GP Export Raster Catalog paths to create a table with the raster paths. Then open a cursor on it and when you have the full path you can do anything with it.

Have fun
Mody
0 Kudos
SteffenSchwarz1
New Contributor
Hi

There is no access in Python to the raster attributes �?? you must use ArcObjects.
A nice solution could be to use the GP Export Raster Catalog paths to create a table with the raster paths. Then open a cursor on it and when you have the full path you can do anything with it.

Have fun
Mody


Hi,
thanks for your reply. I assumed something like this.

the solution with the raster paths sounds good, unfortunately it wouldn't work in my case. in my situation it is very important to query the raster properties directly in the mosaic dataset. i have to do that because i changed the reference system (define projection) of the raster datasets after i added them to a mosaic. the problem is, if i first add the rasters to a mosaic and afterwards change their reference system, the mosaic doesn't recognize the change. i found out that i have to do a synchronize that the changes are recognized. after synchronizing i would have run this python script to ensure, that really the raster properties are up to date.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Hi,
thanks for your reply. I assumed something like this.

the solution with the raster paths sounds good, unfortunately it wouldn't work in my case. in my situation it is very important to query the raster properties directly in the mosaic dataset. i have to do that because i changed the reference system (define projection) of the raster datasets after i added them to a mosaic. the problem is, if i first add the rasters to a mosaic and afterwards change their reference system, the mosaic doesn't recognize the change. i found out that i have to do a synchronize that the changes are recognized. after synchronizing i would have run this python script to ensure, that really the raster properties are up to date.


From what I understand, the Mosaic is just a reference pointing to the actual Raster Datasets that are used to populate it.  That is, I usually have a File Geodatabase as a workspace that contains both the Mosaic and the individual raster datasets that are added to it.

So, if you change/alter the projection information on the individual raster datasets that were originally added to the Mosaic, you should see the change in the properties of the individual rows of the raster field found in the Attribute table of the mosaic.

I am uncertain how to reset or redefine the coordinate properties of the Mosaic itself.

Use this to check the spatial reference of the individual raster datasets:


env.workspace = r'your path to the worspace containing the raster datasets'
for raster in arcpy.ListRasters():
    print raster
    desc = arcpy.Describe(raster)
    print("Spatial reference name: {0}:".format(desc.spatialReference.name))



I use this to redefine the spatial reference of all raster datasets contained in a workspace.  Note: I make this script the source for a Toolbox script/tool and set two parameters (a workspace parameter and a spatial reference parameter)


import arcpy
from arcpy import env
from arcpy.sa import *

prjfile = arcpy.GetParameter(1)
env.workspace = arcpy.GetParameter(0)

for raster in arcpy.ListRasters():
    arcpy.AddMessage(str(raster))
    arcpy.DefineProjection_management(raster, prjfile)
    arcpy.AddMessage("defined projection")

0 Kudos
SteffenSchwarz1
New Contributor

... I usually have a File Geodatabase as a workspace that contains both the Mosaic and the individual raster datasets that are added to it ...


Hi,
thanks for your reply. until now my raster datasets and the mosaic containing these rasters were not in the same FGDB. I assume, that's the reason why the mosaic doesn't recognize in its attribute table, when something is changed in the properties of the input rasters (for example: srs).

I tried your solution to put everything in one FGDB and this is working fine. that would be one idea to ensure the srs of the source raster datasets will be updated in the mosaic as well. the disadvantage is, because of my big amount of input raster datasets (more than 1 TB), the FGDB is getting bigger and bigger and can't be easily moved to another file location.

nevertheless thanks for your solution and i will think about it putting everything in one FGDB.

regards
0 Kudos
MichaelNesius
Occasional Contributor
I do something similar to James' approach to add the spatial reference info to the attributes. I work with many datasets, collected in a variety spatial reference systems, that are aggregated into a single derived mosaic dataset. I create a source mosaic for each mosaic and add the spatial reference info to the attribute table (by creating a table and then appending it to the mosaic).

arcpy.env.workspace = arcpy.GetParameterAsText(0) ##workspace containing the rasters of interest
rasterList=arcpy.ListRasters()
tableWS = arcpy.CreateTable_management(directory, table name) #  table that will later be appended to the mosaic
for ras in rasterList:
        qRaster = arcpy.Raster(ras)
        cursor=arcpy.InsertCursor(tableWS)
        row=cursor.newRow()
        row.Name =str(ras)
        
        spatial_ref=arcpy.Describe(ras).spatialReference
        spref = arcpy.Describe(ras).spatialReference.name
        row.CSDESC = spref
        
        LinearUnit=spatial_ref.linearUnitName
        row.RESO_UNIT_CD=str(LinearUnit)

        cellsize=(qRaster.meanCellWidth*qRaster.meanCellHeight)
        row.RESOLUTION=cellsize
del row, cursor



I then use arcpy.JoinField_management to append this table to the mosaic's footprint layer

In your case, to avoid moving all of your data around, you may want to follow Mody's advice to export the raster file paths to a table. Then join this table to the mosaic so that the attribute table contains this resulting Path field. Then Add a SpaRef string field to the mosaic and calculate it with the Python codeblock expression in the field calculator set to something like: arcpy.Describe( r!Path!).spatialReference.name

hope that's helpful, albeit a few months late.
-Mike
0 Kudos