Python 如何使用geoalchemy2和postgresql插入几何体
我正在尝试将几何体数据插入数据库,但它不起作用。我为几何体创建了一个模型类,但现在不知道它是否正确 几何类:Python 如何使用geoalchemy2和postgresql插入几何体,python,python-3.x,postgresql,geoalchemy2,geoalchemy,Python,Python 3.x,Postgresql,Geoalchemy2,Geoalchemy,我正在尝试将几何体数据插入数据库,但它不起作用。我为几何体创建了一个模型类,但现在不知道它是否正确 几何类: from datetime import datetime from sqlalchemy import and_ from sqlalchemy import Column from sqlalchemy import create_engine from sqlalchemy import DateTime from sqlalchemy import Float from sql
from datetime import datetime
from sqlalchemy import and_
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import DateTime
from sqlalchemy import Float
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import SMALLINT
from sqlalchemy import BIGINT
from sqlalchemy import String
from sqlalchemy import DateTime
from geoalchemy2 import Geometry as Geom
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Geometry(Base):
__tablename__ = "geometry"
id = Column(Integer, primary_key=True)
fill_color = Column(String(7))
stroke_color = Column(String(7))
geom = Column(Geom(geometry_type=None)) # here my Geometry column
type = Column(String(20))
area = Column(BIGINT, nullable=False)
lengh = Column(BIGINT, nullable=False)
creation_date = Column(DateTime, nullable=False)
stroke_width = Column(SMALLINT, nullable=False)
tn = Column(Integer, nullable=False)
vegetation_index = Column(Integer)
插入代码:
for index, line in data[['Shape_Area', 'Shape_Leng', 'TN',
'geometry']].iterrows():
geometry = line['geometry']
area = line['Shape_Area']
lengh = line['Shape_Leng']
tn = line['TN']
now = datetime.now()
geom = Geometry(geom='POLYGON((0 0,1 0,1 1,0 1,0 0))',
type=geometry.type, area=area, lengh=lengh, creation_date=now, tn=tn)
self._session.add(geom)
self._session.commit()
错误:
sqlalchemy.exc.ProgrammingError
我想包括任何类型的几何体,而不仅仅是多边形几何体是列类型,请尝试其他名称
class myTableWithGeom(Base):
__tablename__ = "myTableWithGeom"
id = Column(Integer, primary_key=True)
fill_color = Column(String(7))
stroke_color = Column(String(7))
geom = Column(Geometry(geometry_type='POLYGON')) # here your Geometry column
type = Column(String(20))
area = Column(BIGINT, nullable=False)
lengh = Column(BIGINT, nullable=False)
creation_date = Column(DateTime, nullable=False)
stroke_width = Column(SMALLINT, nullable=False)
tn = Column(Integer, nullable=False)
vegetation_index = Column(Integer)
geom_test = myTableWithGeom()
这里不需要实例化几何体对象,只需
geom_test.geom = 'POLYGON((0 0,1 0,1 1,0 1,0 0))'
然后您可以添加并提交
self._session.add(geom)
self._session.commit()
几何图形是列类型,请尝试其他名称
class myTableWithGeom(Base):
__tablename__ = "myTableWithGeom"
id = Column(Integer, primary_key=True)
fill_color = Column(String(7))
stroke_color = Column(String(7))
geom = Column(Geometry(geometry_type='POLYGON')) # here your Geometry column
type = Column(String(20))
area = Column(BIGINT, nullable=False)
lengh = Column(BIGINT, nullable=False)
creation_date = Column(DateTime, nullable=False)
stroke_width = Column(SMALLINT, nullable=False)
tn = Column(Integer, nullable=False)
vegetation_index = Column(Integer)
geom_test = myTableWithGeom()
这里不需要实例化几何体对象,只需
geom_test.geom = 'POLYGON((0 0,1 0,1 1,0 1,0 0))'
然后您可以添加并提交
self._session.add(geom)
self._session.commit()