Tag Info

Hot answers tagged

4

What you need is to create (if it doesn't exist yet) a startup.py file in the QGIS python folder: On GNU/Linux: /home/YOUR_USER/.qgis2/python/ On Windows: (User folder)/.qgis2/python/ Not sure about it, please tell me if you find it to update the answer and write inside that file something like this: from qgis.core import QgsMapLayerRegistry def ...


4

No you can't. The Python bindings need sip so you will have to get it working. However it is included with QGIS so if your getting that error something isn't setup right. When ever I create a standalone QGIS Python application (on Window at least) I use the following batch file: REM Change OSGeo4W_ROOT to point to your install of QGIS. SET ...


3

You can get the current map canvas' extent by running this line of code in the QGIS Python Console: iface.mapCanvas().extent().toString() In case that you want the extent of the active layer (as your code suggests), run this: iface.activeLayer().extent().toString() You could also get the individual numbers like this: e = iface.mapCanvas().extent() ...


2

After hours of trying I found a solution. It was inspired by this post: http://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/ It's not possible to link the attribute from the table with UI file directly. To display a photo in a custom form, the user needs an addtitional python script. Let's call it startForm.py . Its content is as ...


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(): ...


2

If you keep getting non valid layers, even defining them correctly, you're probably missing the QGIS prefix definition in your script (in case that you work out of QGIS). qgis_prefix="/usr" QgsApplication.setPrefixPath(qgis_prefix, True) QgsApplication.initQgis() If you work on Windows, your qgis_prefix should be something like this: ...


2

After some more experimenting and some looking through the source code, I found what I was doing wrong- The second Composition object needed "setPlotStyle" set to "Print". This is handled automatically by the renderPage method, and so why it isnt a problem for the composition used to render. render = canvas.mapSettings() planrender = ...


2

This should work for a polygon layer (I've tested it in the QGIS Python console, QGIS v.2.8.1): from qgis.gui import QgsRubberBand from PyQt4.QtGui import QColor rbDict = {} # We need this to store the rbs we'll create layer = iface.activeLayer() # Create rubber bands for f in layer.getFeatures(): fId=f.id() rbDict[fId] = ...


1

I have a very basic script, but it works just on point layers where the epsg 4326 (lat, lon) coordinates are stored in column as well. add this script below to your point layer, layer properties-actions (chose type python, and name it): import webbrowser new= 2 url= "https://maps.google.com/maps?z=18&q=&layer=c&cbll=[% "lat" %],[% "lon" ...


1

write 'D:\ne_10m_airports' as 'D:/ne_10m_airports' (change with uri formalism) or 'D:\ne_10m_airports' (escaping) or r'D:\ne_10m_airports' (r means raw string) regards


1

You can (sort of) achieve it by altering the QGIS layer tree model. Write the following code snippet in a QGIS Python console: root = QgsProject.instance().layerTreeRoot() model = QgsLayerTreeModel(root) model.setFlag(QgsLayerTreeModel.ShowLegend) model.setFlag(QgsLayerTreeModel.ShowLegendAsTree) model.setFlag(QgsLayerTreeModel.AllowNodeReorder) ...


1

Sassá, i'm not an expert but here's what i would do : At startup maintain a lookup table containing the layer id and the corresponding name u want to be left unchanged. Then i would use the layerNameChanged signal with a line of this kind : self.curLayer.layerNameChanged.connect(self.watchName) u do that for every layer of your project. then in the ...


1

You can reference/embed the layers from a second file. (by using the "Layer - Embed Layers and groups" menu entry) Layers that are embedded like that are read-only (so you can't rename them). You can move them around temporarily but those changes don't get saved.


1

You could use tools from Processing framework in PyQGIS. I think the tool you need from fTools is already in Processing, and it's called "Random points in layer bounds." Just follow the same logic explained in Cannot run standalone QGIS script for running a Processing algorithm in PyQGIS. You'll need to call the tool like this: ...


1

You have to change several things in the csv file and the code. In the first line of your csv file add the column labels x and y, since they are missing. The other labels are fine. par;num;ano;mes;dia;D;btq;x;y Since your decimal separator isn't the default colon but a comma you have to specify this explicitly. The fully qualified file name hasn't the ...


1

As Luigi anwered: self.canvas.scene().addItem(...) adds image to the canvas. Thanks!


1

a QgsMapCanvas is a QGraphicsView so you can add graphics items as usual in the scene of this class QGraphicsScene regards


1

Eventually, found a way to solve the problem, but I'm not 100% sure if this is it or not. After installing the PyQt4-devel, the next step would be to source the file (you can watch a nice tutorial done by Tim Sutton). If you still have problems with the make file, try to run the "compile strings" file just like in the video. Else if it doesn't work, ...


1

Not sure if I got you right, but you can add a legend widget to your PyQGIS application following How to add a legend to a canvas in a standalone PyQGIS application? Besides displaying your layers' symbology, it allows you to rename layers, set layer visibility, and change layer order in the map canvas.


1

To load a shapefile into QGIS and set the layer as "editable", I executed the code below in the Python Concole: LayerName =iface.addVectorLayer("/Path/To/Your/Shapefile.shp", "name", "ogr") qgis.utils.iface.setActiveLayer(LayerName) qgis.utils.iface.actionToggleEditing().trigger() For PostGIS tables this blog post may help to set up the SELECT statement ...


1

This code works in my Python Console: from qgis.core import QgsRasterLayer from PyQt4.QtCore import QFileInfo def StringToRaster(raster): # Check if string is provided fileInfo = QFileInfo(raster) path = fileInfo.filePath() baseName = fileInfo.baseName() layer = QgsRasterLayer(path, baseName) ...


1

QGIS, as distributed by OSGeo4W, usually comes with its own Python installation and its own packages that are independent of your "regular" Python installation. The easiest way to install a Python package into the OSGeo4W distribution is to open the OSGeo4W Shell and use pip from there. This will install the package into the Python distribution QGIS uses, ...


1

Just put dummy print 'xxx' statement immediately after self.iface.messageBar().pushMessage call.


1

Based in this answer, What is the replacement for 'setColorShadingAlgorithm' in QGIS 2.6?, I include the following code where it is also employed the procedure substitute for 'setColorShadingAlgorithm' ('setRasterShaderFunction', objet of the QgsRasterShader class). This code get the minimum and maximum values of the raster, by using some dataProvider class ...



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