1 year ago

#331952

test-img

jabar

upload shape file in geodjango

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:

enter image description here

django

geopandas

geodjango

0 Answers

Your Answer

Accepted video resources