Function Returning None

Asked

Viewed 1,096 times

1

I am trying to calculate the area of a file using Python with the following function:

from __future__ import division
import ogr
import osr

def transform_geometries(datasource, src_epsg, dst_espg):  
    src_srs = osr.SpatialReference()
    src_srs.ImportFromEPSG(src_epsg)
    dst_srs = osr.SpatialReference()
    dst_srs.ImportFromEPSG(dst_espg)
    transformation = osr.CoordinateTransformation(src_srs, dst_srs)
    layer = datasource.GetLayerByIndex(0)
    geoms = []
    layer.ResetReading()
    for feature in layer:
        geom = feature.GetGeometryRef().Clone()
        geom.Transform(transformation)
        geoms.append(geom)
    return geoms

def calcuate_areas(geometries, unity='km2'):
    conversion_factor = {
        'sqmi': 2589988.11,
        'km2': 1000000,
        'm': 1}
    if unity not in conversion_factor:
        raise ValueError("This unity is not defined: {}".format(unity))

    areas = []
    for geom in geometries:
        area = geom.Area()
        areas.append(area / conversion_factor[unity])

transformed_geoms = transform_geometries(datasource, 4326, 3395)
calculated_areas = calcuate_areas(transformed_geoms, unity='sqmi')
print(calculated_areas)

NONE

1 answer

1


In the method calcuate_areas, failed to return the result, areas:

# ...
for geom in geometries:
    area = geom.Area()
    areas.append(area / conversion_factor[unity])

return areas
  • 1

    A silly mistake, thanks for your help :)

  • @Guilhermelima of nothing, happens. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.