微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用geoalchemy2和PostgreSQL插入几何

如何解决如何使用geoalchemy2和PostgreSQL插入几何

我正在尝试将几何数据插入数据库,但无法正常工作。我为几何创建了一个模型类,但是如果正确的话我现在不知道了。

几何类:

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

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) function st_geomfromewkt(unkNown) does not exist
LINE 1: ..._width,tn,vegetation_index) VALUES (NULL,NULL,ST_GeomFro...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

[sql: INSERT INTO geometry (fill_color,stroke_color,geom,type,area,lengh,creation_date,stroke_width,vegetation_index) VALUES (%(fill_color)s,%(stroke_color)s,ST_GeomFromEWKT(%(geom)s),%(type)s,%(area)s,%(lengh)s,%(creation_date)s,%(stroke_width)s,%(tn)s,%(vegetation_index)s) RETURNING geometry.id]
[parameters: {'fill_color': None,'stroke_color': None,'geom': 'polyGON((0 0,'type': 'Multipolygon','area': 699322.6710989687,'lengh': 3855.587947253079,'creation_date': datetime.datetime(2020,11,4,17,25,37,943407),'stroke_width': None,'tn': '000.000','vegetation_index': None}]
(Background on this error at: http://sqlalche.me/e/13/f405)

我想包括任何类型的几何图形,而不仅仅是多边形

解决方法

几何是列类型,换个名字试试

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()

此处无需实例化 Geometry 对象,只需

geom_test.geom = 'POLYGON((0 0,1 0,1 1,0 1,0 0))' 

然后你可以添加和提交

self._session.add(geom)
self._session.commit()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。