How i get image pixel coordinate (R,C) from image(tiff)

Вопросы по свободной ГИС QGIS. Сообщения об ошибках, предложения по улучшению, локализация.
Ответить
pkgis
Новоприбывший
Сообщения: 1
Зарегистрирован: 24 май 2010, 05:58
Репутация: 0

How i get image pixel coordinate (R,C) from image(tiff)

Сообщение pkgis » 24 май 2010, 07:17

Hello

i have a ploblem with geotiff, I want to get image coordinate in Row number and Column number.So i start to get canvas coordinate from mouse event and transform it in image coordinate (R,C) with QgsMapToPixel() class . I use the conner point of image

but it's not work. the result is not correct when i check with OpenEV. this is my code

Код: Выделить всё


 def leftClicked(self,xy):
	dt = "info-leftClicked"
	mapCoords = self.canvas.getCoordinateTransform().toMapCoordinates(xy["x"],xy["y"])
	# Get Image info
	img_layers = self.iface.mapCanvas().layer(0)
	extlayer = img_layers.extent()
	img_height = img_layers.height()
	img_width = img_layers.width()
	# warp canvas
	self.canvas.setExtent(extlayer)
	cav_pixs = self.canvas.getCoordinateTransform() # canvas dimension
	
	# cal canvas coordinte of image frame
	c_ul = cav_pixs.transform(0,0)
	c_ll = cav_pixs.transform(0,-img_height)

	# cal pixel size
	cc = (c_ul.y()-c_ll.y())/img_height
	# Build image_frame domian from canvas coordinate
	image_frame = QgsMapToPixel(cc,c_ul.y(),c_ll.y(),c_ll.x())
	# find point to image coord
	pix = image_frame.transform(xy["x"],xy["y"])
	
	
	self.pluginGui.textEdit_X.setText(str(mapCoords.x()))
	self.pluginGui.textEdit_Y.setText(str(mapCoords.y()))
	#-----
	#self.pluginGui.textEdit_X.setText(str(xy["x"]))
	#self.pluginGui.textEdit_Y.setText(str(xy["y"]))
	self.pluginGui.textEdit_mX.setText(str(float(pix.x()))
	self.pluginGui.textEdit_mY.setText(str(float(pix.y()))
	





Somebody know how i get the image coordinate at mouse event from the geotiff
thanks

Voltron
Гуру
Сообщения: 2627
Зарегистрирован: 29 мар 2007, 14:12
Репутация: 34
Откуда: Ukraine

Re: How i get image pixel coordinate (R,C) from image(tiff)

Сообщение Voltron » 24 май 2010, 20:47

Assume that we already have real coordinates of the point of interest. Here is code for Python console, you can easy adopt it for your plugin

Код: Выделить всё

import math # we will use floor() function from this module
iface = qgis.utils.iface
# our raster
layer = iface.mapCanvas().currentLayer()
# coordinates of point of interest (replace with our own)
x = 75.0791666
y = 57.2541666
extent = layer.extent()
width = layer.width()
height = layer.height()
# get raster resolution (pixel size)
xres = ( extent.xMaximum() - extent.xMinimum() ) / width
yres = ( extent.yMaximum() - extent.yMinimum() ) / height
# calculate row and column of this point
col = int( floor( ( x - ext.xMinimum() ) / xres ) )
row = int( floor( ( extent.yMaximum - y ) / yres ) )
Hope this helps

Ответить

Вернуться в «QGIS»

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и 1 гость