0

嗨,我正在尝试在 postgis 中存储一个 shp,创建一个附加列,我在其中插入几何图形,但试图转换 gdf ​​['geom'] = gdf ​​['geometry']。apply (lambda x: WKTElement (x.wkt, srid = epsg)),服务器溢出,我没有错误但我想是因为我们有空数据,我在mac环境中工作

我的模型.py

# extract zipfile
with zipfile.ZipFile(file, 'r') as zip_ref:
    zip_ref.extractall(file_path)

os.remove(file)  # remove zip file

shp = glob.glob(r'{}/**/*.shp'.format(file_path),
                recursive=True)  # to get shp

try:
    req_shp = shp[0]
    gdf = gpd.read_file(req_shp)  # make geodataframe

    crs_name = gdf.crs

    epsg = crs_name.to_epsg()

    if epsg is None:
        epsg = 4326  # wgs 84 coordinate system

    #geom_type = gdf.geom_type[1]

    engine = create_engine(conn_str)  # create the SQLAlchemy's engine to use

    print('-----', name, '-----')

    gdf['geom'] = gdf['geometry'].apply(
        lambda x:WKTElement(x.wkt, srid=epsg))
    print('-----', name, '-----')
    # Drop the geometry column (since we already backup this column with geom)
    gdf.drop('geometry', 1, inplace=True)
    gdf.to_sql(name, engine, 'data', if_exists='replace',
              index=False, dtype={'geom': Geometry('Geometry', srid=epsg)})  # post gdf to the postgresql
    gdf.to_postgis(
        con=engine,
        name=name,
        if_exist="replace")
    for s in shp:
       os.remove(s)

except Exception as e:
    for s in shp:
        os.remove(s)

    instance.delete()
    print("There is problem during shp upload: ", e)

geo.create_featurestore(store_name='xxxxxxxx', workspace='xxxxxxxxx', db='xxxxxxxx',
                        host='localhost', pg_user='postgres', pg_password='xxxxxxxx', schema='xxxx')
geo.publish_featurestore(
    workspace='xxxxx', store_name='geoApp', pg_table=name)

geo.create_outline_featurestyle('geoApp_shp', workspace='xxxxx')

geo.publish_style(layer_name=name, style_name='geoApp_shp',
                  workspace='xxxxxxx')


@receiver(post_delete, sender=Shp)
def delete_data(sender, instance, **kwargs):
    db.delete_table(table_name=instance.name, schema='xxxx')
    geo.delete_layer(instance.name, 'geoapp')
4

0 回答 0