Programmatically populating shapefile field with polygon areas in OGR?Need help assigning values to field...

Can I legally make a website about boycotting a certain company?

STM32 PWM problem

What if you do not believe in the project benefits?

Why is quixotic not Quixotic (a proper adjective)?

A cancellation property for permutations

Sauna: Wood does not feel so hot

How can I make my enemies feel real and make combat more engaging?

Are all power cords made equal?

What does an unprocessed RAW file look like?

TikZtree with asymmetric siblings

How do I handle a blinded enemy which wants to attack someone it's sure is there?

Ramanujan's radical and how we define an infinite nested radical

How can a kingdom keep the secret of a missing monarch from the public?

How bad is a Computer Science course that doesn't teach Design Patterns?

Why Is Image Exporting At Larger Dimensions Than In Illustrator File?

Does the Holy Ark weigh 4 tons?

How to modify 'inter arma enim silent leges' to mean 'in a time of crisis, the law falls silent'?

How can changes in personality/values of a person who turned into a vampire be explained?

Can a planet be tidally unlocked?

Why is Shelob considered evil?

How many copper coins fit inside a cubic foot?

Aliased pipeline using head and cut

Is Screenshot Time-tracking Common?

Using font-relative distances in tikzpictures



Programmatically populating shapefile field with polygon areas in OGR?


Need help assigning values to field using OGR in pythonPopulating new field in shapefile with values from list?Getting Field value for a given coordinate out of ESRI Shapefile using GDAL/OGRNeed help assigning values to field using OGR in pythonOGR - Weird GeomType -2147483645 on polygon shapefilePython Band.SetNoDataValue(), sets all bands at oncePolygon overlaps on “Merge Shapefile”Intersect polyline and polygon with ogrWrite coordinates to new shp file columns with OGRExtracting coordinates from multipoint geometries?OGR multipolygon with holes modelPopulating new field in shapefile with values from list?













6















I'm trying to programmatically populate a shp field with polygon areas:



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )

ds = None


But I get an empty values in the field...










share|improve this question

























  • Can you share a dummy shape file?

    – R.K.
    Sep 28 '12 at 15:52











  • how do I share it? email?

    – vascobnunes
    Sep 28 '12 at 15:58











  • dropbox.com/sh/b5uyzqf7oowsv8e/lL9bHRHTCT

    – vascobnunes
    Sep 28 '12 at 16:00
















6















I'm trying to programmatically populate a shp field with polygon areas:



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )

ds = None


But I get an empty values in the field...










share|improve this question

























  • Can you share a dummy shape file?

    – R.K.
    Sep 28 '12 at 15:52











  • how do I share it? email?

    – vascobnunes
    Sep 28 '12 at 15:58











  • dropbox.com/sh/b5uyzqf7oowsv8e/lL9bHRHTCT

    – vascobnunes
    Sep 28 '12 at 16:00














6












6








6


3






I'm trying to programmatically populate a shp field with polygon areas:



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )

ds = None


But I get an empty values in the field...










share|improve this question
















I'm trying to programmatically populate a shp field with polygon areas:



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )

ds = None


But I get an empty values in the field...







python shapefile ogr polygon-creation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 35 mins ago









PolyGeo

53.5k1780240




53.5k1780240










asked Sep 28 '12 at 15:39









vascobnunesvascobnunes

2,79283766




2,79283766













  • Can you share a dummy shape file?

    – R.K.
    Sep 28 '12 at 15:52











  • how do I share it? email?

    – vascobnunes
    Sep 28 '12 at 15:58











  • dropbox.com/sh/b5uyzqf7oowsv8e/lL9bHRHTCT

    – vascobnunes
    Sep 28 '12 at 16:00



















  • Can you share a dummy shape file?

    – R.K.
    Sep 28 '12 at 15:52











  • how do I share it? email?

    – vascobnunes
    Sep 28 '12 at 15:58











  • dropbox.com/sh/b5uyzqf7oowsv8e/lL9bHRHTCT

    – vascobnunes
    Sep 28 '12 at 16:00

















Can you share a dummy shape file?

– R.K.
Sep 28 '12 at 15:52





Can you share a dummy shape file?

– R.K.
Sep 28 '12 at 15:52













how do I share it? email?

– vascobnunes
Sep 28 '12 at 15:58





how do I share it? email?

– vascobnunes
Sep 28 '12 at 15:58













dropbox.com/sh/b5uyzqf7oowsv8e/lL9bHRHTCT

– vascobnunes
Sep 28 '12 at 16:00





dropbox.com/sh/b5uyzqf7oowsv8e/lL9bHRHTCT

– vascobnunes
Sep 28 '12 at 16:00










1 Answer
1






active

oldest

votes


















15














You have to use lyr.SetFeature(i) to trigger the update in your shape file.
You'll have to close the data sources in the end so things get written.



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )
lyr.SetFeature(i)
ds = None


*Low-rep comment and concern: If the 'Area' field already exists, this code creates an extra field, like "Area_n", and overwrites the existing Area field. Maybe folks should add some safety code like:



ldef = lyr.GetLayerDefn()
if ldef.GetFieldIndex("Area") != -1:
print "'Area' field already defined"
... # exit or overwrite logic





share|improve this answer


























  • You're welcome. An upvote would be nice, too ;-)

    – R.K.
    Sep 28 '12 at 16:15











  • I have similar project in .net,code is similar as well.In your code you mentioned that after SetField method,should use SetFeature in order to update layer.Unfortunately that doesn't work.Any ideas ?

    – Myra
    Feb 6 '13 at 6:37











  • @Myra You might want to ask that on a separate question. Unfortunately, I'm not really familiar with .NET. Hopefully, others can help you. Just post the question :)

    – R.K.
    Feb 21 '13 at 15:19











  • Great, wonder why "SetFeature" is mentioned nowhere in the docs...

    – adamczi
    Jul 11 '18 at 21:00











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f34505%2fprogrammatically-populating-shapefile-field-with-polygon-areas-in-ogr%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









15














You have to use lyr.SetFeature(i) to trigger the update in your shape file.
You'll have to close the data sources in the end so things get written.



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )
lyr.SetFeature(i)
ds = None


*Low-rep comment and concern: If the 'Area' field already exists, this code creates an extra field, like "Area_n", and overwrites the existing Area field. Maybe folks should add some safety code like:



ldef = lyr.GetLayerDefn()
if ldef.GetFieldIndex("Area") != -1:
print "'Area' field already defined"
... # exit or overwrite logic





share|improve this answer


























  • You're welcome. An upvote would be nice, too ;-)

    – R.K.
    Sep 28 '12 at 16:15











  • I have similar project in .net,code is similar as well.In your code you mentioned that after SetField method,should use SetFeature in order to update layer.Unfortunately that doesn't work.Any ideas ?

    – Myra
    Feb 6 '13 at 6:37











  • @Myra You might want to ask that on a separate question. Unfortunately, I'm not really familiar with .NET. Hopefully, others can help you. Just post the question :)

    – R.K.
    Feb 21 '13 at 15:19











  • Great, wonder why "SetFeature" is mentioned nowhere in the docs...

    – adamczi
    Jul 11 '18 at 21:00
















15














You have to use lyr.SetFeature(i) to trigger the update in your shape file.
You'll have to close the data sources in the end so things get written.



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )
lyr.SetFeature(i)
ds = None


*Low-rep comment and concern: If the 'Area' field already exists, this code creates an extra field, like "Area_n", and overwrites the existing Area field. Maybe folks should add some safety code like:



ldef = lyr.GetLayerDefn()
if ldef.GetFieldIndex("Area") != -1:
print "'Area' field already defined"
... # exit or overwrite logic





share|improve this answer


























  • You're welcome. An upvote would be nice, too ;-)

    – R.K.
    Sep 28 '12 at 16:15











  • I have similar project in .net,code is similar as well.In your code you mentioned that after SetField method,should use SetFeature in order to update layer.Unfortunately that doesn't work.Any ideas ?

    – Myra
    Feb 6 '13 at 6:37











  • @Myra You might want to ask that on a separate question. Unfortunately, I'm not really familiar with .NET. Hopefully, others can help you. Just post the question :)

    – R.K.
    Feb 21 '13 at 15:19











  • Great, wonder why "SetFeature" is mentioned nowhere in the docs...

    – adamczi
    Jul 11 '18 at 21:00














15












15








15







You have to use lyr.SetFeature(i) to trigger the update in your shape file.
You'll have to close the data sources in the end so things get written.



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )
lyr.SetFeature(i)
ds = None


*Low-rep comment and concern: If the 'Area' field already exists, this code creates an extra field, like "Area_n", and overwrites the existing Area field. Maybe folks should add some safety code like:



ldef = lyr.GetLayerDefn()
if ldef.GetFieldIndex("Area") != -1:
print "'Area' field already defined"
... # exit or overwrite logic





share|improve this answer















You have to use lyr.SetFeature(i) to trigger the update in your shape file.
You'll have to close the data sources in the end so things get written.



import sys
import ogr

ds = ogr.Open( 'tttttttttt.shp', update = 1 )
if ds is None:
print "Open failed./n"
sys.exit( 1 )

lyr = ds.GetLayerByName( "tttttttttt" )
lyr.ResetReading()

field_defn = ogr.FieldDefn( "Area", ogr.OFTReal )
lyr.CreateField(field_defn)

for i in lyr:
# feat = lyr.GetFeature(i)
geom = i.GetGeometryRef()
area = geom.GetArea()
print 'Area =', area
lyr.SetFeature(i)
i.SetField( "Area", area )
lyr.SetFeature(i)
ds = None


*Low-rep comment and concern: If the 'Area' field already exists, this code creates an extra field, like "Area_n", and overwrites the existing Area field. Maybe folks should add some safety code like:



ldef = lyr.GetLayerDefn()
if ldef.GetFieldIndex("Area") != -1:
print "'Area' field already defined"
... # exit or overwrite logic






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 4 '13 at 21:23









Dave X

840517




840517










answered Sep 28 '12 at 15:51









R.K.R.K.

15.9k351105




15.9k351105













  • You're welcome. An upvote would be nice, too ;-)

    – R.K.
    Sep 28 '12 at 16:15











  • I have similar project in .net,code is similar as well.In your code you mentioned that after SetField method,should use SetFeature in order to update layer.Unfortunately that doesn't work.Any ideas ?

    – Myra
    Feb 6 '13 at 6:37











  • @Myra You might want to ask that on a separate question. Unfortunately, I'm not really familiar with .NET. Hopefully, others can help you. Just post the question :)

    – R.K.
    Feb 21 '13 at 15:19











  • Great, wonder why "SetFeature" is mentioned nowhere in the docs...

    – adamczi
    Jul 11 '18 at 21:00



















  • You're welcome. An upvote would be nice, too ;-)

    – R.K.
    Sep 28 '12 at 16:15











  • I have similar project in .net,code is similar as well.In your code you mentioned that after SetField method,should use SetFeature in order to update layer.Unfortunately that doesn't work.Any ideas ?

    – Myra
    Feb 6 '13 at 6:37











  • @Myra You might want to ask that on a separate question. Unfortunately, I'm not really familiar with .NET. Hopefully, others can help you. Just post the question :)

    – R.K.
    Feb 21 '13 at 15:19











  • Great, wonder why "SetFeature" is mentioned nowhere in the docs...

    – adamczi
    Jul 11 '18 at 21:00

















You're welcome. An upvote would be nice, too ;-)

– R.K.
Sep 28 '12 at 16:15





You're welcome. An upvote would be nice, too ;-)

– R.K.
Sep 28 '12 at 16:15













I have similar project in .net,code is similar as well.In your code you mentioned that after SetField method,should use SetFeature in order to update layer.Unfortunately that doesn't work.Any ideas ?

– Myra
Feb 6 '13 at 6:37





I have similar project in .net,code is similar as well.In your code you mentioned that after SetField method,should use SetFeature in order to update layer.Unfortunately that doesn't work.Any ideas ?

– Myra
Feb 6 '13 at 6:37













@Myra You might want to ask that on a separate question. Unfortunately, I'm not really familiar with .NET. Hopefully, others can help you. Just post the question :)

– R.K.
Feb 21 '13 at 15:19





@Myra You might want to ask that on a separate question. Unfortunately, I'm not really familiar with .NET. Hopefully, others can help you. Just post the question :)

– R.K.
Feb 21 '13 at 15:19













Great, wonder why "SetFeature" is mentioned nowhere in the docs...

– adamczi
Jul 11 '18 at 21:00





Great, wonder why "SetFeature" is mentioned nowhere in the docs...

– adamczi
Jul 11 '18 at 21:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f34505%2fprogrammatically-populating-shapefile-field-with-polygon-areas-in-ogr%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Щит и меч (фильм) Содержание Названия серий | Сюжет |...

is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

Meter-Bus Содержание Параметры шины | Стандартизация |...