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.ext.declarative import declarative_base
18 from sqlalchemy.orm import Query
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):
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),
52 func.ST_Transform(table.c.rast, 2154)
53 ])
54
55 # Check the query
56 assert str(wrong_query) == (
57 "SELECT "
58 "ST_AsEWKB("
59 "ST_Transform(raster_table.geom, :ST_Transform_2)) AS \"ST_Transform_1\", "
60 "ST_AsEWKB(" # <= Note that the raster is processed as a Geometry here
61 "ST_Transform(raster_table.rast, :ST_Transform_4)) AS \"ST_Transform_3\" \n"
62 "FROM raster_table"
63 )
64
65 # Define the transform query for both the geometry and the raster in the correct way
66 correct_query = select([
67 func.ST_Transform(table.c.geom, 2154),
68 func.ST_Transform(table.c.rast, 2154, type_=Raster)
69 ])
70
71 # Check the query
72 assert str(correct_query) == (
73 "SELECT "
74 "ST_AsEWKB("
75 "ST_Transform(raster_table.geom, :ST_Transform_2)) AS \"ST_Transform_1\", "
76 "raster(" # <= This time the raster is correctly processed as a Raster
77 "ST_Transform(raster_table.rast, :ST_Transform_4)) AS \"ST_Transform_3\" \n"
78 "FROM raster_table"
79 )
80
81
82 def test_transform_ORM():
83 # Define the transform query for both the geometry and the raster in a naive way
84 wrong_query = Query([
85 RasterTable.geom.ST_Transform(2154),
86 RasterTable.rast.ST_Transform(2154)
87 ])
88
89 # Check the query
90 assert str(wrong_query) == (
91 "SELECT "
92 "ST_AsEWKB("
93 "ST_Transform(raster_table_orm.geom, :ST_Transform_2)) AS \"ST_Transform_1\", "
94 "ST_AsEWKB(" # <= Note that the raster is processed as a Geometry here
95 "ST_Transform(raster_table_orm.rast, :ST_Transform_4)) AS \"ST_Transform_3\" \n"
96 "FROM raster_table_orm"
97 )
98
99 # Define the transform query for both the geometry and the raster in the correct way
100 correct_query = Query([
101 RasterTable.geom.ST_Transform(2154),
102 RasterTable.rast.ST_Transform(2154, type_=Raster)
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 )