From 529611b42c8eae628d4ad980c5a9c5e5b2a1530e Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Wed, 18 May 2022 14:11:51 +0100
Subject: [PATCH] Update model

---
 geocode/model.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/geocode/model.py b/geocode/model.py
index 16efdaa..67cc45f 100644
--- a/geocode/model.py
+++ b/geocode/model.py
@@ -21,7 +21,12 @@ class Polygon(Base):
     way_area = Column(Float)
     tags = Column(postgresql.HSTORE)
     way = Column(Geometry("GEOMETRY", srid=4326, spatial_index=True), nullable=False)
-    area = column_property(func.ST_Area(way))
+    area = column_property(func.ST_Area(way, False))
+
+    @property
+    def osm_url(self):
+        osm_type = "way" if self.osm_id > 0 else "relation"
+        return f"https://www.openstreetmap.org/{osm_type}/{abs(self.osm_id)}"
 
     @hybrid_property
     def area_in_sq_km(self):
@@ -30,8 +35,9 @@ class Polygon(Base):
     @classmethod
     def coords_within(cls, lat, lon):
         point = func.ST_SetSRID(func.ST_MakePoint(lon, lat), 4326)
-        return cls.query.filter(cls.admin_level.isnot(None),
-                                func.ST_Within(point, cls.way))
+        return (cls.query.filter(cls.admin_level.isnot(None),
+                                 func.ST_Within(point, cls.way))
+                         .order_by(cls.area))
 
 class Scotland(Base):
     __tablename__ = "scotland"