于是想起来利用ogr和MySQLdb这两个Python库,写一个简单的脚本来实现这个过程,也算是熟悉一下ogr库的用法。仔细一看,ogr大约也是走OpenGIS的相关标准,定义了Layer、Feature等相关的名词。
以下是代码片段,文档不好找,希望对读者有帮助吧
from osgeo import ogr
def insertion(f, curs):
global c
g = ogr.Open(f)
l = g.GetLayer(0)
for i in range(l.GetFeatureCount()):
f = l.GetFeature(i)
name = unicode(f.GetFieldAsString("name"),"GBK").encode("utf8")
wkt = str(f.GetGeometryRef())
params = "null, GeomFromText('%s'),'%s','',0,0,%d"% (wkt,name,c)
sql = 'insert into '+tablename+' values(%s);'%params
print sql
fsql.write(sql)
fsql.write('\n')
c = c+1
curs.execute(sql)

