My question concerns OGR
in Python3
on Windows
. So I've installed gdal
from the wheel file, and I started playing with OGR
functions, but there's a problem I'm encountering that I can't understand. If GetFeatureCount()
is called on a layer that was opened in a separate file Python stops working, but not if all my code is in the same script (reading & get count of features).
The code below works:
from osgeo import ogr
filename = '<path>'
driver = ogr.GetDriverByName("ESRI Shapefile")
datasource = driver.Open(filename)
layer = datasource.GetLayer()
print('Features count:', layer.GetFeatureCount())
But if I move the opening of the shapefile's layer to a separate file, it doesn't work anymore (Python stops working somehow):
class IO:
@staticmethod
def read_vector(filename):
# load vector
driver = ogr.GetDriverByName("ESRI Shapefile")
datasource = driver.Open(filename)
layer = datasource.GetLayer()
return layer
And it called from this file:
from gdal_io import IO
filename = '<path>'
layer = IO.read_vector(filename)
print('Features count:', layer.GetFeatureCount())
Is something necessary after reading the shapefile vector with OGR
?