[GIS] How to get CRS of a raster layer in pyqgis

coordinate systempyqgis

I have this problem accessing to the coordinate reference system(crs) of a raster layer map.
I wonder if such thing is possible?
I have used this xml from GDAL open map service to load in a raster layer(crs id 900913) to my stand-alone pyqgis application (http://www.gdal.org/frmt_wms_googlemaps_tms.xml)
It displays the map just fine, but when I tried to draw an icon at a specific lat long position (crs id 4326), the icon appeared weirdly.
I used the QgsCoordinateTransform class to convert the coordinates.
Please advise me what I should do to get it right? I am very new to pyqgis.
Thanks a ton!

Best Answer

I am not entirely sure if i understood your question right, but you flagged it with pyqgis and it is quite simple to get the CRS here.

#Assume that you have a raster layer of type QgsRasterLayer
crs = rasterlayer.crs()
print crs.toProj4()
# You can extract CRS of multiple layers and simply compare them with ==
crs1 == crs2 # Returns false, if the CRS is differnet

Using gdal it would look similar to this

rasterPath = "your file" #load as a gdal image 
srcImage = gdal.Open(str(rasterPath))
srcImage.GetProjection() # Returns a string, which contains the crs. The string can be processed by python string splitting or by turning it into a list
Related Question