Note
Click here to download the full example code
Reproject a Raster using ST_TransformΒΆ
The ST_Transform() function (and a few others like ST_SnapToGrid()) can be used on both Geometry and Raster types. In GeoAlchemy2, this function is only defined for Geometry as it can not be defined for several types at the same time. Thus using this function on Raster requires minor tweaking.
This example uses both SQLAlchemy core and ORM queries.
12 from sqlalchemy import Column
13 from sqlalchemy import Integer
14 from sqlalchemy import MetaData
15 from sqlalchemy import Table
16 from sqlalchemy import func
17 from sqlalchemy.orm import Query
18 from sqlalchemy.orm import declarative_base
19
20 from geoalchemy2 import Geometry
21 from geoalchemy2 import Raster
22
23 # Tests imports
24 from tests import select
25
26 metadata = MetaData()
27 Base = declarative_base(metadata=metadata)
28
29 table = Table(
30 "raster_table",
31 metadata,
32 Column("id", Integer, primary_key=True),
33 Column("geom", Geometry("POLYGON", 4326)),
34 Column("rast", Raster()),
35 )
36
37
38 class RasterTable(Base): # type: ignore
39 __tablename__ = "raster_table_orm"
40 id = Column(Integer, primary_key=True)
41 geom = Column(Geometry("POLYGON", 4326))
42 rast = Column(Raster())
43
44 def __init__(self, rast):
45 self.rast = rast
46
47
48 def test_transform_core():
49 # Define the transform query for both the geometry and the raster in a naive way
50 wrong_query = select(
51 [func.ST_Transform(table.c.geom, 2154), func.ST_Transform(table.c.rast, 2154)]
52 )
53
54 # Check the query
55 assert str(wrong_query) == (
56 "SELECT "
57 "ST_AsEWKB("
58 'ST_Transform(raster_table.geom, :ST_Transform_2)) AS "ST_Transform_1", '
59 "ST_AsEWKB(" # <= Note that the raster is processed as a Geometry here
60 'ST_Transform(raster_table.rast, :ST_Transform_4)) AS "ST_Transform_3" \n'
61 "FROM raster_table"
62 )
63
64 # Define the transform query for both the geometry and the raster in the correct way
65 correct_query = select(
66 [
67 func.ST_Transform(table.c.geom, 2154),
68 func.ST_Transform(table.c.rast, 2154, type_=Raster),
69 ]
70 )
71
72 # Check the query
73 assert str(correct_query) == (
74 "SELECT "
75 "ST_AsEWKB("
76 'ST_Transform(raster_table.geom, :ST_Transform_2)) AS "ST_Transform_1", '
77 "raster(" # <= This time the raster is correctly processed as a Raster
78 'ST_Transform(raster_table.rast, :ST_Transform_4)) AS "ST_Transform_3" \n'
79 "FROM raster_table"
80 )
81
82
83 def test_transform_ORM():
84 # Define the transform query for both the geometry and the raster in a naive way
85 wrong_query = Query([RasterTable.geom.ST_Transform(2154), RasterTable.rast.ST_Transform(2154)])
86
87 # Check the query
88 assert str(wrong_query) == (
89 "SELECT "
90 "ST_AsEWKB("
91 'ST_Transform(raster_table_orm.geom, :ST_Transform_2)) AS "ST_Transform_1", '
92 "ST_AsEWKB(" # <= Note that the raster is processed as a Geometry here
93 'ST_Transform(raster_table_orm.rast, :ST_Transform_4)) AS "ST_Transform_3" \n'
94 "FROM raster_table_orm"
95 )
96
97 # Define the transform query for both the geometry and the raster in the correct way
98 correct_query = Query(
99 [
100 RasterTable.geom.ST_Transform(2154),
101 RasterTable.rast.ST_Transform(2154, type_=Raster),
102 ]
103 )
104
105 # Check the query
106 assert str(correct_query) == (
107 "SELECT "
108 "ST_AsEWKB("
109 'ST_Transform(raster_table_orm.geom, :ST_Transform_2)) AS "ST_Transform_1", '
110 "raster(" # <= This time the raster is correctly processed as a Raster
111 'ST_Transform(raster_table_orm.rast, :ST_Transform_4)) AS "ST_Transform_3" \n'
112 "FROM raster_table_orm"
113 )