I have a shapefile I want to reproject based on another projection, and then save the results.
I am trying to do this like so:
from osgeo import ogr, osr, gdal
#tif with projections I want
tif = gdal.Open("C:/path_to_tif/file.tif")
#shapefile with the from projection
driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open("C:/path_to_shape/file.shp", 1)
layer = dataSource.GetLayer()
#set spatial reference and transformation
sourceprj = layer.GetSpatialRef()
targetprj = osr.SpatialReference(wkt = tif.GetProjection())
transform = osr.CoordinateTransformation(sourceprj, targetprj)
#apply transformation
for feature in layer:
transformed = feature.GetGeometryRef()
transformed.Transform(transformed)
#save shapefile
to_fill = ogr.GetDriverByName("Esri Shapefile")
ds = to_fill.CreateDataSource("C:/path_to_projected/projected.shp")
outlayer = ds.CreateLayer('', None, ogr.wkbPolygon)
outlayer.CreateField(ogr.FieldDefn('id', ogr.OFTInteger))
defn = layer.GetLayerDefn()
feat = ogr.Feature(defn)
feat.SetField('id', 123)
geom = ogr.CreateGeometryFromWkb(transformed.ExportToWkb())
feat.SetGeometry(geom)
outlayer.CreateFeature(feat)
but the resulting shapefile is empty and feat.SetGeometry(geom)
returns 0
CreateDataSource
. Though I would think this would have a compile/runtime error, fix it if it is wrong and let us know.