1

Using the instructions here . . . https://pythongisandstuff.wordpress.com/2011/07/07/installing-gdal-and-ogr-for-python-on-windows/ I downloaded the following files: GDAL-1.11.1.win-amd64-py2.7.msi and gdal-111-1800-x64-core.msi

After doing this, I was able to create my Python script (which manipulates DTED data), and run this script from my C# code.

Then I ran into a few problems when attempting to manipulate the pixel values of the DTED file:

  1. I'm trying to use ReadAsArray and I get "ImportError: No module named _gdal_array" on the line in my Python script . . data = rb.ReadAsArray(0, 0, cols, rows) Referred to: Cannot import gdal_array http://lists.osgeo.org/pipermail/gdal-dev/2011-May/028708.html
  2. I also thought that I could use gdallocationinfo . . http://www.gdal.org/gdallocationinfo.html I get a syntax error on the file name . . .
gdallocationinfo C:\Data\TestFiles\TST.DT1 1000 1000
SyntaxError: invalid syntax

I find that it gives an error message on any file name that I enter.

Therefore, I installed OSGeo4W in order to be able to use some of the above mentioned GDAL functions that I would need (after reading this https://hub.qgis.org/issues/8811). By running the OSGeo4W Shell, I was able to type this in at the command line and get the desired result:

C:\>gdallocationinfo C:\Data\TestFiles\TST.DT1
 1000 1000
Report:
  Location: (1000P,1000L)
  Band 1:
    Value: 859

The problem is that I don't think that I can use a Python script with this OSGeo4W shell, which means that I would have to run some other kind of script from my C# code, I guess.

Are there other versions of msi files that I should have downloaded instead of GDAL-1.11.1.win-amd64-py2.7.msi and gdal-111-1800-x64-core.msi?

I'm just not sure which approach I should take.

8
  • Have you tried using the GDAL C# bindings directly?
    – Kersten
    Commented May 19, 2015 at 16:18
  • You can adapt my answer here: gis.stackexchange.com/questions/44958/…; where results depends if the system is x32 or x64 (mine is x32).
    – xunilk
    Commented May 19, 2015 at 16:24
  • I did go through the above mentioned process to get GDAL installed on my machine, and it works OK except for the fact that it does not support the ReadAsArray method. Commented May 19, 2015 at 17:33
  • 1
    My suggestion would be to install Anaconda python and then conda install gdal. This will create a self contained environment where GDAL and the python binding versions are in sync. continuum.io/downloads
    – Jay Laura
    Commented May 20, 2015 at 2:22
  • 1
    FWTools is outdated, and far behind the current GDAL version.
    – AndreJ
    Commented May 22, 2015 at 14:27

1 Answer 1

3

I was able to get around this problem by using ReadRaster:

from osgeo import gdal,ogr
ds = gdal.Open( 'C:/Data/TestFiles/DtedFile.DT1' )
rb=ds.GetRasterBand(1)
import struct
xsize = rb.XSize
ysize = rb.YSize
datatype = rb.DataType
#Reading the raster values  
values = rb.ReadRaster( 0, 0, xsize, ysize, xsize, ysize, datatype )  
#Conversion between GDAL types and python pack types 
data_types ={'Byte':'B','UInt16':'H','Int16':'h','UInt32':'I','Int32':'i','Float32':'f','Float64':'d'}  
values = struct.unpack(data_types[gdal.GetDataTypeName(rb.DataType)]*xsize*ysize,values)
#Now you can view any of these values in the array.
print(values[35])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.