Tag Info

New answers tagged

0

With the next code you can get a ramp color red-yellow-blue . The min/max values for the renderer are got from raster stats. from PyQt4.QtCore import * from PyQt4.QtGui import * layer = iface.activeLayer() renderer = layer.renderer() provider = layer.dataProvider() extent = layer.extent() ver = provider.hasStatistics(1, QgsRasterBandStats.All) stats = ...


3

Assuming all your text files are into the same directory, you can run this code snippet in the QGIS Python console to get your files loaded as individual layers in QGIS: import os.path, glob layers=[] for file in glob.glob('/tmp/xy/*.txt'): # Change this base path uri = "file:///" + file + ...


0

The layer creation option separator should be helpful, i.e. SEPARATOR=TAB Based on the QgsVectorFileWriter.writeAsVectorFormat docs, this should do it (untested): QgsVectorFileWriter.writeAsVectorFormat(inputLayer, "D:/dstest/my_csv", "CP1250", None, "CSV", True, "", "SEPARATOR=TAB")


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

Change variables indir and outdir according to your need. The code will find each file with extension 'txt' in indir and every subdirectory of indir. If you need another coordinate system than EPSG 4326, please change the EPSG number in line 9. The converted files will be written to directory outdir. import os indir = 'G:/LANUV' outdir = 'G:/LANUV' for ...


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


2

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 ...


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 ...


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


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 ...


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, ...


3

Combine all text files in a windows directory with the copy command: copy *.txt river.txt On Linux use the cat (concatenate) command: cat * > river.txt Then in QGIS for the merged text file use 'Add Delimited Text Layer' button.


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


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.


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] = ...


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


0

I've made a plugin using a lot of the logic from the PinPoint plugin and pulled in the x and y coordinates into this function to open up streetview/bing bird's eye: import os def openStreetViewBird(self, x, y): link = 'http://data.mapchannels.com/dualmaps5/map.htm?lat=' + y + '&lng=' + x + ...


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" ...


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 ...


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 = ...


0

Relating to the error from your update, see the answer here if it helps. Sounds like a problem with a permanent entry for PYTHONPATH in your system variables.


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 ...


0

In your code, layer is only the layer name that you put into the combobox. It's NOT an actual vector layer instance. That's why you get an error that layer doesn't have a provider.


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



Top 50 recent answers are included