How can I obtain projected coordinates as well as the actual pixel values at those coordinates from a GeoTiff file and then save them into a numpy array? I have arsenci020l.tif file, and its coordinates are in meters. Below is the abridged output of gdalinfo I ran on it.
~$ gdalinfo arsenci020l.tif
Driver: GTiff/GeoTIFF
Files: arsenci020l.tif
arsenci020l.tfw
Size is 10366, 7273
Coordinate System is:
PROJCS["Lambert Azimuthal Equal Area projection with arbitrary plane grid; projection center 100.0 degrees W, 45.0 degrees N",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4326"]],
PROJECTION["Lambert_Azimuthal_Equal_Area"],
PARAMETER["latitude_of_center",45],
PARAMETER["longitude_of_center",-100],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]]]
Origin = (-6086629.000000000000000,4488761.000000000000000)
Pixel Size = (1000.000000000000000,-1000.000000000000000)
...
There was a similar question here about obtaining lat/long coordinates from tiff (Obtain Latitude and Longitude from a GeoTIFF File) and the answer showed how to obtain only top left x and y pixel coordinates. I need to obtain ALL projected pixel coordinates as well as get the pixel values and save them in a numpy array. How can I do it?
Answer
would add as comment, but a bit long - in case you wanted to use gdal/ogr within python - something like this might work (hacked together from some other code i had - not tested!) This also assumes that rather than finding the nearest raster pixel to a polygon centroid, you simply query the raster at the xy of the centroid. i have no idea what the speed tradeoff might be...
from osgeo import gdal,ogr
fc='PathtoYourVector'
rast='pathToYourRaster'
def GetCentroidValue(fc,rast):
#open vector layer
drv=ogr.GetDriverByName('ESRI Shapefile') #assuming shapefile?
ds=drv.Open(fc,True) #open for editing
lyr=ds.GetLayer(0)
#open raster layer
src_ds=gdal.Open(rast)
gt=src_ds.GetGeoTransform()
rb=src_ds.GetRasterBand(1)
gdal.UseExceptions() #so it doesn't print to screen everytime point is outside grid
for feat in lyr:
geom=feat.GetGeometryRef()
mx=geom.Centroid().GetX()
my=geom.Centroid().GetY()
px = int((mx - gt[0]) / gt[1]) #x pixel
py = int((my - gt[3]) / gt[5]) #y pixel
try: #in case raster isnt full extent
structval=rb.ReadRaster(px,py,1,1,buf_type=gdal.GDT_Float32) #Assumes 32 bit int- 'float'
intval = struct.unpack('f' , structval) #assume float
val=intval[0]
except:
val=-9999 #or some value to indicate a fail
feat.SetField('YOURFIELD',val)
lyr.SetFeature(feat)
src_ds=None
ds=None
GetCentroidValue(fc,rast)
No comments:
Post a Comment