Skip to content

Commit

Permalink
Adding new column simplified_geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
ssadhu-sl committed Sep 24, 2024
1 parent 060944e commit 1e79a3b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
9 changes: 5 additions & 4 deletions application/data_access/entity_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ def _apply_location_filters(session, query, params):
clauses.append(
or_(
and_(
EntityOrm.geometry.is_not(None),
func.ST_IsValid(EntityOrm.geometry),
EntityOrm.simplified_geometry.is_not(None),
func.ST_IsValid(EntityOrm.simplified_geometry),
spatial_function(
EntityOrm.geometry, func.ST_GeomFromText(geometry, 4326)
EntityOrm.simplified_geometry,
func.ST_GeomFromText(geometry, 4326),
),
),
and_(
Expand All @@ -195,7 +196,7 @@ def _apply_location_filters(session, query, params):
)
if clauses:
query = query.filter(or_(*clauses))

print("query:: ", query)
intersecting_entities = params.get("geometry_entity", [])
if intersecting_entities:
intersecting_entities_query = (
Expand Down
3 changes: 3 additions & 0 deletions application/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class EntityOrm(Base):
reference = Column(Text, nullable=True)
typology = Column(Text, nullable=True)
geometry = Column(Geometry(geometry_type="MULTIPOLYGON", srid=4326), nullable=True)
simplified_geometry = Column(
Geometry(geometry_type="MULTIPOLYGON", srid=4326), nullable=True
)
point = Column(Geometry(geometry_type="POINT", srid=4326), nullable=True)
_geometry_geojson = column_property(func.ST_AsGeoJSON(geometry))
_point_geojson = column_property(func.ST_AsGeoJSON(point))
Expand Down
45 changes: 45 additions & 0 deletions migrations/versions/3ba8728c2a20_add_simplified_geometry_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Add simplified_geometry column
Revision ID: 3ba8728c2a20
Revises: 4703bef121cb
Create Date: 2024-09-24 14:02:29.091241
"""
import geoalchemy2

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "3ba8728c2a20"
down_revision = "4703bef121cb"
branch_labels = None
depends_on = None


def upgrade():
# Add the new column to the entity table
op.add_column(
"entity",
sa.Column(
"simplified_geometry",
geoalchemy2.types.Geometry(
geometry_type="MULTIPOLYGON",
srid=4326,
from_text="ST_GeomFromEWKT",
name="geometry",
),
nullable=True,
),
)
op.execute(
sa.text(
" UPDATE entity SET simplified_geometry = ST_SimplifyPreserveTopology(geometry, 0.0001);"
)
) # noqa


def downgrade():
# Remove the column if downgrading
op.drop_column("entity", "simplified_geometry")
24 changes: 12 additions & 12 deletions tests/test_data/entities.csv
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
entity|name|entry_date|start_date|end_date|dataset|json|organisation_entity|prefix|reference|typology|geometry|point
1|Green space|2019-01-07||2020-01-07|greenspace|""|""|greenspace|Q1234567|geography|MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))|POINT (-0.33753991127014155 53.74458682618967)
2|Forest|2020-01-07|||forest|""|""|forest|Q7654321|geography|MultiPolygon (((-2.7509593963623047 52.93715549235, -2.777738571166992 52.93534492713729, -2.7803993225097656 52.92815364890322, -2.7874374389648438 52.91883942676673, -2.7840042114257812 52.909160862597396, -2.757740020751953 52.909937297800774, -2.740316390991211 52.92427296652735, -2.7383422851562496 52.93746586735196, -2.7509593963623047 52.93715549235)))|POINT (-2.764263153076172 52.92416947690202)
3|Brownfield site|2021-01-07|||brownfield-site|""|""|brownfield-land|Q111222|geography|MultiPolygon (((-1.9229105114936829 52.42831459230579, -1.9224652647972105 52.4283636568728, -1.9224652647972105 52.428456879399704, -1.922604739665985 52.42844379554817, -1.9229695200920107 52.428402908487115, -1.9229105114936829 52.42831459230579)))|POINT (-1.9227442145347595 52.42837183429533)
4|Historical monument|2022-01-07|||historical-monument|""|""|historical-monument|Q333444|geography|MultiPolygon (((-1.824502944946289 51.18507295677642, -1.8281936645507812 51.180822619154625, -1.8299531936645508 51.17891252968377, -1.8306827545166016 51.17681403030165, -1.8283653259277342 51.176598794195044, -1.8196105957031252 51.178105425840684, -1.8184518814086914 51.18273262948443, -1.817464828491211 51.185046057137775, -1.824502944946289 51.18507295677642)))|POINT (-1.8236660957336424 51.18125305092543)
5|Historical monument|2022-01-07|||historical-monument|""|""|historical-monument|Q333444|geography|""|
3030404|Rosendale Road|2020-09-04|||conservation-area|""||conservation-area|CA47|geography|MultiPolygon (((-0.094539 51.440354,-0.094751 51.440312,-0.094718 51.440251,-0.094946 51.440203,-0.094921 51.440154,-0.094986 51.440141,-0.094983 51.440134,-0.095365 51.440058,-0.095390 51.440111,-0.095862 51.440023,-0.095816 51.439924,-0.095940 51.439898,-0.096625 51.439554,-0.096877 51.439473,-0.096918 51.439507,-0.097020 51.439467,-0.097313 51.439413,-0.097170 51.439113,-0.097555 51.439043,-0.097889 51.438972,-0.097673 51.438500,-0.097625 51.438401,-0.097605 51.438385,-0.097100 51.437322,-0.096857 51.436788,-0.095302 51.437125,-0.095525 51.437577,-0.094770 51.437739,-0.094792 51.437782,-0.094067 51.437912,-0.094003 51.437773,-0.093807 51.437811,-0.093649 51.437500,-0.092809 51.437654,-0.092610 51.437695,-0.092600 51.437703,-0.092597 51.437725,-0.092641 51.437823,-0.092684 51.437815,-0.092806 51.437988,-0.092791 51.437991,-0.092981 51.438322,-0.093016 51.438412,-0.093113 51.438598,-0.093307 51.438893,-0.093355 51.438979,-0.093475 51.439141,-0.093650 51.439435,-0.093671 51.439500,-0.093684 51.439602,-0.093737 51.439795,-0.093734 51.439936,-0.093840 51.440200,-0.093908 51.440338,-0.094363 51.440240,-0.094430 51.440375,-0.094539 51.440354)))|POINT(-0.095357 51.438607)
7002001662||2021-12-27|||tree|""||tree|1662|geography||POINT (-0.094959 51.439306)
7002001663||2021-12-27|||tree|""||tree|1663|geography||POINT (-0.094987 51.439363)
7002001664||2021-12-27|||tree|""||tree|1664|geography||POINT (-0.094894 51.439378)
101|Dacorum Borough Council|2022-06-06|||local-authority||600001|local-authority|DAC|organisation||
44002322|Historic England|2022-04-12|||conservation-area|||conservation-area|1|geography|""|POINT (-2.246357 50.91254)
entity|name|entry_date|start_date|end_date|dataset|json|organisation_entity|prefix|reference|typology|geometry|point|simplified_geometry
1|Green space|2019-01-07||2020-01-07|greenspace|||greenspace|Q1234567|geography|MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))|POINT (-0.33753991127014155 53.74458682618967)|MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))
2|Forest|2020-01-07|||forest|||forest|Q7654321|geography|MultiPolygon (((-2.7509593963623047 52.93715549235, -2.777738571166992 52.93534492713729, -2.7803993225097656 52.92815364890322, -2.7874374389648438 52.91883942676673, -2.7840042114257812 52.909160862597396, -2.757740020751953 52.909937297800774, -2.740316390991211 52.92427296652735, -2.7383422851562496 52.93746586735196, -2.7509593963623047 52.93715549235)))|POINT (-2.764263153076172 52.92416947690202)|MultiPolygon (((-2.7509593963623047 52.93715549235, -2.777738571166992 52.93534492713729, -2.7803993225097656 52.92815364890322, -2.7874374389648438 52.91883942676673, -2.7840042114257812 52.909160862597396, -2.757740020751953 52.909937297800774, -2.740316390991211 52.92427296652735, -2.7383422851562496 52.93746586735196, -2.7509593963623047 52.93715549235)))
3|Brownfield site|2021-01-07|||brownfield-site|||brownfield-land|Q111222|geography|MultiPolygon (((-1.9229105114936829 52.42831459230579, -1.9224652647972105 52.4283636568728, -1.9224652647972105 52.428456879399704, -1.922604739665985 52.42844379554817, -1.9229695200920107 52.428402908487115, -1.9229105114936829 52.42831459230579)))|POINT (-1.9227442145347595 52.42837183429533)|MultiPolygon (((-1.9229105114936829 52.42831459230579, -1.9224652647972105 52.4283636568728, -1.9224652647972105 52.428456879399704, -1.922604739665985 52.42844379554817, -1.9229695200920107 52.428402908487115, -1.9229105114936829 52.42831459230579)))
4|Historical monument|2022-01-07|||historical-monument|||historical-monument|Q333444|geography|MultiPolygon (((-1.824502944946289 51.18507295677642, -1.8281936645507812 51.180822619154625, -1.8299531936645508 51.17891252968377, -1.8306827545166016 51.17681403030165, -1.8283653259277342 51.176598794195044, -1.8196105957031252 51.178105425840684, -1.8184518814086914 51.18273262948443, -1.817464828491211 51.185046057137775, -1.824502944946289 51.18507295677642)))|POINT (-1.8236660957336424 51.18125305092543)|MultiPolygon (((-1.824502944946289 51.18507295677642, -1.8281936645507812 51.180822619154625, -1.8299531936645508 51.17891252968377, -1.8306827545166016 51.17681403030165, -1.8283653259277342 51.176598794195044, -1.8196105957031252 51.178105425840684, -1.8184518814086914 51.18273262948443, -1.817464828491211 51.185046057137775, -1.824502944946289 51.18507295677642)))
5|Historical monument|2022-01-07|||historical-monument|||historical-monument|Q333444|geography|||
3030404|Rosendale Road|2020-09-04|||conservation-area|||conservation-area|CA47|geography|MultiPolygon (((-0.094539 51.440354,-0.094751 51.440312,-0.094718 51.440251,-0.094946 51.440203,-0.094921 51.440154,-0.094986 51.440141,-0.094983 51.440134,-0.095365 51.440058,-0.095390 51.440111,-0.095862 51.440023,-0.095816 51.439924,-0.095940 51.439898,-0.096625 51.439554,-0.096877 51.439473,-0.096918 51.439507,-0.097020 51.439467,-0.097313 51.439413,-0.097170 51.439113,-0.097555 51.439043,-0.097889 51.438972,-0.097673 51.438500,-0.097625 51.438401,-0.097605 51.438385,-0.097100 51.437322,-0.096857 51.436788,-0.095302 51.437125,-0.095525 51.437577,-0.094770 51.437739,-0.094792 51.437782,-0.094067 51.437912,-0.094003 51.437773,-0.093807 51.437811,-0.093649 51.437500,-0.092809 51.437654,-0.092610 51.437695,-0.092600 51.437703,-0.092597 51.437725,-0.092641 51.437823,-0.092684 51.437815,-0.092806 51.437988,-0.092791 51.437991,-0.092981 51.438322,-0.093016 51.438412,-0.093113 51.438598,-0.093307 51.438893,-0.093355 51.438979,-0.093475 51.439141,-0.093650 51.439435,-0.093671 51.439500,-0.093684 51.439602,-0.093737 51.439795,-0.093734 51.439936,-0.093840 51.440200,-0.093908 51.440338,-0.094363 51.440240,-0.094430 51.440375,-0.094539 51.440354)))|POINT(-0.095357 51.438607)|MultiPolygon (((-0.094539 51.440354,-0.094751 51.440312,-0.094718 51.440251,-0.094946 51.440203,-0.094921 51.440154,-0.094986 51.440141,-0.094983 51.440134,-0.095365 51.440058,-0.095390 51.440111,-0.095862 51.440023,-0.095816 51.439924,-0.095940 51.439898,-0.096625 51.439554,-0.096877 51.439473,-0.096918 51.439507,-0.097020 51.439467,-0.097313 51.439413,-0.097170 51.439113,-0.097555 51.439043,-0.097889 51.438972,-0.097673 51.438500,-0.097625 51.438401,-0.097605 51.438385,-0.097100 51.437322,-0.096857 51.436788,-0.095302 51.437125,-0.095525 51.437577,-0.094770 51.437739,-0.094792 51.437782,-0.094067 51.437912,-0.094003 51.437773,-0.093807 51.437811,-0.093649 51.437500,-0.092809 51.437654,-0.092610 51.437695,-0.092600 51.437703,-0.092597 51.437725,-0.092641 51.437823,-0.092684 51.437815,-0.092806 51.437988,-0.092791 51.437991,-0.092981 51.438322,-0.093016 51.438412,-0.093113 51.438598,-0.093307 51.438893,-0.093355 51.438979,-0.093475 51.439141,-0.093650 51.439435,-0.093671 51.439500,-0.093684 51.439602,-0.093737 51.439795,-0.093734 51.439936,-0.093840 51.440200,-0.093908 51.440338,-0.094363 51.440240,-0.094430 51.440375,-0.094539 51.440354)))
7002001662||2021-12-27|||tree|||tree|1662|geography||POINT (-0.094959 51.439306)|
7002001663||2021-12-27|||tree|||tree|1663|geography||POINT (-0.094987 51.439363)|
7002001664||2021-12-27|||tree|||tree|1664|geography||POINT (-0.094894 51.439378)|
101|Dacorum Borough Council|2022-06-06|||local-authority||600001|local-authority|DAC|organisation|||
44002322|Historic England|2022-04-12|||conservation-area|||conservation-area|1|geography||POINT (-2.246357 50.91254)|

0 comments on commit 1e79a3b

Please sign in to comment.