- QGIS Python Programming Cookbook
- Joel Lawhead
- 233字
- 2021-07-23 19:48:53
Examining vector layer features
Once a vector layer is loaded, you may want to investigate the data. In this recipe, we'll load a vector point layer from a shapefile and take a look at the x and y values of the first point.
Getting ready
We'll use the same New York City Museums layer from Loading a vector layer from a file recipe in this chapter. You can download the layer from https://geospatialpython.googlecode.com/svn/NYC_MUSEUMS_GEO.zip.
Unzip that file and place the shapefile's contents in a directory named nyc
within your qgis_data
directory, within your root or home directory.
How to do it...
In this recipe, we will load the layer, get the features, grab the first feature, obtain its geometry, and take a look at the values for the first point:
- First, load the layer:
layer = QgsVectorLayer("/qgis_data/nyc/NYC_MUSEUMS_GEO.shp", "New York City Museums", "ogr")
- Next, get an iterator of the layer's features:
features = layer.getFeatures()
- Now, get the first feature from the iterator:
f = features.next()
- Then, get the feature's geometry:
g = f.geometry()
- Finally, get the point's values:
g.asPoint()
- Verify that the Python console output is similar to the following QgsPoint object:
(-74.0138,40.7038)
推薦閱讀
- Spring Cloud Alibaba核心技術(shù)與實戰(zhàn)案例
- 自然語言處理實戰(zhàn):預(yù)訓(xùn)練模型應(yīng)用及其產(chǎn)品化
- OpenCV實例精解
- Apache Spark 2 for Beginners
- Java從入門到精通(第5版)
- Cassandra Data Modeling and Analysis
- C語言程序設(shè)計
- Mastering Xamarin.Forms(Second Edition)
- RESTful Java Web Services(Second Edition)
- Xcode 6 Essentials
- App Inventor 2 Essentials
- Anaconda數(shù)據(jù)科學(xué)實戰(zhàn)
- WildFly Cookbook
- 3D Printing Designs:Design an SD Card Holder
- Building Microservices with .NET Core 2.0(Second Edition)