- QGIS Python Programming Cookbook
- Joel Lawhead
- 267字
- 2021-07-23 19:48:49
Using the QGIS Python console for interactive control
The QGIS Python console allows you to interactively control QGIS. You can test out ideas or just do some quick automation. The console is the simplest way to use the QGIS Python API.
How to do it…
In the following steps, we'll open the QGIS Python console, create a vector layer in memory, and display it on the map:
- Start QGIS.
- From the Plugins menu, select Python Console.
- The following code will create a point on the map canvas:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , 'memory') pr = layer.dataProvider() pt = QgsFeature() point1 = QgsPoint(20,20) pt.setGeometry(QgsGeometry.fromPoint(point1)) pr.addFeatures([pt]) layer.updateExtents() QgsMapLayerRegistry.instance().addMapLayers([layer])
How it works…
This example uses a memory
layer to avoid interacting with any data on disk or a network to keep things simple. Notice that when we declare the layer type, we add the parameter for the Coordinate Reference System (CRS) as EPSG:4326. Without this declaration, QGIS will prompt you to choose one. There are three parts or levels of abstraction to create even a single point on the map canvas, as shown here:
- First, create a layer that is of the type geometry. Next, set up a data provider to accept the data source.
- Then, create a generic feature object, followed by the point geometry.
- Next, stack the objects together and add them to the map.
The layer type is memory
, meaning that you can define the geometry and the attributes inline in the code rather than in an external data source. In this recipe, we just define the geometry and skip the defining of any attributes.
- 小程序?qū)崙?zhàn)視頻課:微信小程序開發(fā)全案精講
- 零起步玩轉(zhuǎn)掌控板與Mind+
- LabVIEW2018中文版 虛擬儀器程序設(shè)計(jì)自學(xué)手冊
- Java完全自學(xué)教程
- R語言編程指南
- Python自然語言處理(微課版)
- C++程序設(shè)計(jì)基礎(chǔ)教程
- Mastering JBoss Enterprise Application Platform 7
- SQL Server與JSP動(dòng)態(tài)網(wǎng)站開發(fā)
- Citrix XenServer企業(yè)運(yùn)維實(shí)戰(zhàn)
- Hands-On JavaScript for Python Developers
- Cocos2d-x by Example:Beginner's Guide(Second Edition)
- C++ Application Development with Code:Blocks
- 虛擬現(xiàn)實(shí)建模與編程(SketchUp+OSG開發(fā)技術(shù))
- Learning Alfresco Web Scripts