官术网_书友最值得收藏!

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:

  1. First, load the layer:
    layer = QgsVectorLayer("/qgis_data/nyc/NYC_MUSEUMS_GEO.shp", "New York City Museums", "ogr")
    
  2. Next, get an iterator of the layer's features:
    features = layer.getFeatures()
    
  3. Now, get the first feature from the iterator:
    f = features.next()
    
  4. Then, get the feature's geometry:
    g = f.geometry()
    
  5. Finally, get the point's values:
    g.asPoint()
    
  6. Verify that the Python console output is similar to the following QgsPoint object:
    (-74.0138,40.7038)
    

How it works...

When you access a layer's features or geometry using the previously demonstrated methods, PyQGIS returns a Python iterator. The iterator data structure allows Python to work efficiently with very large data sets without keeping the entire dataset in memory.

主站蜘蛛池模板: 盐边县| 剑阁县| 北辰区| 图们市| 师宗县| 安乡县| 辽中县| 平安县| 商水县| 肇东市| 新巴尔虎左旗| 吉安市| 万全县| 陇川县| 陈巴尔虎旗| 宁强县| 阳新县| 乌兰浩特市| 卫辉市| 老河口市| 收藏| 丹棱县| 安吉县| 柘城县| 垫江县| 河北省| 故城县| 凌海市| 寿宁县| 玉屏| 斗六市| 肃北| 聂拉木县| 乌拉特前旗| 安陆市| 凭祥市| 五华县| 隆昌县| 吉林市| 山西省| 金堂县|