Tag Info

Hot answers tagged

10

tl;dr import qgis import PyQt4 etc is the correct way Long version Yes the import order can matter and in the case of QGIS 2.0 and above it does matter. You should always import qgis.core or qgis.gui, even just import qgis is enough, before you import any PyQt stuff. That seems silly. Why? In QGIS 2.0 we switched to using version 2 bindings of SIP ...


8

Sextante is now called Processing and has become a core feature of QGIS 2.0. You can still load experimental versions of the plugin under it's new name.


4

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts. This answer is based on answers to Problem with import qgis.core when writing a stand-alone Python script that uses QGIS and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion. I suggest you to follow the work flow given in ...


4

I have found the answer I need by using the advanced python field calculator. I set the following parameters; Field type:String Field length: 10 Global Expression: import time Formula: value = time.strftime("%Y-%m-%d") This gave me a new field with today's date added at the end.


4

The Processing menu is tied to the Processing plugin. So please check if you have deactivated the Processing plugin in the Plugin Manager.


3

This answer is based on answers to Problem with import qgis.core when writing a stand-alone Python script that uses QGIS and to How to use QGIS explode lines with Python. I suggest you to follow the work flow given in Problem with import qgis.core when writing a stand-alone Python script that uses QGIS to enable your QGIS libraries in your OSGeo4W Shell. ...


3

Finally found the proper way of running processing algorithms via standalone PyQGIS scripts. Using Processing plugin version 2.2.0-2, you can try the following script: # Prepare the environment import sys from qgis.core import * from PyQt4.QtGui import * app = QApplication([]) QgsApplication.setPrefixPath("/usr", True) QgsApplication.initQgis() # Prepare ...


3

first thing, osgeo.gdal is for pure Python scripting: from osgeo import gdal raster = gdal.Open('your.tif') raster.GetProjection() Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers: ##raster_1=raster ##raster_2=raster you don't need it. To process the layers, the correct code is: raster_1 = ...


3

I found out what was wrong. No Layer call inside ggplot, here the right code: ##Vector processing=group ##showplots ##Layer=vector ##a=Field Layer ##b=Field Layer library("ggplot2") ggplot() + geom_point(aes(Layer[[a]], Layer[[b]])) Cheers


3

So why doesn't it work? Look at your code, specifically the line with the attribute fields. You need to point R/QGIS directly towards the field data vectors unless you attach them beforehand. The following code works without issues for me (provided that you have installed ggplot2): ##Vector processing=group ##showplots ##Layer=vector ##Field1=Field Layer ...


3

You get this message whenever the SAGA process is stuck somewhere, and therefor the resulting file is not created. The message tells you also that SAGA is correctly installed, so it must have to do with the data, or the parameters you entered. Usefull hints may come from the log, which is behind the error message box. Just a guess: Are all your layers in ...


3

I was starting to work with it a week ago and it's a little bit strange from the arc-user-perspective. You have to start with adding your input data for example with double-click on "vector layer". This is just an empty slot while you are creating the model. Afterwards you can add the functions. At the last function you give a name for the output. Everything ...


3

Another Thing to try: Go to Processing->Options and Configuration Click on Providers->SAGA Uncheck "Enable SAGA 2.0.8 compatibility"


3

Here is a way to set a primitive progress bar. Design is based on example you can found within the PyQgis Dev. Cookbook (see section 11.2) Example is designed to work with features of a QgsVectorLayer but it shows the main steps you could adapt for your process algo. The key to your problem is to find a way to evaluate the progression of your algo within ...


3

For using processing standalone, it must be initiated with an interface. Thus, you can create a dummy iface before calling processing (this worked for me for QGIS 2.4): import qgis app = qgis.core.QgsApplication([], True) import processing class DummyInterface(object): def __init__(self): self.destCrs = None def __getattr__(self, *args, ...


3

Try this: import os, glob, shutil root_dir = "C:\Users\xxxx\Desktop\Test\\" country_dir = "Country_" grid_path = "C:\Users\xxxx\Desktop\Test\Grid\Grid.shp" # Get all files that constitute the Grid Shapefile gridShpFiles = glob.glob(grid_path[:-3]+"*") for path,dirname,files in os.walk(root_dir): if country_dir in path: for f in gridShpFiles: ...


2

This is a pretty late answer but you can do this in the Model Builder, a simple example is shown below: With your vector point layer already added, you can access the Field Calculator: In the Formula textbox, you can add your little equation to calculate the percentage. Depending on the column names of the attributes, you can use something like this: ...


2

You could get statistics on that field (Vector > Analysis Tools > Basic statistics) which will tell you the mean, and then create a new attribute and use Field Calculator to fill every record with that value.


2

There appears to be a problem with your calculation of the rotation angle. You should replace the following code: if dy == 0: #avoid divide by zero dy = 0.0000000001 protate = abs( math.atan(dx/dy) ) * 180 / math.pi if (dx >= 0) and (dy >= 0): # 1st quarter: dx>0, dy>0 protate = protate elif (dx >= 0) and ...


2

Until I get to work the generic way of doing it, I will tell you a workaround. I use the Processing plugin version 2.2.0-2 (I suggest you to use this version), which is installed in /home/germap/.qgis2/python/plugins/ on my computer. You need to know this folder location, because you import the processing module from there. Since you know the provider ...


2

I have found Dos3 to be more relistic for Landsat 5 on the plains for green crops. No experience in other situations. Landsat 8 will respond differently to the various correction methods, compared to L5. I haven't used GRASS in a while, have migrated to the plugin for QGIS - see http://fromgistors.blogspot.com/


2

The problem you are facing is due to not valid layers constructed by QgsVectorLayer(). If you print shapefile.isValid() you will be getting False for all layers. This happens because you need to set the QGIS prefix path and initialize QgsApplication before using QgsVectorLayer in standalone scripts/apps. The solution for your problem is to: Add these ...


2

I assume your road_layer_i (for i in [0-99]) layers are not the same layer, that is, they do not have exactly the same lines. Having said that, I've modified your code snippet a bit, making these changes: Use QgsMapLayerRegistry.instance().mapLayers().values() instead of self.iface.mapCanvas().layers(). This ensures you'll iterate on all layers and not ...


2

I would try deleting the feature attribute that corresponds to class_field (you can do it right before you call setAttributes()): del featAttr[class_field_index] and deleting the field from fields (you can do it before the loop, once you have instantiated both fields and class_field_index): fields.remove( class_field_index )


2

I had the same problem, but, looking on the web, I found a solution. Simply, I installed SAGA from other repository (Installing SAGA GIS 2.1). Try the following commands from a command line: sudo add-apt-repository ppa:johanvdw/saga-gis sudo apt-get update sudo apt-get install saga I hope it works.


2

The tool works well. I modified slightly the code for producing only 3 raster (10, 20 and 30 Pixel Size) with a point shapefile with 5 features. import processing RasterPixelSize = (10.0,20.0,30.0) i=1 for rps in RasterPixelSize: processing.runalg("gdalogr:rasterize",\ "/home/zeito/pyqgis_data/point.shp",\ "id",None,1,rps,rps,6, ...


2

Try changing this line: outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], file, os.path.sep.join(path.split(os.path.sep)[:-1]) + os.path.sep + processed_dir + os.path.sep + file) to: outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], file, os.path.join(os.path.dirname(path), processed_dir, file))


2

For the first question: You need to create a QgsVectorLayer object from your Shapefile path. Only then you can access the getFeatures() function. That is: for fname in glob.glob(path_dir + "*.shp"): for feature in QgsVectorLayer(fname,"any name","ogr").getFeatures(): My recommended way of doing it You don't actually need to access getFeatures() nor ...


2

Assuming that you always want to match like files from Original and Processed folders (i.e. Paris(O).shp always matched with Paris(P).shp or New York City(O).shp always matched with New York City(P).shp), You need to add a conditional after your second for loop to filter out everything you are grabbing from the processed folder. for f in glob.glob(root_dir ...


2

Found a solution from this post where I only had to add the following code to the script: from osgeo import gdal gdal.PushErrorHandler('CPLQuietErrorHandler') After adding the above and removing the try statement, the working script ignores the ERROR 1 messages: from osgeo import gdal gdal.PushErrorHandler('CPLQuietErrorHandler') def run(): ...



Only top voted, non community-wiki answers of a minimum length are eligible