Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Hi I am trying to set the projection on a raster to match that of a vector point layer. Thus I need to findout what is the projection of a give layer, to use it in the GDAL.Dataset.SetProjection() so that I can create the GEOTIFF with the appropriate projection.

How do I do That? (in Python)

share|improve this question
    
Another way to put it is: How to get the EPSG for the Layer? –  fccoelho Sep 25 '12 at 11:55
    
Do you want to reproject the raster to the vector's CRS using Python? Please try to clarify. –  underdark Sep 25 '12 at 12:01
add comment

1 Answer

up vote 3 down vote accepted

Short answer

qgis.utils.iface.activeLayer().crs().authid()
# returns: PyQt4.QtCore.QString(u'EPSG:26913')

Explanation

qgis.utils.iface.activeLayer() returns a reference to the active QgsMapLayer.

QgsMapLayer.crs() returns the crs or QgsCoordinateReferenceSystem for the layer.

QgsCoordinateReferenceSystem.authid() returns the Authority identifier for the crs as a QString.

However, this is assuming there is an active layer, it is of a vector type, and it has a valid crs. You will want to test for validity of those items before committing to reprojecting a raster.

If you are reprojecting, using GDAL.Dataset.SetProjection() will not suffice, since it will only assign a projection and not reproject (warp) the raster to the same as your vector layer.

share|improve this answer
    
I ended up using: layer.srs() to get the layer and then srs.epsg to get the EPSG code for the layer. Then, to generate the geotiff: see here: github.com/fccoelho/spatialKDE/blob/master/kernel.py in the to_geotiff method. –  fccoelho Sep 29 '12 at 16:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.