- QGIS Python Programming Cookbook
- Joel Lawhead
- 369字
- 2021-07-23 19:48:56
Adding a point feature to a vector layer
This recipe performs the simplest possible edit to a vector layer instantiated from a shapefile. We will add a point to an existing point layer.
Getting ready
For this recipe, download the zipped shapefile from https://geospatialpython.googlecode.com/svn/NYC_MUSEUMS_GEO.zip.
Extract the .shp
, .shx
, and .dbf
files to the /qgis_data/nyc
directory.
How to do it...
We will load the vector layer from the shapefile, create a new geometry object as a point, create a new feature, set the geometry, and add it to the layer's data provider. Finally, we will update the extent of the layer to make sure that the bounding box of the layer encapsulates the new point:
- Start QGIS.
- From the Plugins menu, select Python Console.
- First, load the layer:
vectorLyr = QgsVectorLayer('/qgis_data/nyc/NYC_MUSEUMS_GEO.shp', 'Museums' , "ogr")
- Now, will access the layer's data provider:
vpr = vectorLyr.dataProvider()
- Next, create a new point using the
QgsGeometry
object:pnt = QgsGeometry.fromPoint(QgsPoint(-74.80,40.549))
- Now, will create a new
QgsFeature
object to house the geometry:f = QgsFeature()
- Next, set the geometry of the feature using our point:
f.setGeometry(pnt)
- Then, place the features into the layer's feature list:
vpr.addFeatures([f])
- Finally, update the layer's extent to complete the addition:
vectorLyr.updateExtents()
How it works...
PyQGIS abstracts the points within a layer into four levels. At the lowest level is the QgsPoint
object, which contains nothing more than the coordinates of the point. This object is added to an abstract QgsGeometry
object. This object becomes the geometric part of a QgsFeature
object, which also has the ability to store and manage attributes. All the features are managed by the QgsDataProvider
object. The data provider manages the geospatial aspect of a layer to separate that aspect from styling and other presentation-related portions. QGIS has another editing approach in Python, which is called an editing buffer. When you use an editing buffer, the changes can be displayed, but they are not permanent until you commit them. The most common use case for this editing method is in GUI applications where the user may decide to roll back the changes by cancelling the editing session. The PyQGIS Developer Cookbook has an example of using and editing buffers in Python, and is available at http://docs.qgis.org/2.6/en/docs/pyqgis_developer_cookbook/vector.html.
- AngularJS Testing Cookbook
- Cocos2d-x游戲開發(fā):手把手教你Lua語(yǔ)言的編程方法
- C#完全自學(xué)教程
- Production Ready OpenStack:Recipes for Successful Environments
- 精通軟件性能測(cè)試與LoadRunner實(shí)戰(zhàn)(第2版)
- 你必須知道的204個(gè)Visual C++開發(fā)問(wèn)題
- 組態(tài)軟件技術(shù)與應(yīng)用
- Mastering openFrameworks:Creative Coding Demystified
- Service Mesh實(shí)戰(zhàn):基于Linkerd和Kubernetes的微服務(wù)實(shí)踐
- Statistical Application Development with R and Python(Second Edition)
- HTML5權(quán)威指南
- 運(yùn)維前線:一線運(yùn)維專家的運(yùn)維方法、技巧與實(shí)踐
- 大學(xué)計(jì)算機(jī)基礎(chǔ)實(shí)驗(yàn)指導(dǎo)
- Learning iOS Security
- jQuery for Designers Beginner's Guide Second Edition