2

I am trying to write a function to reproject a vector file , by taking the original shapefile as input and giving out the name of the reprojected layer . Though when I call this function I get an error saying The file size exceed 2GB, and I don't see why.

The file has only one polygon feature, though i wish to write a generalized code, i am specifying the type as

outlayer=outputdata.CreateLayer('',geom_type=ogr.wkbPolygon) 

for simplicity so that if it works for this , would probably iteratively develop extra functionality

I am fairly new to programming using ogr.

This is what it looks like

#transformataion from one system to the other using ogr
from osgeo import ogr,osr
from epsg_ident import EpsgIdent
from os.path import basename,splitext,exists
def transformfromshapefile(file):
    driver=ogr.GetDriverByName("ESRI Shapefile")
    #to identify the epsg code of the input layer 
    filebasename=splitext(basename(file))[0]  
    fileprj=filebasename+'.prj'
    ident=EpsgIdent()
    ident.read_prj_from_file(fileprj)
    in_epsg_value=int(ident.get_epsg())
    src_epsg=osr.SpatialReference()
    target_epsg=osr.SpatialReference()
    src_epsg.ImportFromEPSG(in_epsg_value)
    target_epsg.ImportFromEPSG(4326)
    #define the transformation
    transformation=osr.CoordinateTransformation(src_epsg,target_epsg)
    #create an output layer if does not exist, or delete and create it if it already does
    outputfile="projected.shp"
    if(exists(outputfile)):
        driver.DeleteDataSource(outputfile)
    outputdata=driver.CreateDataSource(outputfile)
    #set the fields for the output layer using the fields of the input layer, which is inlayer, outputlayer is outlayer
    inputshapefile=driver.Open(file)
    inlayer=inputshapefile.GetLayer()
    outlayer=outputdata.CreateLayer('',geom_type=ogr.wkbPolygon)
    inlayerdefinition=inlayer.GetLayerDefn()
    for i in range(inlayerdefinition.GetFieldCount()):
        fieldDefn=inlayerdefinition.GetFieldDefn(i)
        outlayer.CreateField(fieldDefn)
    #Get the feature definition of the outputlayer
    outlayerdefn=outlayer.GetLayerDefn()
    #Transform the outputlayer features , looping through each feature
    infeature=inlayer.GetNextFeature()
    while infeature:
        geom=infeature.GetGeometryRef()
        #Reproject the geometry 
        geom.Transform(transformation)
        #create a new feature that is transformed
        newfeature=ogr.Feature(outlayerdefn)
        #Set the geometry for the givenfeature
        newfeature.SetGeometry(geom)
        #Set the fields 
        for i in range(outlayerdefn.GetFieldCount()):
            newfeature.SetField(outlayerdefn.GetFieldDefn(i).GetNameRef(),infeature.GetField(i))
        #Add the feature to the shapefile
        outlayer.CreateFeature(newfeature)
        #Dereference the outfeature for the next loop
        newfeature=None
        inlayer.GetNextFeature()

The size of the input .shp file is 300KB and for the .dbf file it is 1KB

  • 1
    Please Edit the question to specify how many features are present and the sizes of the .shp and .dbf components before conversion. – Vince Dec 7 '19 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.