2010-11-01 20 views

cevap

15

SQLAlchemy 0.8 kullanarak, Flask-SQLAlchemy ve Geoalchemy 2:

from app import db 
from geoalchemy2.types import Geometry 

class Point(db.Model): 

    """represents an x/y coordinate location.""" 

    __tablename__ = 'point' 

    id = db.Column(db.Integer, primary_key=True) 
    geom = db.Column(Geometry(geometry_type='POINT', srid=4326)) 

Örnek sorgu: x ve y koordinatlarının sonucu dönüştürme

from geoalchemy2.elements import WKTElement 
from app import models 

def get_nearest(lat, lon): 
    # find the nearest point to the input coordinates 
    # convert the input coordinates to a WKT point and query for nearest point 
    pt = WKTElement('POINT({0} {1})'.format(lon, lat), srid=4326) 
    return models.Point.query.order_by(models.Point.geom.distance_box(pt)).first() 

bir yolu (GeoJSON dönüştürmek ve koordinatları ayıkla):

import geoalchemy2.functions as func 
import json 
from app import db 

def point_geom_to_xy(pt): 
    # extract x and y coordinates from a point geometry 
    geom_json = json.loads(db.session.scalar(func.ST_AsGeoJSON(pt.geom))) 
    return geom_json['coordinates'] 
1

Flask kullanımıyla sınırlı değilseniz, Pylons'a dayanan ve GeoAlchemy kullanan bir MapFish denemek isteyebilirsiniz.

1

Flask-SQLAlchemy ile birlikte kullanabilirsiniz, ancak bunu düz SQLAlchemy ile de kullanabilirsiniz. Sadece sample models from GeoAlchemy'u Flask-SQLAlchemy'ye çevir. Bunun gibi bir şey: Bu kodu test etmedim, ancak adil bir transkripsiyon olmalıydı,

+2

Bu kod çalışmıyor: size "AttributeError" verir: 'SQLAlchemy' nesnesinin 'GeometryColumn' özelliği yoktur. – jsalonen

0
from myapp import db 
from geoalchemy import GeometryColumn, Point 

class FixXX(db.Model): 

    __tablename__ = 'fixXX' 

    fix_pk = db.Column(db.Integer, primary_key=True) 
    fix = db.Column(db.String) 
    geometry = GeometryColumn(Point(2, srid=4326)) 

GeometryDDL(FixXX.__table__)