2010-11-18 10 views
6

Je cherche un moyen de prendre un fichier Shape ESRI existant avec un ensemble de 200 pays. Chaque entité géographique a un attribut "NAME". Mon objectif est de créer un script Python qui ajoute un attribut supplémentaire arbitraire (pour l'instant), par exemple, "POPULATION".Ajout d'attributs de fonction personnalisés à ESRI Shapefile avec Python

Bien sûr, j'ai installé les modules OSGeo et GeoDjango. Je suis aussi loin que:

from osgeo import ogr 

infile = ogr.Open('sample.shp', 1) #'sample.shp' is a pre-existing ESRI shapefile described above 
inlyr = infile.GetLayerByIndex(0) 

Suis-je manque une fonction OGR qui me permettra d'insérer des champs dans un attribut Feature Shapefile existant?

Répondre

5

Pour ajouter un champ, vous devez créer un OGRFieldDefn puis appelez inlyr.CreateField

fieldDefn = ogr.FieldDefn('POPULATION', ogr.OFTReal) 
fieldDefn.SetWidth(14) 
fieldDefn.SetPrecision(6) 
inlyr.CreateField(fieldDefn) 
+0

Merci beaucoup. – mattdeboard

+0

Ensuite, après cela dois-je appeler un Destroy() pour écrire les données? – mattdeboard

+2

Pour fermer le fichier, il est recommandé d'utiliser 'infile = None' –

Questions connexes