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
A silly mistake, thanks for your help :)
– Guilherme Lima
@Guilhermelima of nothing, happens. :)
– stderr