Tag Info

Hot answers tagged

14

You could use PyQGIS for that (not sure is the best solution for it, though). Select (or activate) the layer in the QGIS ToC, open the QGIS Python console, and copy this code snippet: prefix = "'" layer = iface.activeLayer() attr = layer.rendererV2().classAttribute() attrColor = 'color' # Name of the field to store colors fieldIndex = ...


3

Why not data define the image path? Next to the image filename is a button which allows you to link the path to an attribute in your data (or set via an expression).


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

As Luigi suggests, you can have a look at the API documentation, specifically to QgsVectorFileWriter::writeAsVectorFormat, and realize you're just missing one parameter (from the docs): bool onlySelected = false, It says that the parameter onlySelected is of type boolean and is false by default. This parameter is right after the driver name. So, calling ...


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.


3

You could loop through the the Layers panel to check if the layer exists or not by the name before assigning: layers = QgsMapLayerRegistry.instance().mapLayers().values() foundLayer = False for layer in layers: if layer.name() == 'my_layer': inputLayer = QgsMapLayerRegistry.instance().mapLayersByName("my_layer")[0] foundLayer = True if ...


2

A point inside a polygon canvas = qgis.utils.iface.mapCanvas() aLayer = canvas.layer(0) # or code to select the layer of interest for f in aLayer.getFeatures(): geom = f.geometry() p = geom.pointOnSurface() print p.asPoint() Get Inner Rings canvas = qgis.utils.iface.mapCanvas() aLayer = canvas.layer(0) # or code to select the layer of ...


2

Add the second line in my code: layer = QgsVectorLayer("LineString", "points", "memory") QgsMapLayerRegistry.instance().addMapLayer(layer) pr = layer.dataProvider() pt = QgsFeature() point1 = QgsPoint(0,0) point2 = QgsPoint(100,100) pt.setGeometry(QgsGeometry.fromPolyline([point1,point2])) pr.addFeatures([pt]) layer.updateExtents() The result is in the ...


2

Try simply: layer = iface.addVectorLayer("/path/to/shapefile/file.shp", "layer_name_you_like", "ogr") Here you have a nice guide for PyQGIS: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html


2

I think some of the tools you are looking for can be found in a single plugin - the Processing plugin. Importing this plugin from PyQGIS would allow you to call various tools (I am not an ArcGIS user so I am judging the tools you mentioned by their name and a quick search): ArcGIS QGIS arcpy.AddField_management ...


2

Reason When you create a symbol it has ownership of its layers. When you add a layer to an existing symbol, it takes ownership of the added layer. Ownership means it is free to delete it whenever it wants. In your example both symbols assume ownership of symbol_layer2 (symbol2 because it created symbol_layer2, symbol1 because symbol_layer2 was added to ...


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


2

QGIS updated to the SIP API V2 between 1.8 and 2.0. See: https://hub.qgis.org/wiki/17/Python_plugin_API_changes_from_18_to_20#Replace-QStringList-with-list It will transparently convert some types (like QString or QList) between python and Qt types. So you can just use a python list of str or unicode objects and pass it to the pyqgis and pyqt API wherever ...


1

If you want to use a solution with processing as in How to merge shape files with attributes from a python script in QGIS? (without in the solution of gcarillo): 1) The easiest solution is to is to run the wanted algorithm from the toolbox and look at the /.../.qgis2/processing/processing.log file. The last lines gives the solution: ALGORITHM|Mon Jul 27 ...


1

You can join your two layers in this way: # Get input and target layers targetLyr = iface.mapCanvas().layers()[1] # Second layer from the ToC inLyr = iface.mapCanvas().layers()[0] # First layer from the ToC # Set properties for the join targetField='myTargetField' inField='myInputField' joinObject = QgsVectorJoinInfo() joinObject.joinLayerId = inLyr.id() ...


1

To avoid "layer is not valid" errors you should carefully set your QGIS prefix path. The prefix path must be set to let QGIS know where to find resources such as data providers and the reference system database. If you look at your QGIS installation, you should be able to access lib/qgis/plugins on Unix (or just plugins on Windows), relative to the prefix ...


1

You can get the attributes toolbar and iterate through its actions (buttons) until you get the "deselect" one. Then you can trigger it. Try this in your QGIS Python console: for a in iface.attributesToolBar().actions(): if a.objectName() == 'mActionDeselectAll': a.trigger() break


1

To be honest, a python script (if you are familiar with Python) would be the most efficient method of creating your polygons in your use-case. However, if you are not familiar with scripting, then there are a variety of tools that you should investigate in QGIS (e.g. Vector->Research Tools->Vector Grid). You will have to use a crazy combination of ...


1

Thanks to Nathan Woodrows blog, and some API doc reading i figured method using QgsMapRenderer is deprecated. (Seems that pyqgis cookbook is often way out of date) So, instead, i used QgsMapSettings and QgsMapRendererSequentialJob classes. I got settings from a canvas via canvas.mapSettings() method. Then the rendering procedure goes like this: job = ...


1

Just as a remark: QgisInterface is an abstract class and has virtual functions so it may not be instantiated, i.e. it defines an interface and not a complete implementation. But you can use QgisInterface class within a standalone application where the iface class is not available. There you can reimplement and override the main functions of the class. An ...


1

If you look at the source code for the context manager qgisapp: https://github.com/qgis/QGIS/blob/master/python/core/contextmanagers.py you'll see it is just a neat wrapper for running code inside a QgsApplication context. Following what Nathan W does with his Qgis python is usually a good idea, so I'd go with this.


1

You can move cursor like that: from PyQt4.QtGui import * from PyQt4.QtCore import * cursor = QCursor() cursor.setPos(100, 200) It moves your cursor to point 100,200 in your screen. If you want to get coordinate of a point on the map canvas, you need to translate them: # coordinates of point on map canvas point = QPoint(100,100) # translate widget ...


1

You need to set your map to use the atlas feature's extent, using QgsComposerMap.setAtlasScalingMode, eg myAtlasMap.setAtlasScalingMode( QgsComposerMap.Auto ) Place this line after you've setup your atlas, but before the beginRender call.


1

Never used the QgsGeometryAnalyzer() method before (I should take a look at this) but you could use the following code which I think should do what your code does (tested this on Python Console in QGIS 2.8.2): import processing, os, glob result_path = "C:\Temp\Dissolved\\" os.chdir("C:\Temp\\") for fname in glob.glob("*.shp"): name= ...


1

SQL query is wrong, it should be: result = dataSource.ExecuteSQL("select * from roi_classMacro WHERE C_info = 'g19' or C_info = 'g22' or C_info = 'g23'") For attribute filter try: mylayer.SetAttributeFilter("C_info = 'g19' or C_info = 'g22' or C_info = 'g23'")


1

Your version of QGIS (2.11 = QGIS dev) use a new geometry class (QGIS dev: New geometry classes merged and many other posts in this mailing list as Questions regarding new geometry engine, QGIS QgsGeometry.fromWkt not accepting polygons with Z values? , Potentially serious performance regression in new geometry - should 2.10 be delayed?, ...) What is the ...


1

In Windows QGIS has its own Python version. By default if you instal libraries (ex. pip install name_of_library) it will be installed only in your pure Python version not for QGIS. Here is a guide, I think it may be helpful: http://quantumofgis.blogspot.be/2014/11/qgis-standalone-and-python-modules.html


1

You can listen to the extent changed event and reset it back if it goes outside of the bounds: def extentsChanged(): extent = iface.mapCanvas().extent() outofbounds = # Do your check if outofbounds: iface.mapCanvas().setExtent(newextent) iface.mapCanvas().extentsChanged.connect(extentsChanged)


1

Thanks to promting by Joseph I had a look at the docs for the application class reference. In order to utilise functions from qgis in your script you need to add a qgis application class reference object. app = QgsApplication([],False, None) after the QgsApplication.initQgis() line and app.exit() At the end. The docs describe the parameters but ...


1

What about the QgsFeatureRequest.NoGeometry flag? all_matrix=[] request = QgsFeatureRequest() request.setFlags(QgsFeatureRequest.NoGeometry) for f in matrix.getFeatures(request): a=feat.attributes()[origin] b=feat.attributes()[destination] c=feat.attributes()[flow] all_matrix.append([a,b,c]) matrix=np.array(all_matrix)



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