0

I just write a small website to show some spatial data using flask and Geoalchemy2. Now I can insert some new spatial records (for example, points) into my postgresql database but having some troubles when I want to update them.

my code is as below.

Model.py:

class Geopoint(db.Model):
"""point class"""
    __tablename__ = 'geo_point'
    ptid = db.Column(db.Integer, primary_key=True)
    quiztime = db.Column(db.Numeric)
    geopt = db.Column(Geography(geometry_type='POINT', srid=4326))

init.py:

db = SQLAlchemy()
geo_engine = create_engine('postgresql://postgres:password@localhost/database', echo=True)

view.py:

geo_session_class = sessionmaker(bind=geo_engine)
geo_session = geo_session_class()

if request.method == 'POST':
    if geo_type == 'POINT':
        pt_res = geo_session.query(
            Geopoint.ptid,
            Geopoint.quiztime,
            Geopoint.geopt.ST_AsText()
        ).filter_by(ptid=geo_id).first()
        if pt_res:
            print pt_res
        else:
            geo_session.add(
                Geopoint(
                    quiztime=time.time(),
                    geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
                )
            )
            geo_session.commit()

My code works when I add a new point record.

When I edit the existed point my code of update part returns the printed result (I just want to know how to write it.):

(4L, Decimal('1508430387.581'), u'POINT(120.057, 30.262)')

that does not look like a class but a tuple so I can not update it with

Geopoint.geopt=.....
db.session.add(Geopoint)
db.session.commit()

In the official document there are only examples to add new objects into the database so I am really confused.

Is there any MAGIC sentence to update the data or is there any other geography orm libraries to use?

Appreciation for any answer.

韩双犬
  • 1
  • 1

1 Answers1

0

emmmmmm……

Finally I figure out it myself.

Actually the change is very simple. I just change the query object so it return a Geopoint object to me that can be updated.

geo_session_class = sessionmaker(bind=geo_engine) geo_session = geo_session_class()

if request.method == 'POST':
    if geo_type == 'POINT':
        pt_res = geo_session.query(
            Geopoint.ptid,
            Geopoint.quiztime,
            Geopoint.geopt.ST_AsText()
        ).filter_by(ptid=geo_id).first()
        if pt_res:
            print pt_res
        else:
            geo_session.add(
                Geopoint(
                    quiztime=time.time(),
                    geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
                )
            )
            geo_session.commit()

change to this.

if request.method == 'POST':
    if geo_type == 'POINT':
        pt_res = geo_session.query(Geopoint).filter_by(ptid=geo_id).first()
        if pt_res:
            print pt_res
        else:
            geo_session.add(
                Geopoint(
                    quiztime=time.time(),
                    geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
                )
            )
            geo_session.commit()

Then my update code is possible to write.

=。=

韩双犬
  • 1
  • 1