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