使用ArcGIS Desktop在Python中将栅格图像获取为数组吗?


10

当开始使用Python和ArcGIS 9.3时,我假设将有一种简单的方法将栅格图像放入Python数组中,以便在将其存储为其他栅格图像之前可以对其进行操作。但是,我似乎找不到解决方法。

如果有可能,那怎么办?

Answers:


6

我认为ArcGIS <= 9.3.1不可能做到这一点

我将开源GDAL API用于此类任务。


大!我过去曾经使用过GDAL实用程序,但从未考虑过使用它们来执行此操作。
robintw

3
我同意,gdal Python模块使您可以轻松读取栅格并将数据转储到Numpy数组。克里斯·加拉德(Chris Garrard)开设了在GIS中使用开放源代码Python的课程,涵盖了该主题。您可以在以下网址
DavidF 2010年


6

fmark已经回答了问题,但这是我编写的一些示例OSGEO Python代码,用于将栅格(tif)读取到NumPy数组中,对数据进行重新分类,然后将其写到新的tif文件中。您可以读写支持gdal的任何格式。

"""
Example of raster reclassification using OpenSource Geo Python

"""
import numpy, sys
from osgeo import gdal
from osgeo.gdalconst import *


# register all of the GDAL drivers
gdal.AllRegister()

# open the image
inDs = gdal.Open("c:/workshop/examples/raster_reclass/data/cropland_40.tif")
if inDs is None:
  print 'Could not open image file'
  sys.exit(1)

# read in the crop data and get info about it
band1 = inDs.GetRasterBand(1)
rows = inDs.RasterYSize
cols = inDs.RasterXSize

cropData = band1.ReadAsArray(0,0,cols,rows)

listAg = [1,5,6,22,23,24,41,42,28,37]
listNotAg = [111,195,141,181,121,122,190,62]

# create the output image
driver = inDs.GetDriver()
#print driver
outDs = driver.Create("c:/workshop/examples/raster_reclass/output/reclass_40.tif", cols, rows, 1, GDT_Int32)
if outDs is None:
  print 'Could not create reclass_40.tif'
  sys.exit(1)

outBand = outDs.GetRasterBand(1)
outData = numpy.zeros((rows,cols), numpy.int16)


for i in range(0, rows):
  for j in range(0, cols):

    if cropData[i,j] in listAg:
        outData[i,j] = 100
    elif cropData[i,j] in listNotAg:
        outData[i,j] = -100
    else:
        outData[i,j] = 0


# write the data
outBand.WriteArray(outData, 0, 0)

# flush data to disk, set the NoData value and calculate stats
outBand.FlushCache()
outBand.SetNoDataValue(-99)

# georeference the image and set the projection
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs.SetProjection(inDs.GetProjection())

del outData



1

我不确定您可以逐像素地处理栅格,但是可以将地理处理对象与python API结合使用。

您可以使用任何工具箱进行此类操作。示例脚本为:

#import arcgisscripting

gp = arcgisscripting.create(9.3)

gp.AddToolbox("SA") # addint spatial analyst toolbox

rasterA = @"C:\rasterA.tif"
rasterB = @"C:\rasterB.tif"

rasterC = @"C:\rasterC.tif" # this raster does not yet exist
rasterD = @"C:\rasterD.tif" # this raster does not yet exist

gp.Minus_SA(rasterA,rasterB,rasterC)

gp.Times_SA(rasterA,rasterB,rasterD)

# lets try to use more complex functions

# lets build and expression first

expression1 = "slope( " + rasterC + ")"
expression2 = "(" + rasterC " + " rasterD + ") - " + rasterA 

gp.SingleOutputMapAlgebra_SA(expression1,@"C:\result_exp1.tif")
gp.SingleOutputMapAlgebra_SA(expression2,@"C:\result_exp2.tif")

这是您问题的跟进。仍然不可能。不确定版本10.0。


谢谢-非常有帮助。但是,理想情况下,我希望能够遍历整个栅格数组以对其执行各种操作。我本以为在ArcGIS中会有一种方法可以做到这一点,但也许没有!
robintw

robintw,对于我在参考文献中看到的内容,无法获得栅格的特定像素。我不确定在ArcPy(可从v10版本开始)中是否可以获取这些单独的单元,因为它们使用许多新功能扩展了python API。
乔治·席尔瓦

0

最简单的方法是将栅格转换为netCDF,然后将其打开并遍历网格。对于一个涉及基于分配给栅格像元的数据将栅格转换为要素数据的项目,我做了几乎相同的事情。我研究了很长时间,得出结论,使用netCDF可以轻松遍历网格数据。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.