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.

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Renee Cammarere
  • 1,025
  • 8
  • 20
  • Have you tried using the GDAL C# bindings directly? – Kersten May 19 '15 at 16:18
  • You can adapt my answer here: http://gis.stackexchange.com/questions/44958/gdal-importerror-in-python-on-windows/143140#143140; where results depends if the system is x32 or x64 (mine is x32). – xunilk May 19 '15 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. – Renee Cammarere May 19 '15 at 17:33
  • FWTools appears to only be compatible with 32bit Windows. – Renee Cammarere May 19 '15 at 17:39
  • Actually, I would like to try FWTools. However when I click on this link (http://home.gdal.org/fwtools/FWTools247.exe) I get the message "Oops! Internet Explorer could not connect to home.gdal.org" How do I download this extension? Thanks. – Renee Cammarere May 19 '15 at 18:27
  • 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. http://continuum.io/downloads – Jay Laura May 20 '15 at 02:22
  • I downloaded Anaconda. However, when I start up the Python GUI, I get an error message on "conda install gdal". – Renee Cammarere May 20 '15 at 19:54
  • 1
    FWTools is outdated, and far behind the current GDAL version. – AndreJ May 22 '15 at 14:27

1 Answers1

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])
Renee Cammarere
  • 1,025
  • 8
  • 20