-4

I have a GDAL python script that is wrong but it work perfectly on my python interpretor. Is this a virus? The code is:

from math import floor
from osgeo import gdal,ogr
import pandas as pd

src_filename='C:/Users/admin/Desktop/RF/vrt_vrancea.tif' shp_filename = 'C:/Users/admin/Desktop/RF/labels_shp/labels.shp'

src_ds=gdal.Open(src_filename)

gt_forward=src_ds.GetGeoTransform()

gt_reverse=gdal.InvGeoTransform(gt_forward)

df=pd.DataFrame(columns['label_id',1,2,3,4,5,6,7])

ds=ogr.Open(shp_filename) lyr=ds.GetLayer() for feat in lyr: pixel_value = []

pixel_value.append(feat.GetField('label'))
for band in range(1,8):


   rb=src_ds.GetRasterBand(band)
   geom=feat.GetGeometryRef()
   mx,my=geom.GetX(), geom.GetY()  #coord in map units

#Convert from map to pixel coordinates.
    px,py=gdal.ApplyGeoTransform(gt_reverse, mx, my)
    px = floor(px) #x pixel
    py = floor(py) #y pixel

    intval=rb.ReadAsArray(px,py,1,1)
    pixel_value.append(intval[0][0])
    print(intval[0][0]) ## WRONG. Should be intval[0]



df_temp=pd.DataFrame([pixel_value],columns=['label_id',1,2,3,4,5,6,7])
df = pd.concat([df,df_temp])  

df.to_csv('C:/Users/admin/Desktop/df.csv')

My problem is with this line: print(intval[0][0])

There is not a nested list in ReadAsArray function

Duststar
  • 11
  • 3
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 13 '23 at 11:40
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short [tour] for more about how the site works – Ian Turton Jul 13 '23 at 12:08
  • what do you mean "wrong"? – Ian Turton Jul 13 '23 at 12:09
  • 2
    When you copy code you are required under the CC-BY license to attribute it. – user2856 Jul 13 '23 at 13:08

1 Answers1

3

Your question boils down to:

print(intval[0][0]) ## WRONG. Should be intval[0]

If I try something like your code on a test raster, I see the following:

>>> print(intval)
[[-9999.]]
>>> print(intval[0])
[-9999.]
>>> print(intval[0][0])
-9999.0

In the first case, this is a list of lists of pixel values. The second line is then the first list of pixel values. The third line gets me the pixel value. The nested structure is because ReadAsArray can return an array, not just a single value like here.

I don't know why you think the code is "wrong" since you've not shown any error message or what you expect. If there is an error message with this line, then its much more likely that the code was written for a different version of the gdal module than some "virus"-based explanation. The code looks perfectly safe, although I've not run it.

Spacedman
  • 63,755
  • 5
  • 81
  • 115