Read and write vector files with GDAL/OGR in Python

preview_player
Показать описание
This tutorial explains how to open vector files with GDAL/OGR in Python, add attribute fields to store measures such as the perimeter and area of a polygon, and save the updated features to a new shapefile. We will also look at how to create a geometry from individual coordinates in Python and write it to a new vector file.

Code:
from osgeo import ogr

# open shapefile
layer = ds.GetLayer()

# get extent
ext = layer.GetExtent()

# get a single feature
feature = layer.GetFeature(1)

# loop over all features in a layer
for feature in layer:
print(feature.GetField("FID"))

# create new shapefile
driver = ogr.GetDriverByName("ESRI Shapefile")

# copy layer
outlayer = ds.CopyLayer(layer, "out")

# create a new attribute field
newfield = ogr.FieldDefn("Perimeter", ogr.OFTReal)
newfield.SetWidth(10)
newfield.SetPrecision(3)
outlayer.CreateField(newfield)

# get the perimeter of each polygon
for feature in outlayer:
geom = feature.GetGeometryRef()
perim = geom.Boundary().Length()
feature.SetField("Perimeter", perim)
outlayer.SetFeature(feature)

# close everything
outlayer = outds = feature = None

# create a ring geometry from the extent of the merged layer
ring.AddPoint(ext[0], ext[2])
ring.AddPoint(ext[1], ext[2])
ring.AddPoint(ext[1], ext[3])
ring.AddPoint(ext[0], ext[3])
ring.AddPoint(ext[0], ext[2])

# convert to polygon
poly.AddGeometry(ring)

# save to shapefile
outlayer = outds.CreateLayer("extent", layer.GetSpatialRef())
feature = ogr.Feature(outlayer.GetLayerDefn())

feature.SetGeometry(poly)
outlayer.CreateFeature(feature)

outds = outlayer = feature = None
Рекомендации по теме
Комментарии
Автор

Great playlist, thank you for these, excellent resource for learning GDAL! A note for anyone else having trouble with the final feature not being updated (i.e. perimeter value of final feature still null when opened in QGIS): make sure you close the feature (as noted at minute 10:12) *within* the for loop (as the final line).

michaelmeadows
Автор

I was struggling with ogr vector functions. Thank you Making Sense Remotely, you made learning such a breeze. I look forward for more content and learnings from your channel. Know that your work and efforts are highly appreciated by those who work in GIS field.

shishirnair
Автор

Wow, thanks so much. Clear voice/sound and explanations. Waiting for more :)

agerman
Автор

reallly helpful and gave very nice overall understanding of OGR vector operations!! thank you

MusicallyDisturbed
Автор

salutations, Making Sense Remotely. pretty good video. thanks. :)

colorizedenhanced-silentmo
Автор

Thank you very much for your grate lesson. I have encountered following issue "KeyError: 'Illegal field requested in GetField()'" when I tried to access "FID" field .However, I was able to access other fields of the layer. Would like to hear any clarification/comment on this

JeewantinieKapilaratne
Автор

If you could link the files (.shp and .tif) you are using in your tutorials it would make them a lot easier to follow. Thanks!

Ryan-tfix
Автор

Thank you very much! Also for the code

sebastianmartijena
Автор

I am looking for a video on how to convert a list of coordinate points from, for example UTM, to lat long, using gdaltransform. Have you already covered this? If not, I would find it very helpful. That is my main reason for wanting to use gdal.

erinlindsay
Автор

How to rotet tif file actioly im trying open tiff file using python but i can able to load but if compated to global mapper image is looking like mirrer image kindly help me to solove this issue

nikhilkumarkulkarni
Автор

I am working with raster data for ANN, i have converted rasters to numaric and then test train that numerical data and get very Good accuracy. now i need to convert my train data into raster but i don't know how. please guide me.

umairrasool
Автор

That's very wonderful and helpful, thank you! please I am beginner in python and I just tried to run my first script but I got an error as following:
"from osgeo import gdal

ImportError: No module named osgeo"
I already installed gdal but it's not working, please could you help me with this?

aichamoumni