[Gdal-dev] Get Line Length

Frank Warmerdam warmerdam at pobox.com
Thu Oct 26 12:30:34 EDT 2006


Rob McCulley wrote:
> Hi All,
> 
>  
> 
> I’m trying to write a script that will calculate the total length of a 
> polyline shapefile using ogr in python.  I see in the ogr documentation 
> that both the OGRCurve and OGRLineString class support the method 
> get_Length().  The problem I’m having is that I can’t figure out how to 
> get a linestring or curve object.  Here  is what I have so far:
> 
>  
> 
> shp = ogr.Open(gpsfilelocation)
> 
> lyr = shp.GetLayer()
> 
> totalLength = 0
> 
> for i in range(lyr.GetFeatureCount()):
> 
>             ftr = lyr.GetFeature(i)
> 
>             geom = lyr.GetGeometryRef()
> 
>             length = ?
> 
>             totalLength = totalLength + length
> 
>  
> 
> At this point I could iterate through the points of the geometry and 
> calculate the length of each segment, but using a get_Length() function 
> seems much more elegant.

Rob,

I'm afraid that get_Length() is not currently exposed to python.

The python (and other mapscript bindings and OGR C API) arranged
such that you work only with OGRGeometry handles - you can't turn
these into specific geometry classes though underneath they are
of course of a particular class.

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org




More information about the Gdal-dev mailing list