简体   繁体   English

如何从外部运行QGIS的python脚本

[英]How to run python script for QGIS from outside

I have hundred shape files in a folder. 我的文件夹中有一百个形状文件。 I have following tasks 我有以下任务

  1. Find out what are the fields in each shapefile 找出每个shapefile中的字段是什么
  2. If there is no field 'City' in any of the shapefiles I have to add that field in those shapefile. 如果任何shapefile中都没有字段“ City”,则必须在这些shapefile中添加该字段。

I can access each layer in python console in QGIS using iface.activeLayer() 我可以使用iface.activeLayer()访问QGIS中python控制台中的每一层

But I want to run a stand alone python script which will perform the tasks. 但是我想运行一个独立的python脚本来执行任务。 Can anyone help? 有人可以帮忙吗?

In my humble opinion, if you want to execute python scripts without the QGIS GUI, you could use the OGR Python API directly. 以我的拙见,如果您想在没有QGIS GUI的情况下执行python脚本,则可以直接使用OGR Python API。

There's an entire "cookbook" here: https://pcjericks.github.io/py-gdalogr-cookbook/ 这里有一个完整的“食谱”: https : //pcjericks.github.io/py-gdalogr-cookbook/

The official API documentation is here: http://gdal.org/python/ 官方API文档在这里: http : //gdal.org/python/

Simple tutorial: http://www.gdal.org/ogr_apitut.html 简单教程: http//www.gdal.org/ogr_apitut.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM