I was stuck for 2 or 3 days. I have read many lectures from RS/GIS Laboratory of Utah State University. Also I read «Python Geospatial Development» book and many docs from gdal.org.
So, my problem is:
I have GeoTiff files (*.tif) with WGS84 coordinate system, (for example 35 N 532402 4892945) and need to cut this image (tif file) by specified coordinates (minX=27.37 maxX=27.42 minY=44.15 maxY=44.20) without gdalwarp utility.
So anyone have some ideas or advice?
p.s. my code for this moment:
#! /usr/bin/python
# coding: utf-8
from osgeo import gdal
from osgeo.gdalconst import *
filename = raw_input("Enter file name: ")
# Get the Imagine driver and register it
driver = gdal.GetDriverByName('GTiff')
driver.Register()
dataset = gdal.Open(filename, GA_ReadOnly)
if dataset is None:
print 'Could not open ' + filename
sys.exit(1)
cols = dataset.RasterXSize
rows = dataset.RasterYSize
bands = dataset.RasterCount
transform = dataset.GetGeoTransform()
xOrigin = transform[0]
yOrigin = transform[3]
pixelWidth = transform[1]
pixelHeight = transform[5]
for i in range(3):
x = xValues[i]
y = yValues[i]
xOffset = int((x - xOrigin) / pixelWidth)
yOffset = int((y - yOrigin) / pixelHeight)
s = str(x) + ' ' + str(y) + ' ' + str(xOffset) + ' ' + str(yOffset) + ' '
for j in range(bands):
band = dataset.GetRasterBand(j+1) # 1-based index
data = band.ReadAsArray(xOffset, yOffset, 1, 1)
value = data[0,0]
s = s + str(value) + ' '
print s
dataset.GetGeoTransform()
But also I need to convert coordinates and cut this image. And I just have no idea what to do next.