I'm trying to access the CRS of a layer and use this to create a grid. I followed the answer in this post which works great in QGIS but I would like it to work in a standalone script. Here is a snippet:

for fname in glob.glob("C:\Users\moi\Desktop\Test\Grid\\" + "*.shp"):

    extent = QgsVectorLayer( fname, '', 'ogr' ).extent()
    centerx = (extent.xMinimum() + extent.xMaximum()) / 2
    centery = (extent.yMinimum() + extent.yMaximum()) / 2
    width = extent.xMaximum() - extent.xMinimum()
    height = extent.yMaximum() - extent.yMinimum()
    CRS = QgsMapLayer.crs(fname).authid()

    general.runalg("qgis:creategrid", 1000, 1000, width, height, centerx, centery, 1, CRS, None)

Running the above gives me the following error message:

TypeError: QgsMapLayer.crs(): first argument of unbound method must have type 'QgsMapLayer'


Bleh, answer was simple.

Just had to replace what I originally wrote:

CRS = QgsMapLayer.crs(fname).authid()

With this:

CRS = QgsVectorLayer( fname, '', 'ogr' ).crs().authid()

Works a treat, hope this helps someone!

| improve this answer | |
  • I'm also interested in seeing if there are better or any other methods of obtaining a shapefile's CRS =) – Joseph Jun 5 '15 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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