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?
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
$length
Check update existing field at the top of field calculator and select the newly created double field name.
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.