## Pages

### Tutorial: Calculating Line Lengths and Statistics

Here I will show you how to calculate length of each feature in a line shapefile. This is a very useful operation when working with line data and Quantum GIS has a built-in feature that help you easily calculate line length. In the process, I will also show you how to re-project the vector layer to a different projection, enable on-the-fly CRS transformation and also calculate basic statistics on a particular data-field.

The example here contains a line shapefile of railroads in north america. These are from Natural Earth public domain dataset. The question we will try to answer is, ‘what is the total length of railroads in United States?’.
1. Open QGIS. Click on Settings → Project Properties.

1. In the Coordinate Reference System tab, check the box named Enable on the fly CRS transformation. Enabling this will ensure that regardless of the projection of the dataset we add in QGIS, it will be displayed after being transformed to a particular projection. From the list of Coordinate Reference Systems, choose WGS84 as the projection for this project.

1. Right click on the layer and select Open Attribute Table. Click on ‘Advanced Search’ button on bottom-right.

1. In the search query builder, type in the query as SOV_A3=’USA’ and click OK. This will select all the features that belong to USA.
1. The selected features will be displayed in yellow. Right click the layer and select Save selection as...

1. Name the output file as 10m_railroads_usa.shp.

1. Add this vector layer to the project.

1. Now we will export this layer to a different projection. Since we are interested in calculating length, let’s select an equidistance projection. Click on Vector → Data Management Tools → Export to a new projection.

1. Under output spatial reference system, select Use predefined spatial reference system and click Choose. From the list, select World Equidistance Cylindrical (Sphere) and click OK. Name the output layer as 10m_railroads_usa_reprojected.

1. Once the reprojection is complete, click Yes on the prompt asking whether you would like to add the new layer to TOC.

1. Right click on the new layer and open the attribute table. Now we will calculate the lengths of individual features. Click on Toggle Editing at bottom of the attribute table. Then click on Field Calculator.

1. Type length_km in the Output Field Name. In the Field Calculator Expression type in \$length / 1000 and click OK. You can click on the Length button to populate the \$length parameter. Since the layer is projected in units of ‘metre’, we divide the length by 1000 to get the result in kilometres.

1. Click on Toggle Editing again and click Yes on the prompt to save the results. Close the attribute table.

1. Click on Vector → Analysis Tools → Basic Statistics.

1. Select the field length_km and click OK. The sum of all feature lengths will be displayed as Sum. This is the answer we are looking for. This answer will vary slightly if a different projection is chosen. If we compare this number to the answer given by Wolphram Alpha , it is significantly different. This is likely due to the fact that many smaller railroads might be missing from our dataset. Nevertheless, the important point here is to always cross-check your GIS Analysis and never trust the numbers blindly :)

Note: In practice, line lengths for roads and other linear features are measured on the ground and provided as attributes to the dataset. This method works in absence of such attribute and as an approximation of actual line lengths.