-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
206 lines (165 loc) · 6.86 KB
/
Copy pathmodels.py
File metadata and controls
206 lines (165 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""SQLAlchemy 2.0 models for a PostGIS schema.
Generated by geomodelgen from schema 'public'. Edits will be lost the next
time the generator runs; change the database (or the schema description) and
regenerate instead.
"""
from __future__ import annotations
import datetime
import decimal
import enum
import typing
import uuid
from typing import Optional
from geoalchemy2 import Geography, Geometry
from geoalchemy2.elements import WKBElement
from sqlalchemy import (
ARRAY,
BigInteger,
Boolean,
CheckConstraint,
Date,
DateTime,
Enum,
Float,
ForeignKey,
Index,
Integer,
Numeric,
SmallInteger,
String,
Text,
UniqueConstraint,
Uuid,
text,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
"""Declarative base shared by every generated model."""
class RoadSurface(enum.Enum):
"""PostgreSQL enum type 'road_surface'."""
asphalt = "asphalt"
concrete = "concrete"
gravel = "gravel"
unpaved = "unpaved"
class ServiceTier(enum.Enum):
"""PostgreSQL enum type 'service_tier'."""
standard = "standard"
priority = "priority"
emergency = "emergency"
class City(Base):
"""Populated places, one point per city centre."""
__tablename__ = "cities"
__table_args__ = (
Index("cities_geom_idx", "geom", postgresql_using="gist"),
Index("cities_name_idx", "name", postgresql_using="btree"),
UniqueConstraint("region_id", "name", name="cities_region_id_name_key"),
CheckConstraint("population >= 0", name="cities_population_check"),
{"comment": "Populated places, one point per city centre."},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
region_id: Mapped[Optional[int]] = mapped_column(
Integer, ForeignKey("regions.id", ondelete="SET NULL")
)
name: Mapped[str] = mapped_column(Text, nullable=False)
population: Mapped[Optional[int]] = mapped_column(Integer)
area_km2: Mapped[Optional[decimal.Decimal]] = mapped_column(Numeric(12, 3))
tags: Mapped[Optional[list[str]]] = mapped_column(ARRAY(Text))
properties: Mapped[Optional[dict[str, typing.Any]]] = mapped_column(
JSONB, server_default=text("'{}'::jsonb")
)
external_id: Mapped[Optional[uuid.UUID]] = mapped_column(Uuid)
geom: Mapped[WKBElement] = mapped_column(
Geometry(geometry_type="POINT", srid=4326, spatial_index=False),
nullable=False,
comment="City centre.",
)
updated_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default=text("now()")
)
region: Mapped[Optional["Region"]] = relationship("Region", back_populates="cities")
roads: Mapped[list["Road"]] = relationship("Road", back_populates="city")
class Region(Base):
"""Administrative regions used to group cities and service areas."""
__tablename__ = "regions"
__table_args__ = (
UniqueConstraint("code", name="regions_code_key"),
{"comment": "Administrative regions used to group cities and service areas."},
)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
code: Mapped[str] = mapped_column(
String(8), nullable=False, comment="ISO 3166-2 subdivision code."
)
name: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default=text("now()")
)
cities: Mapped[list["City"]] = relationship("City", back_populates="region")
service_areas: Mapped[list["ServiceArea"]] = relationship(
"ServiceArea", back_populates="region"
)
class Road(Base):
"""Road centrelines with a 3D geometry."""
__tablename__ = "roads"
__table_args__ = (
Index("roads_geom_idx", "geom", postgresql_using="gist"),
Index(
"roads_toll_geom_idx", "geom", postgresql_using="gist", postgresql_where=text("is_toll")
),
CheckConstraint("lanes > 0 AND lanes < 12", name="roads_lanes_check"),
{"comment": "Road centrelines with a 3D geometry."},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
city_id: Mapped[int] = mapped_column(
BigInteger, ForeignKey("cities.id", ondelete="CASCADE"), nullable=False
)
name: Mapped[Optional[str]] = mapped_column(Text)
surface: Mapped[RoadSurface] = mapped_column(
Enum(RoadSurface, name="road_surface", native_enum=True),
nullable=False,
server_default=text("'asphalt'::road_surface"),
)
lanes: Mapped[Optional[int]] = mapped_column(SmallInteger)
speed_limit_kph: Mapped[Optional[int]] = mapped_column(SmallInteger)
is_toll: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default=text("false"))
geom: Mapped[WKBElement] = mapped_column(
Geometry(geometry_type="LINESTRINGZ", srid=4326, dimension=3, spatial_index=False),
nullable=False,
)
city: Mapped["City"] = relationship("City", back_populates="roads")
class ServiceArea(Base):
"""Polygonal coverage areas, stored as geography for metre-accurate distance."""
__tablename__ = "service_areas"
__table_args__ = (
{"comment": "Polygonal coverage areas, stored as geography for metre-accurate distance."},
)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
region_id: Mapped[int] = mapped_column(
Integer, ForeignKey("regions.id", ondelete="CASCADE"), nullable=False
)
label: Mapped[str] = mapped_column(Text, nullable=False)
tier: Mapped[ServiceTier] = mapped_column(
Enum(ServiceTier, name="service_tier", native_enum=True),
nullable=False,
server_default=text("'standard'::service_tier"),
)
valid_from: Mapped[Optional[datetime.date]] = mapped_column(Date)
boundary: Mapped[WKBElement] = mapped_column(
Geography(geometry_type="POLYGON", srid=4326, spatial_index=True), nullable=False
)
region: Mapped["Region"] = relationship("Region", back_populates="service_areas")
class SurveyPoint(Base):
"""Raw survey captures; deliberately unindexed to show the missing-index warning."""
__tablename__ = "survey_points"
__table_args__ = (
{
"comment": "Raw survey captures; deliberately unindexed to show the missing-index warning."
},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
captured_at: Mapped[datetime.datetime] = mapped_column(DateTime(timezone=True), nullable=False)
accuracy_m: Mapped[Optional[float]] = mapped_column(Float)
location: Mapped[WKBElement] = mapped_column(
Geometry(geometry_type="POINTZM", srid=4326, dimension=4, spatial_index=False),
nullable=False,
) # no spatial index on this column in the source schema; see queries.py for the CREATE INDEX to run