Tag Info

New answers tagged

0

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


1

Your code is uncomplete and not well formated. However, this adaptation seems to work as it is espected: canvas1 = iface.mapCanvas() layers = [] n = canvas1.layerCount() for i in range(n): layers.append(canvas1.layer(i)) canvas2 = canvas1 properties = {"color": "255,0,0", "outline_color": "255,0,255"} symbol = ...


2

You can do that in this way, from the QGIS Python console: from PyQt4.QtGui import QToolBar iface.mainWindow().findChild(QToolBar,'mLabelToolBar').setVisible(False) iface.mainWindow().findChild(QToolBar,'mLayerToolBar').setVisible(False) I got the tool bar names (e.g., mLabelToolBar and mLayerToolBar) from Settings->Customization...->Toolbars. Check ...


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


0

it is not possible as qgis2leaf.qgis2leaf_exec works on a layer list from QGIS and the current map display settings.


1

You could try using pyinstaller: https://github.com/pyinstaller/pyinstaller/wiki It will require some tweeking to get all the dependencies.


2

The best practice is to use a resource.qrc file and compile that in into a resource_rc.py file using pyrcc4. pyrcc4 ships in the QGIS folder run OSGeo4w.bat to get access to it. pyrcc4 -o resources_rc.py resource.qrc In your Python code you then do: import resources_rc icon = QIcon("Identity.png")


2

Non-valid layers are most of the times due to a wrong QGIS prefix definition. Please try with: qgis_prefix="C:\\Program Files\\QGIS Wiena\\apps\\qgis" QgsApplication.setPrefixPath(qgis_prefix, True) Which should go right before this line: QgsApplication.initQgis() Now your tested layers should be valid.


1

Without creating a QT application in the main function I directly called the function that initiates the QGIS libraries. That was the problem. Now it got solved after making changes.


2

Okay. Solved it. There were 2 things i had to do, to get the styles working. You must set the coordinate system explicitly. Loading Postgis layers with a preset CRS did not do that automatically (as we are used from QGis desktop). You can do so by: map.setDestinationCrs(QgsCoordinateReferenceSystem(srid)) You must explicitly set map units to meters, or ...


2

The problem is that with your first solution the result of roadsLayer.getFeatures() is an iterator and not a list ( iterable ) as in for feat in roadsLayer.getFeatures() and iterators can only be iterated over once, after which they are exhausted (look at Loop Like A Native, Understanding Python Iterables and Iterators or Build a Basic Python Iterator). ...


0

your question isn't a QGIS question, but a QT quetion. Legend tree is managed by QgsLegendTreeView that is a QTreeView. So all what you need is to browse you tree an hide rows to compact your legend with http://doc.qt.io/qt-4.8/qtreeview.html#setRowHidden If you simply want to remove a layer from QGIS (standalone or not) simply unregister it from ...


1

I've tested the following script on GNU/Linux, QGIS v.2.8.1, Processing v.2.6 and Processing v.2.9.1. import sys,os,glob from qgis.core import * from PyQt4.QtGui import * app = QApplication([]) QgsApplication.setPrefixPath("/usr", True) # Adjust it to your path QgsApplication.initQgis() path_dir = "/docs/borrar/test/" path_res = path_dir + "results/" ...


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


1

i don't recall all the steps i used, but with the anaconda distro, i placed a text file (something like qgislibs.pth) within my anaconda directory (or you default python install) - within the text file, include the path to your osgeo4w site packages - probably like C:\OSGeo4W64\apps\Python27\Lib\site-packages\ i think i also had to include a path in the ...


4

You can do it in three steps: Get group names, create groups, and move layers. For testing purposes, I've replicated your sample scenario: Run the following code snippet in your QGIS Python console: # 1. Get group names and list of layer ids root = QgsProject.instance().layerTreeRoot() dictGroups={} for layer in root.findLayers(): if ...


2

To complement Luigi's answer, this is how you can use the Layer Tree View for adding a legend and/or a ToC to your application. After you create your canvas in your standalone PyQGIS application, you need to create a QgsLayerTreeView. For that, you need a model, and for the model you need the layer tree root. Additionally, you need to use a ...


1

follow this guide Lutra consulting qgis-layer-tree-api-part-3 regards, Luigi Pirelli


1

if you do look into OGR - a great resource is the cookbook. One example of writing z (m) values into a shapefile might be something like: def mkline(): drv=ogr.GetDriverByName('ESRI Shapefile') ds=drv.CreateDataSource('test.shp') lyr=ds.CreateLayer('test',ogr.wkbLineString25D) flddef=ogr.FieldDefn('Name',ogr.OFTString) ...


2

You can do something like this (steps explained as comments): #Get the layer reference. In this example I'll use the current layer: cLayer = iface.mapCanvas().currentLayer() #Build the expression. STATE and CITY are field names; # state and city represent the values from your combo boxes expr = QgsExpression( "\"STATE\"='" + state + "' AND \"CITY\"='"+ ...


1

Here would be a way to add a new feature to a polygon vector: # reference your layer, such as the active layer lyr = iface.activeLayer() # get the vertexes of your new geometry and add them to a list coordinatePairs = [] coordinatePairs.append(QgsPoint(-80.23, -3.28)) coordinatePairs.append(QgsPoint(-65.58, -4.21)) coordinatePairs.append(QgsPoint(-65.87, ...


1

I would point out, in addition, that the GRASS model, v.what.rast takes a point layer and raster layer as input and uploads the raster values to a given attrib column for all points. To run this in the python console (within a GRASS session) you would do something like: import os import grass.script as grass input_points = "<your point list>" ...


2

Numpy is installed by default in QGIS (Standalone version, OSGeo4W version). For rasterio, there are many problems. I know that rasterio is powerful and easy to use (I use it) but why rasterio in a QGIS plugin while you have all the functions of PyQGIS ? (it was designed for the Python shell or for scripts, not to be used with QGIS/PyQGIS, as Fiona for the ...


3

Assuming you want to run your script within QGIS (from a script file or Python console), you can use the following: import processing processing.runalg("qgis.selectbylocation", INPUT, INTERSECT, METHOD, OUTPUT) Here is the help description provided by the Python console which defines each parameter: processing.alghelp("qgis:selectbylocation") ALGORITHM: ...


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

I think you need to include this line: QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Wien\\apps\\qgis", True) right before this one: QgsApplication.initQgis()


3

If you want to do that with PyQGIS, the def Val_raster(x,y,layer,bands,gt) of Python Script for getting elevation difference between two points becomes: def Val_raster(point,raster): return raster.dataProvider().identify(point,QgsRaster.IdentifyFormatValue).results().values() for point in points.getFeatures(): geompt = point.geometry().asPoint() ...


1

os.walk returns a tuple of three values: for path, dirs, files in os.walk() Also consider http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python


2

The reason you are not getting the exact shape you want is because when you vectorize a raster it usually outputs as a rectangular shape because rasters are rectangular grids. You have a few options here. You can apply a mask to the raster and vectorize only the area you want, but I think you don't know which area you want yet? In this case I would ...


1

Following the idea from Cannot rename output file using standalone script for QGIS, I would do something like this: for file in files: if file.endswith('.shp'): pieces = list(os.path.splitext(file)) pieces[0]=pieces[0][:-3]+"(B)" newFile = "".join(pieces) outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], ...


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


0

1) For calculate the Vincenty distance, you need coordinates in longitude, latitude (degrees) and you choose WGS84 (look at with Geopy:distance) 2) The coordinates of your points are in meters: u'units': u'm' (according to your Proj4 string (+a=6378137 +b=6378137 +k=1 +lon_0 +no_defs +proj=merc +units=m +x_0 +y_0) As your coordinates are in meters, you ...


1

setLayerName() will rename the layer: for layer in QgsMapLayerRegistry.instance().mapLayers().values(): layer.setLayerName('NewName')


1

QGIS for Mac (KyngChaos version) is an application bundle ( directory) and the content of QGIS.app is: The path of the modules is /Applications/QGIS.app/Contents/Resources/python The path of the qgis library is /Application/QGIS.app/Contents/Resources/python/qgis And of the processing module, for example: ...


2

Finally I found the answer : using the QgsLegendModelV2()class, and its functions inherited from QgsLayerTreeModel : "index(i,j)" to select each legend item and "rowcount" to obtain the rows number. All in a "for x in xrange" loop. That gives: for i in self.composition.items(): if isinstance(i,QgsComposerLegend): legend = i for i in ...



Top 50 recent answers are included