I have a Linestrings' shapefile to which I added a column named "length". I want to set the length of each row (geometry) in that column. Here's my code :
import geopandas as gpd from shapely.geometry import * from shapely.ops import transform from functools import partial import pyproj def addLengthField(shapefile, output): shp = gpd.GeoDataFrame.from_file(shapefile) shp['length'] = '' shp.to_file(output) addLengthField('./Shapefiles/curbs.shp', './Outputs/curbsWithLength.shp') def setLengthValue(shapefile, output): shp = gpd.GeoDataFrame.from_file(shapefile) for index, row in shp.iterrows(): geom = shape(row['geometry']) # since the result of geom.length is not in Meters, I wanted to make a transformation # Geometry transform partial function based on pyproj.transform project = partial( pyproj.transform, pyproj.Proj(init='EPSG:4152'), pyproj.Proj(init='EPSG:4152')) projectedCurbs = transform(project, geom) row['length'] = int(projectedCurbs.length) print row['length'] # length values are well displayed # Save the final output in another shapefile shp.to_file(output) setLengthValue('./Outputs/curbsWithLength.shp','.Outputs/finaloutput.shp')
When I run the script the printed values are well displayed but when I open the output shapefile in QGIS the length's column is empty! Any ideas on what could be wrong?