# -*- coding: utf-8 -*-

from datetime import datetime

from sqlalchemy.types import Integer, String, DateTime
from geoalchemy2.types import Geometry

from skylines.model import db


class Airspace(db.Model):
    __tablename__ = 'airspace'

    id = db.Column(Integer, autoincrement=True, primary_key=True)
    time_created = db.Column(DateTime, nullable=False, default=datetime.utcnow)
    time_modified = db.Column(DateTime, nullable=False, default=datetime.utcnow)

    the_geom = db.Column(Geometry('POLYGON', srid=4326))

    name = db.Column(String(), nullable=False)
    airspace_class = db.Column(String(3), nullable=False)
    base = db.Column(String(30), nullable=False)
    top = db.Column(String(30), nullable=False)
    country_code = db.Column(String(2), nullable=False)

    def __repr__(self):
        return ('<Airspace: id=%d name=\'%s\'>' % (self.id, self.name)).encode('unicode_escape')

    @classmethod
    def by_location(cls, location):
        '''Returns a query object of all airspaces at the location'''
        return cls.query() \
            .filter(cls.the_geom.ST_Contains(location.make_point()))

@@ -1122,8 +1122,685 @@
 int()))%0A
+%0A    def extract_height(self, column):%0A        if column == 'GND':%0A            return -1000, 'msl'%0A%0A        elif column.startswith('FL'):%0A            return int(column%5B3:%5D) * 100 / 3.2808399, 'fl'%0A%0A        elif column.endswith('AGL'):%0A            return int(column%5B:-4%5D) / 3.2808399, 'agl'%0A%0A        elif column.endswith('MSL'):%0A            return int(column%5B:-4%5D) / 3.2808399, 'msl'%0A%0A        elif column == 'NOTAM':%0A            return -1000, 'notam'%0A%0A        else:%0A            return -1000, 'unknown'%0A%0A    @property%0A    def extract_base(self):%0A        return self.extract_height(self.base)%0A%0A    @property%0A    def extract_top(self):%0A        return self.extract_height(self.top)%0A

add extract_airspace method