-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from SkyTruth/feature-optimize-database-inserts
Feature optimize database inserts
- Loading branch information
Showing
10 changed files
with
246 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
alembic/versions/c7c033c1cdb5_populate_aoi_chunks_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Populate aoi_chunks table | ||
Revision ID: c7c033c1cdb5 | ||
Revises: 3736e85bc273 | ||
Create Date: 2023-11-17 11:44:59.370910 | ||
""" | ||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "c7c033c1cdb5" | ||
down_revision = "3736e85bc273" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
""" | ||
Upgrade the database schema. | ||
This function subdivides the geometries in the 'aoi' table into smaller chunks | ||
if they have more than 255 points, and inserts these chunks into the 'aoi_chunks' table. | ||
This is done to manage large geometries more efficiently. | ||
""" | ||
bind = op.get_bind() | ||
session = orm.Session(bind=bind) | ||
with session.begin(): | ||
session.execute( | ||
sa.text( | ||
""" | ||
WITH dumped AS ( | ||
SELECT aoi.id, (st_dump(st_makevalid(st_buffer(geometry::geometry,0)))).geom as dgeom | ||
FROM aoi | ||
), split as( | ||
SELECT id, st_subdivide(dgeom) as dgeom FROM dumped WHERE st_npoints(dgeom)>255 | ||
UNION ALL | ||
SELECT id, dgeom FROM dumped WHERE st_npoints(dgeom)<=255 | ||
) | ||
INSERT INTO aoi_chunks (id, geometry) | ||
SELECT id, dgeom | ||
FROM split; | ||
""" | ||
) | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
""" | ||
Downgrade the database schema. | ||
This function clears the 'aoi_chunks' table by truncating it, effectively | ||
undoing the changes made by the upgrade function. | ||
""" | ||
bind = op.get_bind() | ||
session = orm.Session(bind=bind) | ||
with session.begin(): | ||
session.execute(sa.text("TRUNCATE TABLE aoi_chunks;")) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.