import geopandas
import matplotlib.pyplot as plt
import numpy as np
from shapely import wkt
path2="F:\\shapefile\\gis_osm_buildings_a_free_1.shp"
#读取shapefile文件,可设置bbox选定区域
gdf2=geopandas.read_file(path2)
print(gdf2.columns)
print(gdf2['name']) #输出name列
'''for i in range(1000):
#wkt_string = wkt.dumps(gdf2['geometry'][i])
#print(i,wkt_string)
print(i,gdf2['geometry'][i].wkt)
print(i,gdf2['geometry'][i].wkb)
'''
#gdf2.plot() #画图
#plt.show()
import pymysql
# 建库和建表
con = pymysql.connect(host='localhost', user='root',
passwd='root', charset='UTF8')
cur = con.cursor()
cur.execute("show databases;")
cur.execute("use test;")
cur.execute("drop table if exists aa;")
cur.execute("create table if not exists `aa`(`id` int(11) not null,`name` varchar(6400) ,`shape` geometry not null)DEFAULT CHARSET=utf8;")
"""
for i in range(100):
value=(i,gdf2['name'][i],gdf2['geometry'][i].wkt)
sql='insert into aa values(%s,"%s",ST_GeomFromText("%s"));'%value
print(sql)
cur.execute(sql)
"""
values=[]
for i in range(1000):
value=(i,gdf2['name'][i],gdf2['geometry'][i].wkt)
print(value)
values.append(value)
#print(values)
cur.executemany("insert into aa values(%s,%s,ST_GeomFromText(%s));", values)
cur.execute("select id,name,st_astext(shape) from aa;")
print(cur.fetchall())
con.commit()
# 关闭连接
cur.close()
con.close()
python读取shapefile批量写入mysql
关注
打赏