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?
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
add a comment |
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
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
add a comment |
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
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
python shapefile ogr polygon-creation
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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