Tag Info

Hot answers tagged

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


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


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


3

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


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


3

Definitely that's not an obvious way of loading a WMS. This is what have worked for me (QGIS v.2.8.1): urlWithParams = "url=http://maps.itu.edu.tr:8082/geoserver/sf/wms&format=image/png&layers=sfdem&styles=&crs=EPSG:26713" rlayer = QgsRasterLayer(urlWithParams, 'DEM', 'wms') rlayer.isValid() # Returns True this time ...


3

SimpleSVG is a Python plugin https://plugins.qgis.org/plugins/simplesvg/ --> using PyQGIS! Therefore its source code should be the perfect example of how to export SVG from QGIS.


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


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

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


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


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)


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


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


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

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


2

If you want to replicate the status bar shown in the image, do this: Add a QMainWindow in Qt-Designer. A QStatusBar is added by default. Add widgets (2 QLabel) to the status bar in your Python class: self.lblXY = QLabel() self.lblXY.setFrameStyle( QFrame.Box ) self.lblXY.setMinimumWidth( 170 ) self.lblXY.setAlignment( Qt.AlignCenter ) ...


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

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

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


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.


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

< 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


1

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


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.


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

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



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