make sure we generate the right geometry column type and stay with the types we had from osm2psql (and thus compatible with older installations).

I believe retrofitting the migrations is OK as these were overwritten by osm2pgsql in the past anyhow. Now at least we create the schema everyone is using already.
This commit is contained in:
gluap 2023-03-29 23:35:23 +02:00 committed by Paul Bienkowski
parent 8ce5816f53
commit 10f6b0c0c9
3 changed files with 47 additions and 14 deletions

View file

@ -22,14 +22,15 @@ def upgrade():
op.create_table( op.create_table(
"road", "road",
sa.Column( sa.Column(
"way_id", sa.BIGINT, autoincrement=True, primary_key=True, index=True "way_id", sa.BIGINT, primary_key=True, index=True, autoincrement=False
), ),
sa.Column("zone", dbtype("zone_type")), sa.Column("zone", dbtype("zone_type")),
sa.Column("name", sa.String), sa.Column("name", sa.Text),
sa.Column("geometry", dbtype("GEOMETRY"), index=True), sa.Column("geometry", dbtype("geometry(LINESTRING,3857)")),
sa.Column("directionality", sa.Integer), sa.Column("directionality", sa.Integer),
sa.Column("oneway", sa.Boolean), sa.Column("oneway", sa.Boolean),
) )
op.execute('CREATE INDEX ix_road_geometry ON road USING GIST (geometry) WITH (FILLFACTOR=100);')
def downgrade(): def downgrade():

View file

@ -22,13 +22,14 @@ def upgrade():
op.create_table( op.create_table(
"region", "region",
sa.Column( sa.Column(
"relation_id", sa.BIGINT, autoincrement=True, primary_key=True, index=True "relation_id", sa.BIGINT, primary_key=True, index=True, autoincrement=False
), ),
sa.Column("name", sa.String), sa.Column("name", sa.Text),
sa.Column("geometry", dbtype("GEOMETRY"), index=True), sa.Column("geometry", dbtype("GEOMETRY(GEOMETRY,3857)"), index=False),
sa.Column("admin_level", sa.Integer, index=True), sa.Column("admin_level", sa.Integer, index=True),
sa.Column("tags", dbtype("HSTORE")), sa.Column("tags", dbtype("HSTORE")),
) )
op.execute('CREATE INDEX ix_region_geometry ON region USING GIST (geometry) WITH (FILLFACTOR=100);')
def downgrade(): def downgrade():

View file

@ -34,6 +34,7 @@ from sqlalchemy import (
select, select,
text, text,
literal, literal,
Text
) )
from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.dialects.postgresql import UUID
@ -107,6 +108,28 @@ class Geometry(UserDefinedType):
return func.ST_AsGeoJSON(func.ST_Transform(col, 4326), type_=self) return func.ST_AsGeoJSON(func.ST_Transform(col, 4326), type_=self)
class LineString(UserDefinedType):
def get_col_spec(self):
return "geometry(LineString, 3857)"
def bind_expression(self, bindvalue):
return func.ST_GeomFromGeoJSON(bindvalue, type_=self)
def column_expression(self, col):
return func.ST_AsGeoJSON(func.ST_Transform(col, 4326), type_=self)
class GeometryGeometry(UserDefinedType):
def get_col_spec(self):
return "geometry(GEOMETRY, 3857)"
def bind_expression(self, bindvalue):
return func.ST_GeomFromGeoJSON(bindvalue, type_=self)
def column_expression(self, col):
return func.ST_AsGeoJSON(func.ST_Transform(col, 4326), type_=self)
class OvertakingEvent(Base): class OvertakingEvent(Base):
__tablename__ = "overtaking_event" __tablename__ = "overtaking_event"
__table_args__ = (Index("road_segment", "way_id", "direction_reversed"),) __table_args__ = (Index("road_segment", "way_id", "direction_reversed"),)
@ -134,14 +157,18 @@ class OvertakingEvent(Base):
class Road(Base): class Road(Base):
__tablename__ = "road" __tablename__ = "road"
way_id = Column(BIGINT, primary_key=True, index=True) way_id = Column(BIGINT, primary_key=True, index=True, autoincrement=False)
zone = Column(ZoneType) zone = Column(ZoneType)
name = Column(String) name = Column(Text)
geometry = Column(Geometry) geometry = Column(LineString)
directionality = Column(Integer) directionality = Column(Integer)
oneway = Column(Boolean) oneway = Column(Boolean)
import_group = Column(String) import_group = Column(String)
__table_args__ = (
Index('ix_road_geometry', 'geometry', postgresql_using='gist', postgresql_with={'fillfactor':100}),
)
def to_dict(self): def to_dict(self):
return { return {
"way_id": self.way_id, "way_id": self.way_id,
@ -472,12 +499,16 @@ class Comment(Base):
class Region(Base): class Region(Base):
__tablename__ = "region" __tablename__ = "region"
relation_id = Column(BIGINT, primary_key=True, index=True) relation_id = Column(BIGINT, primary_key=True, index=True, autoincrement=False)
name = Column(String) name = Column(Text)
geometry = Column(Geometry) geometry = Column(GeometryGeometry)
admin_level = Column(Integer) admin_level = Column(Integer, index=True)
import_group = Column(String) import_group = Column(String)
__table_args__ = (
Index('ix_region_geometry', 'geometry', postgresql_using='gist', postgresql_with={'fillfactor':100}),
)
Comment.author = relationship("User", back_populates="authored_comments") Comment.author = relationship("User", back_populates="authored_comments")
User.authored_comments = relationship( User.authored_comments = relationship(