Tag Info

New answers tagged

2

Several things to check here: Firstly, make sure that %QGIS_PATH%\apps\qgis\python\plugins\processing is added to the PYTHON_PATH environment variable. Secondly, instead of: import processing from processing import * Use: import processing from processing.core.Processing import Processing Finally, before calling any algorithms using processing, do ...


3

It's just a widget so you sure can. Normally you would add it to a layout however that will resize your canvas when it pops up which is a bit annoying IMO. What you can do is just some logic to resize the bar to match the parent size when it is shown. Here is some code that I use to do just that: class MessageBar(QgsMessageBar): def __init__(self, ...


2

There's several methods but I will mention the ones I normally use, both using the Python Console. For SINGLE shapefiles: Method 1 - Using a method utilising the QGIS algorithm - Delete Column: output_0=processing.runalg('qgis:deletecolumn', myshapefile.shp, "myFirstField", None) output_1=processing.runalg('qgis:deletecolumn', output_0['OUTPUT'], ...


1

I was able to figure this out myself using the Shapely library, like so: from shapely.geometry import LineString, Point, Polygon epsilon = 0.001 def is_between(a, b, c): crossproduct = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y) if abs(crossproduct) > epsilon : return False # (or != 0 if using integers) dotproduct = (c.x - a.x) * ...


1

You can get the layers selected in the TOC with something like this selectedLayers = iface.legendInterface().selectedLayers() the selectedLayers method in QgsLegendInterface do the trick.


2

You are on the right track. The thing that bit you are the backslashes in your prefix path \ which are escape sequences in python. Just do it the same way as you do it with the layer path and use slashes / instead. qgishome = "C:/OSGeo4W64/apps/qgis" A useful snippet that you can run after QgsApplication.initQgis() that makes sure that the providers are ...


2

The folder for the PYTHONPATH must be the compiled one and not the source one. Your build path should be something like /home/user/QGIS-master/<builddir>/output/. I am guessing you are running from build your QGIS version so you just export the environment var like: export PYTHONPATH=/home/user/QGIS-master/<builddir>/output/python/ maybe it ...


1

Got it working now using this user-defined function. from qgis.core import * from qgis.gui import * from qgis.utils import iface @qgsfunction(args=0, group='Custom', usesgeometry=True) def mapCenter(value1,feature, parent): point = iface.mapCanvas().extent().center().toString() return point


1

Rebelious, here is what i did to solve your question (maybe not the smarter solution but working "automatically" at least ...) : from PyQt4.QtCore import QObject,QTimer from PyQt4.QtGui import * import re class Test(): openedTables=[] def main(self): QApplication.instance().focusChanged.connect(self.monitor) def ...


2

areas = [] for line_feature in line_layer.getFeatures(): cands = area_layer.getFeatures(QgsFeatureRequest().setFilterRect(line_feature.geometry().boundingBox())) for area_feature in cands: if line_feature.geometry().intersects(area_feature.geometry()): areas.append(area_feature.id()) area_layer.select(areas)


0

I found alternative to OpenLayers plugin. I used WMS format in GDAL Check below links http://www.gdal.org/frmt_wms.html http://www.gdal.org/frmt_wms_googlemaps_tms.xml I loaded the WMS service as raster layer in QGIS and then used QgsMapRendererParallelJob class to render the map. mapSettings = iface.mapCanvas().mapSettings() ...


1

You could use the GDAL Ogr 2 Ogr algorithm to convert the vector file to a CSV, and then run that CSV through the QGIS algorithm "Delete column" to remove the geometry column (and any others you don't need). I haven't tried it, but it might work. [Edit] Okey, did a quick test. The GDAL convert to CSV works fine in model builder, but it seems like the QGIS ...


1

Bonjour everybody, The user Joseph help me to solve the problem. I'm so happy to share with you the steps: Open QGis 2.8.2 create your pluging with pluging builder, click ok when finishing fill the form. select the folder pluging(C:\OSGeo4W64\apps\qgis\python\plugins) where your pluging will be save. The pluging builder result appear, and then press Ok. ...


3

You can use the hasGeometryType() method from QgsVectorLayer. To examinate whether the selected layer has an associated geometry you can run this code in the QGIS Python console: iface.activeLayer().hasGeometryType() If it is True, the layer has a geometry (i.e., it's an spatial table). If it is False, the layer is a table (i.e., the table is non ...


1

To get the width of the map item in millimetres: map_item.rect().width() And to get the height of the item: map_item.rect().height()


1

Sorry. I was in a mistake: The current CRS is get by authid() method, not geographicCRSAuthId() On the other hand, I wanted to avoid the CRS Dialog when I load a vector layer from pyQGIS. Similar questions are been asked here and here . I have got the results setting QGIS like: Setting>Options>CRS ->Use CRS of project And I put my snippet of code to ...


1

You're looking for placementFlags. So, palyr.placementFlags = QgsPalLayerSettings.AboveLine should work. Note that LinePlacementFlags can be combined, so palyr.placementFlags = QgsPalLayerSettings.AboveLine | QgsPalLayerSettings.BelowLine will allow labels above and below the line, but not on over the line.


6

I have found the answer reading the QGIS API This is the method: void QgsDataSourceURI::setKeyColumn (QString column ) this is my example: uri = QgsDataSourceURI() uri.setConnection('localhost', '5432', '-mydatabase-', '-myowner-', '-mypasswd-') uri.setDataSource('public', 'parcelas', 'the_geom') #public: schema, parcelas: the view, the_geom: spatial ...


0

You can (ab-)use the TimeManager plugin for this by making it output each frame of its animation as image. If your data has no time values, just fake them by incrementing an integer.


1

The pyuic4 command generates .py files from .ui files - not the other way round. .ui files are created with QtDesigner and define the graphical user interface of your application. As they are not directly usable with python, you have to either convert them to python files first or load them with a QUiLoader.


0

I have found the solution of the issue. I checked ui_DlgResults.py file from /usr/share/qgis/python/plugins/processing/ui/ and I have found that QWebView is not imported correctly. In the file was: from QtWebKit.QWebView import QWebView I have replace with from PyQt4.QtWebKit import QWebView and recompile the script using py_compiler. The error is gone. In ...


1

< 2.0 plugins can't run in > 2.0 unless they are upgraded. QGIS went though a API change and plugins need to change too. Here is a guide on how to convert older plugins: https://hub.qgis.org/wiki/17/Python_plugin_API_changes_from_18_to_20


2

This is a bug and not related to PyQGIS. It has been fixed for the 2.10 release Please note that gis.se is not an issue tracker and you should prefer to open a new ticket in the QGIS issue tracker.


2

Virtual Fields are saved in the project, not in the layer itself. Shapefiles do not know the concept of virtual layers, only QGIS (projects) do. If you dynamically load a layer like in the sample code you pasted, there is no project involved and hence no virtual field specification loaded. You can check with the following code that there is no Field_2: ...


1

In Options -> Network, you can type addresses in "Exclude URLs" to bypass the proxy.


1

the visibilityChanged signal of a QgsLayerTreeNode object should do the job i think. Tell me if this helps ...


1

The predicate is u'intersects'. For more than one predicate use an array like "[u'within',u'intersects']" The full command is processing.runalg('qgis:extractbylocation', inputlayer, intersectlayer, u'intersects', None) The possible arguments for the predicates are: ...


1

I recommend using the following script by Tim Sutton as a base, it worked well for me:- http://kartoza.com/how-to-create-a-qgis-pdf-report-with-a-few-lines-of-python/ Just pass an empty dict to the composition.loadFromTemplate() function instead of the 'substitution map' he uses, since you're not interested in that functionality. Also, since you asked ...


0

not sure if you're still having trouble with this, but I have indeed got a setup going of producing maps using the QGIS composer functionality via the Python API. My general strategy is:- 1) Create a shared map canvas used by the composer, and all maps etc. Code to first create the QgsComposition looks like: ms = canvas.mapSettings() comp = ...


2

Run the "Extract by location" command of the Toolbox and examine the last line of the ../.qgis2/processing/processing.log file (that records all the commands executed) ALGORITHM|Sun Jun 14 2015 17:34:39| processing.runalg("qgis:extractbylocation","/Users/Shared/result.shp","/Users/Shared/input.shp","['intersects']",None) The predicate is simply ...


1

Should have searched a bit more (...) : class showMenu(QgsMapTool): def __init__(self, iface): canvas = iface.mapCanvas() QgsMapTool.__init__(self,canvas) self.canvas = canvas def canvasPressEvent(self,e): if e.button() == Qt.RightButton: menu = QMenu() quitAction = menu.addAction("AnyAction") action = ...


1

A solution with following restrictions: QGIS will be started through a batch file (I'm on Windows). The user will be working with a predefined QGIS project (as the project will be opened through the batch file). Username and password for the PostGIS connection are be stored in the QGIS project. All PostGIS layers of the project must be in the same PostGIS ...


2

Ok, this is do able (If I understand correctly). Here is what I am thinking. Make 1 master shapefile with all the attributes. Create a csv with your list. Use python to read each line (species name) in the csv (use a for loop), use a copy function from ArcGIS or QGIS to copy the master shapefile and use the species name as the ouput name. The following code ...


3

You could use this function: def laylist(): l=[] for layer in iface.mapCanvas().layers(): item = layer.name() l.append(item) print l or this one, if you only want to get layer names printed: def laylist(): for layer in iface.mapCanvas().layers(): print layer.name() Tested in the QGIS Python console. EDIT: In ...


1

There's no direct way to set the paper orientation - you need to use QgsComposition::setPaperSize and set the page width and height as desired for your page orientation.


0

I had to go through the source code of DB Manager plugin to get to the bottom of this! I don't know why they didn't mention it in PyQGIS cookbook, but in order to write to the database another (but similar) function have to be used: QgsVectorLayerImport.importLayer(). Also notice that provider name is postgres instead of PostgreSQL O_o and no word in ...


1

Yes. Here's a python function I wrote, which does the job. The function takes as its inputs: a layer to dice up, the name for the output layer, and the maximum number of vertices for each feature. The output features then also retain their parent attributes and no features in the output layer have a greater number of vertices than the limit passed to the ...


1

You can use the term: None to discount various parameters. Additionally, adding it as an output parameter will create a memory layer for the resulting layer. Hope this helps!


0

The solution: QgsMapLayerRegistry.instance().removeMapLayer(vlayer.id()) before os.remove()


0

Perhaps not exactly what you're looking for but there's a script "Split vector layer by attribute" from the Processing Toolbox which might help: I've included the code from the script below in the unlikelyhood that you don't have it: ##[Example scripts]=group ##input=vector ##class_field=field input ##output=output file from qgis.core import * from ...


0

The OpenLayers plugin doesn't support being exported via the composer at this stage.


3

Look at Using Vector Layers: Adding and Removing Fields res = layer.dataProvider().addAttributes([QgsField("STATUT", QVariant.Int)]) layer.updateFields()


2

Bleh, answer was simple. Just had to replace what I originally wrote: CRS = QgsMapLayer.crs(fname).authid() With this: CRS = QgsVectorLayer( fname, '', 'ogr' ).crs().authid() Works a treat, hope this helps someone!


0

Try this (note the double slashes for the directory in which your shapefile resides in): layer2 = iface.addVectorLayer("H:\Mapping Stuff\ne_50m_urban_areas\\ne_50m_urban_areas.shp", "urban", "ogr")


1

The following code produces a tab delimited txt file: #Create python list createopts=["SEPARATOR=TAB"] #Use list in QgsVectorFileWriter QgsVectorFileWriter.writeAsVectorFormat(inputLayer, "D:/dstest/my_csv", "CP1250", None, "CSV", True, "", "", createopts) Thanks to @underdark for helping with this. Ian



Top 50 recent answers are included