首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用geoalchemy2和postgresql插入几何学

如何用geoalchemy2和postgresql插入几何学
EN

Stack Overflow用户
提问于 2020-11-04 20:37:51
回答 1查看 1.6K关注 0票数 1

我试图将几何数据插入数据库,但它不起作用。我创建了一个几何模型类,但如果它是对的,我现在就不这样做了。

几何类:

代码语言:javascript
复制
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)

插入代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
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, tn, 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,1 0,1 1,0 1,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)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-23 00:54:52

几何图形是列类型,请尝试另一个名称。

代码语言:javascript
复制
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()

这里不需要实例化几何对象,只需

代码语言:javascript
复制
geom_test.geom = 'POLYGON((0 0,1 0,1 1,0 1,0 0))' 

然后可以添加和提交

代码语言:javascript
复制
self._session.add(geom)
self._session.commit()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64687383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档