I am trying to copy a selection of features from a shapefile to a separate shapefile, but I am stuck with trying to set the attributes of the separate shapefile to be the same as that of the original shapefile. I first thought about creating a list of attributes with the ' Name', 'Type', 'Width' and 'Precision' of each attribute and than use that list to define the attributes of the new shapefile. But I am unsure how to go about doing this?
My code so far is just constructing a list of existing attributes and creating a new shapefile:
attributes = []
for i in range(layerDef.GetFieldCount()):
fieldName = layerDef.GetFieldDefn(i).GetName()
fieldTypeCode = layerDef.GetFieldDefn(i).GetType()
fieldType = layerDef.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode)
fieldWidth = layerDef.GetFieldDefn(i).GetWidth()
GetPrecision = layerDef.GetFieldDefn(i).GetPrecision()
attributesList = fieldName, fieldType, fieldWidth, GetPrecision
attributes.append(attributesList)
newShapefile = driver.CreateDataSource("BuildingsFilter.shp")
srs = osr.SpatialReference()
srs.ImportFromWkt(DataSrsWkt)
newLayer = newShapefile.CreateLayer("BuildingsFilter, srs, geom_type = layer.GetLayerDefn().GetGeomType())