import os.path
import MySQLdb
import osgeo.ogr
coection = MySQLdb.coect(host = 'localhost',user='root',passwd='')
cursor = coection.cursor()
cursor.execute("USE distal")
cursor.execute("DELETE FROM countries")
cursor.execute("SET GLOBAL max_allowed_packet=50000000")
srcFile = os.path.join("DISTAL-data", "E:ComputerProgrammingPythonProjectTM_WORLD_B ORDERS-0.3",
"E:ComputerProgrammingPythonProjectTM_WORLD_B ORDERS-0.3TM_WORLD_BORDERS-0.3.shp")
shapefile = osgeo.ogr.Open(srcFile)
layer = shapefile.GetLayer(0)
for i in range(layer.GetFeatureCount()):
feature = layer.GetFeature(i)
name = feature.GetField("NAME").decode("Latin-1")
wkt = feature.GetGeometryRef().ExportToWkt()
cursor.execute("INSERT INTO countries (name,outline) " +
"VALUES (%s, PolygonFromText(%s, 4326))",
(name.encode("utf8"), wkt))
coection.commit()
برچسب:
نویسنده: خنج