2

We have 1000's of files for which projection/datum/geocs/projcs names has to be extracted. This code reads the filename listed in the csv.

from os import path
from osgeo import gdal,osr
import csv
csvpath = r"D:\\Test\\QGISPRJ.csv"
csvdir = path.split(csvpath)[0]
with open(csvpath, "r") as csvfile:
    fcs = [r[0] for r in csv.reader(csvfile)]
    for ds in fcs:
        print ds

Output:
E:\Santiago_Low\SourceData\Chile Projects\spectrem\Area2\Area2_AEM_x7.TIF
E:\Santiago_Low\SourceData\_South America Regional\233_080_Si_15m_321comp.ECW
E:\Santiago_Low\SourceData\Inactive Projects\ASTER_VNIR_orthoRGB_PSAD56_19S.TIF
E:\Santiago_Low_Priority\SourceData\Chile Projects\Explora_KRIP_Res.TIF

But, if i use getProjection() in the code - as mentioned in this thread - GDAL/Python: How do I get coordinate system name from SpatialReference? - i am getting this error

from os import path
from osgeo import gdal,osr
import csv
csvpath = r"D:\\Test\\QGISPRJ.csv"
csvdir = path.split(csvpath)[0]
with open(csvpath, "r") as csvfile:
    fcs = [r[0] for r in csv.reader(csvfile)]
    for ds in fcs:
        prj=ds.GetProjection()
        srs=osr.SpatialReference(wkt=prj)
        print srs.GetAttrValue('projcs')
        print srs.GetAttrValue('geogcs')
        print srs.GetAttrValue('Datum')
        print srs.GetAttrValue('Projection')

Traceback (most recent call last):
File "<input>", line 4, in <module>
AttributeError: 'str' object has no attribute 'GetProjection'

But if i run the same code without the csv file as an input and use the filename directly it works fine.

from osgeo import gdal,osr
ds=gdal.Open(r'E:\\Santiago_Low\\SourceData\\Chile Projects\\Ground POS EM\\Mapinfo\\300mSlice_Image.TIF')
prj=ds.GetProjection()
print prj
srs=osr.SpatialReference(wkt=prj)
print srs.GetAttrValue('projcs')
print srs.GetAttrValue('geogcs')

Since i am very much a beginner as far as Python is concerned, i don't know how to proceed.I've read this gdal/python: extracting projection info from hdf file and few other questions w.r.t 'str' object has no attribute error in python. But still couldn't able to figure out what's wrong.

2
  • 4
    ds is a string. You probably mean to open a dataset, i.e. for fname in fcs: ds = gdal.Open(fname) ... – Mike T Oct 18 '16 at 3:39
  • Should be closed... not likely to be helpful to anyone else. Simple mistake. – Benjamin Oct 18 '16 at 19:16
3

The attribute error is because ds is a string, and not a GDAL dataset.

It can be fixed by referencing the file name string in the for loop, then opening the dataset:

for fname in fcs:
    ds = gdal.Open(fname)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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