Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shapefile which renders a map.

I want to calculate and preferably store length of each feature in the shapefile either using QGis or through programmatic means.

What is the best way to get this task done?

share|improve this question
up vote 4 down vote accepted

Create a Real Number field in the shapefile. From Attribute table, open 'Field Calculator'. From the functions list, expand 'Geometry' section. selection length.

The expression would look


Check update existing field at the top of field calculator and select the newly created double field name.

share|improve this answer
What is the unit of length I get there? I want it to be in km. – Sibi Nov 20 '12 at 6:21
it will be in the units of the coordinate system of your layer. It could be meter/feet/degrees depending on it. you could do a conversion depending on your coordinate system.If the coordinate system is Geogrpahic, it would be best to convert to a projected coordinate system for linear measurement. – vinayan Nov 20 '12 at 8:19
So to get km from metres, multiply $length x 1000 in the calculator and that will be the output into the shapefile field. A projected coordinate system may be referred to as UTM or GRID. – Willy Nov 20 '12 at 11:20
@Willy The map is in the Geographic coordinate system. So i don't exactly know what is the unit in which geographic coordinate system renders it. It's in EPSG:4326. – Sibi Nov 20 '12 at 11:25
@Sibi - says that it is in Degrees – vinayan Nov 20 '12 at 12:01

Using the arc python module (arcpy) is an alternative. To run the code it is necessary with an ArcInfo license.

Fairly straight forward. The script establishes a new field and populates it with a field value in meters:

import arcpy

my_coastline = "coastal_Segments.shp"

#Add field to support lengt calculation
arcpy.AddField_management(my_coastline, "length",  "FLOAT", "12", "", "", "i_perim",   "NULLABLE", "REQUIRED")

#Calculate the length of all segment in the shapefile
arcpy.CalculateField_management(my_coastline, 'length', '!shape.length@meters!', 'PYTHON')

Not sure but I assume the Shapely open source initiative could be an alternative.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.