在 geodjango 中上传形状文件
我正在尝试通过 django 和 geopandas 将形状文件上传到 postgres。但它返回此错误:
(psycopg2.errors.DuplicateTable) relation "idx_Village_kcnT1Uu_geom" already exists
这是我的模型:
class ShapeFile(models.Model):
name = models.CharField(max_length=45)
description = models.TextField()
file = models.FileField(upload_to='user_shape_file')
date = models.DateTimeField(auto_now_add=True)
并且我正在使用此信号上传它:
@receiver(post_save, sender=ShapeFile)
def post_save_r(instance, **kwargs):
file = instance.file.path
file_format = os.path.basename(file).split('.')[-1]
file_name = os.path.basename(file).split('.')[0]
file_path = os.path.dirname(file)
name = instance.name
connection = 'postgresql://postgres:1234@localhost:5432/geoapp'
with zipfile.ZipFile(file, 'r') as opened_zip:
opened_zip.extractall(file_path)
shape_file = glob.glob(r'{}/**/*.shp'.format(file_path), recursive=True)[0]
gfr = gpd.read_file(shape_file)
epsg = 4326
engine_ = create_engine(connection)
gfr['geom'] = gfr['geometry'].apply(lambda x: WKTElement(x.wkt, srid=epsg))
gfr.to_sql(name, engine_, 'public', if_exists='replace', index=False,
dtype={'geom': Geometry('Geometry', srid=epsg)})
但它返回此错误:
i am Trying to upload a shape file to postgres by django and geopandas. but it return this error:
(psycopg2.errors.DuplicateTable) relation "idx_Village_kcnT1Uu_geom" already exists
this is my model:
class ShapeFile(models.Model):
name = models.CharField(max_length=45)
description = models.TextField()
file = models.FileField(upload_to='user_shape_file')
date = models.DateTimeField(auto_now_add=True)
and i am using this signal to upload it:
@receiver(post_save, sender=ShapeFile)
def post_save_r(instance, **kwargs):
file = instance.file.path
file_format = os.path.basename(file).split('.')[-1]
file_name = os.path.basename(file).split('.')[0]
file_path = os.path.dirname(file)
name = instance.name
connection = 'postgresql://postgres:1234@localhost:5432/geoapp'
with zipfile.ZipFile(file, 'r') as opened_zip:
opened_zip.extractall(file_path)
shape_file = glob.glob(r'{}/**/*.shp'.format(file_path), recursive=True)[0]
gfr = gpd.read_file(shape_file)
epsg = 4326
engine_ = create_engine(connection)
gfr['geom'] = gfr['geometry'].apply(lambda x: WKTElement(x.wkt, srid=epsg))
gfr.to_sql(name, engine_, 'public', if_exists='replace', index=False,
dtype={'geom': Geometry('Geometry', srid=epsg)})
but it return this error:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论