I'm looking to populate a newly created field in a shapefile that from a list using basic python and osgeo tools. The values are integers and in a python list that is the length of the number of features in the shapefile. The goal is to have a set of unique numbers for grabbing random samples of features. Here is example.
from osgeo import ogr, osr import os, sys import random driver = ogr.GetDriverByName ("ESRI Shapefile") sampleshapefile = ogr.Open('shapefile.shp', 1) sample = sampleshapefile.GetLayer() new_field = ogr.FieldDefn('randnum', ogr.OFTInteger) f40.CreateField(new_field) ran = random.sample(range(1, len(sample)+1), len(sample))
I've seen the examples such as Programmatically populating shapefile field with polygon areas in OGR?. Yet in that example the for loop on each feature fills the field with the result of a geometry calculation on the feature. I'm looking to have the first value in the list "ran" be value in the "randnum" field of the first feature, second list value for the second feature and so on.