4

I have 1000+ Shapefiles and want to get extent of these polygon shapefiles. I know the manual way to do so.. Is there any way to do it through Script .

0

1 Answer 1

5
  • For shapefiles loaded into QGIS, you can use the following in the Python Console:

    for layer in QgsMapLayerRegistry.instance().mapLayers().values():   
        extent = layer.extent()
        xmin = extent.xMinimum()
        xmax = extent.xMaximum()
        ymin = extent.yMinimum()
        ymax = extent.yMaximum()
        print layer.name() + "|| xmin: %f, xmax: %f, ymin: %f, ymax: %f"% (xmin, xmax, ymin, ymax)
    

  • For shapefiles not loaded into QGIS (i.e. in a folder), you can use the following in the Python Console:

    import glob, os
    folder = "path/to/folder"
    for layer in glob.glob(folder + "*.shp"):
        layerName = os.path.splitext(os.path.basename(layer))[0]
        layer = QgsVectorLayer(layer, layerName, "ogr")
        extent = layer.extent()
        xmin = extent.xMinimum()
        xmax = extent.xMaximum()
        ymin = extent.yMinimum()
        ymax = extent.yMaximum()
        print layerName + "|| xmin: %f, xmax: %f, ymin: %f, ymax: %f"% (xmin, xmax, ymin, ymax)
    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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