目前建议的方法:
from osgeo import ogr
为了保持兼容性,同样可以使用下面的方法:
try:
from osgeo import ogr
except:
import ogr
from pathlib import Path
inshp = Path('/data/gdata/GSHHS_c.shp')
from osgeo import ogr
datasource = ogr.Open(inshp)
driver = datasource.GetDriver()
driver.ShortName
'ESRI Shapefile'
driver.LongName
'ESRI Shapefile'
driver = ogr.GetDriverByName('ESRI Shapefile')
数据驱动driver的Open()(方法返回一个数据源对象),其中update为0是只读,为1是可写)。 例如:
Open(self, char name, int update = 0) -> DataSource
dataSource = driver.Open(inshp,0)
if dataSource is None:
print ('could not open')
else:
print ('QED.')
QED.
注意filename一定要写绝对路径! 使用Python的内省函数 dir()
看一下datasource有哪些可用的方法。
dir(datasource)[:10]
['AbortSQL', 'AddBand', 'AddFieldDomain', 'AddRelationship', 'AdviseRead', 'BeginAsyncReader', 'BuildOverviews', 'ClearStatistics', 'Close', 'CommitTransaction']