concept feature in category geoprocessing

appears as: features, features, feature
Geoprocessing with Python

This is an excerpt from Manning's book Geoprocessing with Python.

You’ll learn how to work with the two main types of spatial data, vector and raster. Vector data is made up of points, lines, and polygons, while raster data is a two- or three-dimensional array of data values such as the pixels in a photograph. A dataset containing country boundaries is an example of vector data. In this case, each country is generally represented as a polygon. Datasets that use lines to represent roads or rivers, or points to show the location of weather stations, are other examples. Early primitive maps, such as those drawn on cave walls, only showed the features themselves. Later maps contained labels for features of interest such as cities or seaports; for example, the Portolan map of northwest Africa shown in figure 1.1.

And speaking of the actual data, each layer contains a collection of Feature objects that holds the geometries and their attributes. If you load vector data into a GIS, such as QGIS, and then look at the attribute table, you’ll see something similar to figure 3.7. Each row in the table corresponds to a feature, such as the feature representing Afghanistan. Each column corresponds to an attribute field, and in this case two of the attributes are SOVEREIGNT and TYPE. Although you can open data tables that don’t have any spatial information or geometries associated with the features, we’ll work with datasets that do have geometries. As you can see in figure 3.7, the geometries don’t show up in the attribute table in QGIS, although other GIS software packages, such as ArcGIS, do show a shape column in the attribute table.

Figure 3.7. An example of an attribute table shown in QGIS. Each row corresponds to a feature, and each column is an attribute field.

Sometimes you don’t need every feature, so you have no reason to iterate through all of them as you’ve done so far. One powerful method of limiting features to a subset is to select them by attribute value or spatial extent, and you’ll do that in chapter 5. Another way is to look at features with specific offsets, also called feature IDs (FIDs). The offset is the position that the feature is at in the dataset, starting with zero. It depends entirely on the position of the feature in the file and has nothing to do with the sort order in memory. For example, if you open the ne_50m_populated_places shapefile in QGIS and look at the attribute table, it would show Bombo as the first record in the table, as in figure 3.11A. See the numbers in the left-most column? Those are the offset values. Now try sorting the table by name by clicking on the NAME column header, as shown in figure 3.11B. Now the first record shown in the table is the one for Abakan, but it has an offset of 346. As you can see, that left-most column isn’t a row number like you see in spreadsheets, where the row numbers are always in the right order no matter how you sort the data. These numbers represent the order in the file instead.

Figure 3.11. The attribute table for the ne_50m_populated_places shapefile. Table A shows the native sort order, with the FIDs in order. Table B has been sorted by city name, and the FIDs are no longer ordered sequentially.

If you know the offset of the feature you want, you can ask for that feature by FID. To get the feature for Vatican City, you use GetFeature(7).

You can also get the total number of features with GetFeatureCount, so you could grab the last feature in the layer like this:

>>> num_features = lyr.GetFeatureCount()
>>> last_feature = lyr.GetFeature(num_features - 1)
>>> last_feature.NAME
'Hong Kong'

You have to subtract one from the total number of features because the first index is zero. If you had tried to get the feature at index num_features, you’d have gotten an error message saying that the feature ID was out of the available range. This snippet also shows an alternate way of retrieving an attribute value from a feature, instead of using GetField, but it only works if you know the names beforehand so that you can hardcode them into your script.

3.6.2. Adding, updating, and deleting features

Adding new features to existing layers is exactly the same as adding them to brand-new layers. Create an empty feature based on the layer definition, populate it, and insert it into the layer. Updating features is much the same, except you work with features that already exist in the layer instead of blank ones. Find the feature you want to edit, make the desired changes, and then update the information in the layer by passing the updated feature to SetFeature instead of CreateFeature. For example, you could do something like this to add a unique ID value to each feature in a layer:

lyr.CreateField(ogr.FieldDefn('ID', ogr.OFTInteger))
n = 1
for feat in lyr:
    feat.SetField('ID', n)
    lyr.SetFeature(feat)
    n += 1

First you add an ID field, and then you iterate through the features and set the ID equal to the value of the n variable. Because you increment n each time through the loop, each feature has a unique ID value. Last, you update the feature in the layer by passing it to SetFeature.

Deleting features is even easier. All you need to know is the FID of the feature you want to get rid of. If you don’t know that number off the top of your head, or through another means, you can get it from the feature itself, like this:

for feat in lyr:
    if feat.GetField('City_Name') == 'Seattle':
        lyr.DeleteFeature(feat.GetFID())

For each feature in the layer, you check to see if its 'City_Name' attribute is equal to 'Seattle', and if it is, you retrieve the FID from the feature itself and then pass that number to DeleteFeature.

Certain formats don’t completely kill the feature at this point, however. You may not see it, but sometimes the feature has only been marked for deletion instead of totally thrown out, so it’s still lurking in the shadows. Because of this, you won’t see any other features get assigned that FID, and it also means that if you’ve deleted many features, there may be a lot of needlessly used space in your file. See figure 3.19 for a simple example. Deleting these features will reclaim this space. If you have much experience with relational databases, you should be familiar with this idea. It’s similar to running Compact and Repair on a Microsoft Access database or using VACUUM on a PostgreSQL database.