[FWTools] Problem with memory allocation using FWTools 2.0.4

"SAEZ Laurent - CETE Méditerr./DI/ETER" Laurent.Saez at equipement.gouv.fr
Wed Jan 16 05:08:32 EST 2008


Thanks Frank for your quick response,

I have 100 shape files produced with Arc Analyst by a friend of mine.
I have to do some changes :
- rename the field GRIDCODE to CODE_PENTE,
- add a new field called PENTE whose value depends on CODE_PENTE.

I'm not at all a programmer.
	
I have been guided by your file vec_tr.py.
Thanks for your help !

This is my script :

#!/usr/bin/env python
###############################################################################
# $Id$
#
# Projet :  Creation des classes de pente (produit derive du MNT de la BD TOPO)
# Fonction :  Copie un shp en creant un champ CODE_PENTE (copie de GRIDCODE)
#             et cree un champ PENTE contenant la classe correspondant au CODE_PENTE.
# Auteur :   Laurent SAEZ (merci a Franck Warmerdam)
#
###############################################################################
# Copyright (c) 2008, Laurent SAEZ
# 
# 
###############################################################################
# 
#  $Log$
#  Revision 1  07/01/2008 17:43  lsaez
#  Revision 2  16/01/2008 10:00  lsaez
#

try:
    from osgeo import osr
    from osgeo import ogr
    from osgeo import gdal
except ImportError:
    import osr
    import ogr
    import gdal
import string
import sys
import os

#############################################################################
def Usage():
    print 'Usage: pente.py infile outfile'
    print 'Exemple : pente.py C:\pente10.shp D:\pente_fin\PENTE-10-L2E.SHP'
    print
    sys.exit(1)

#############################################################################
# Argument processing.

infile = None
outfile = None

i = 1
while i < len(sys.argv):
    arg = sys.argv[i]

    if infile is None:
        infile = arg

    elif outfile is None:
        outfile = arg

    else:
        Usage()

    i = i + 1

if outfile is None:
    Usage()

#############################################################################
# Ouverture du fichier

in_ds = ogr.Open( infile, update = 0 )

in_layer = in_ds.GetLayer( 0 )

in_defn = in_layer.GetLayerDefn()

#############################################################################
# Creation d'une copie

shp_driver = ogr.GetDriverByName( 'ESRI Shapefile' )
# On supprime un peu brutalement la copie si le fichier existe
if os.path.isfile(outfile) is True:
    shp_driver.DeleteDataSource( outfile )
# On cree la copie
shp_ds = shp_driver.CreateDataSource( outfile )
# Meme nom, meme type de geometrie et meme systeme de coordonnees
shp_layer = shp_ds.CreateLayer( in_defn.GetName(),
                                geom_type = in_defn.GetGeomType(),
                                srs = in_layer.GetSpatialRef() )
# On compte le nombre de champs
in_field_count = in_defn.GetFieldCount()
# On copie le champ
for fld_index in range(in_field_count):
    src_fd = in_defn.GetFieldDefn( fld_index )
    # On cree un champ PENTE qui a les memes caracteristiques que
    # le champ GRIDCODE 
    if src_fd.GetName()=="GRIDCODE":
        fd1 = ogr.FieldDefn("CODE_PENTE", src_fd.GetType())
        fd1.SetWidth(src_fd.GetWidth())
        fd1.SetPrecision(src_fd.GetPrecision())
        shp_layer.CreateField(fd1)
# On cree maintenant un champ CLASSE_PT
# Le type OFTString correspond a la valeur 4
fd2 = ogr.FieldDefn("PENTE", 4)
fd2.SetWidth(13)
fd2.SetPrecision(0)
shp_layer.CreateField(fd2)
#############################################################################
# On passe en revue tous les objets dans la couche source.  
gdal.TermProgress(0.0)
gdal.TermProgress(1.0 / in_layer.GetFeatureCount())
in_feat = in_layer.GetNextFeature()
i=-1
while in_feat is not None:
    geom = in_feat.GetGeometryRef().Clone()
    if geom is not None:
        i=i+1
        out_feat = ogr.Feature( feature_def = shp_layer.GetLayerDefn() )
        out_feat.SetFrom( in_feat )
        out_feat.SetGeometryDirectly( geom )
#=====================================================
# Il faut recuperer les valeurs des attributs et les
# copier dans les nouveaux objets crees.
        in_field = in_feat.GetField( 'GRIDCODE' )
        out_feat.SetField("CODE_PENTE", in_field)
        if in_field==1:
            out_feat.SetField("PENTE", "0 - 5 %")
        elif in_field==2:
            out_feat.SetField("PENTE", "5 - 10 %")
        elif in_field==3:
            out_feat.SetField("PENTE", "10 - 15 %")
        elif in_field==4:
            out_feat.SetField("PENTE", "15 - 20 %")
        elif in_field==5:
            out_feat.SetField("PENTE", "20 - 25 %")
        elif in_field==6:
            out_feat.SetField("PENTE", "25 - 30 %")
        elif in_field==7:
            out_feat.SetField("PENTE", "30 - 40 %")
        elif in_field==8:
            out_feat.SetField("PENTE", "40 - 60 %")
        elif in_field==9:
            out_feat.SetField("PENTE", "60 - 80 %")
        elif in_field==10:
            out_feat.SetField("PENTE", "80 - 100 %")
        elif in_field==11:
            out_feat.SetField("PENTE", "Plus de 100 %")  
        shp_layer.CreateFeature( out_feat )
        out_feat.Destroy()
#=====================================================
    in_feat.Destroy()
    in_feat = in_layer.GetNextFeature()
    gdal.TermProgress(float(i + 1) / (in_layer.GetFeatureCount() - 1))

#############################################################################
# On nettoie
print 'Nettoyage de la memoire en cours... Veuillez patienter...'
shp_ds.Destroy()
in_ds.Destroy()
print 'Traitement termine.'

-----Message d'origine-----
De : fwtools-bounces at lists.maptools.org
[mailto:fwtools-bounces at lists.maptools.org]De la part de Frank Warmerdam
Envoyé : mercredi 16 janvier 2008 10:12
À : "SAEZ Laurent - CETE Méditerr./DI/ETER"
Cc : fwtools at lists.maptools.org
Objet : Re: [FWTools] Problem with memory allocation using FWTools 2.0.4


SAEZ Laurent - CETE Méditerr./DI/ETER wrote:
> Hi,
> 
> I've written some lines code in Python.
> I have tried with the same file on the same PC.
> With FWTools 2.0.3 it works very well.
> With FWTools 2.0.4 I have some troubles.
> Example :
> ERROR 2:CPLMalloc(): Out of memory allocating 1845637 bytes.
> 
> Is there a big change with the memory management on FWTools 2.0.4 ?

Laurent,

It is not at all obvious why this might be happening.  I think you will need
to provide more context.  What is your script doing?

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

_______________________________________________
FWTools mailing list
FWTools at lists.maptools.org
http://lists.maptools.org/mailman/listinfo/fwtools
http://fwtools.maptools.org/


More information about the FWTools mailing list