Skip to content

Instantly share code, notes, and snippets.

@niklasfi
Last active February 10, 2016 13:33
Show Gist options
  • Save niklasfi/2c9be00f069e9cb88170 to your computer and use it in GitHub Desktop.
Save niklasfi/2c9be00f069e9cb88170 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
engine = sqlalchemy.create_engine('postgresql://osm:@/osm')
Base = declarative_base()
class Coord_Way(Base):
__tablename__ = 'coord_way'
coord_osmid = sqlalchemy.Column(sqlalchemy.BigInteger, sqlalchemy.ForeignKey('coord.osmid'), primary_key=True)
way_osmid = sqlalchemy.Column(sqlalchemy.BigInteger, sqlalchemy.ForeignKey('way.osmid'), primary_key=True)
index = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
way = sqlalchemy.orm.relationship('Way', back_populates='coords')
coord = sqlalchemy.orm.relationship('Coord', back_populates='ways')
class Coord(Base):
__tablename__ = 'coord'
osmid = sqlalchemy.Column(sqlalchemy.BigInteger, primary_key=True)
lon = sqlalchemy.Column(sqlalchemy.Float)
lat = sqlalchemy.Column(sqlalchemy.Float)
ways = sqlalchemy.orm.relationship('Coord_Way', back_populates='coord')
class Way(Base):
__tablename__ = 'way'
osmid = sqlalchemy.Column(sqlalchemy.BigInteger, primary_key=True)
coords = sqlalchemy.orm.relationship('Coord_Way', back_populates='way')
#Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
Session = sqlalchemy.orm.sessionmaker(bind=engine)
osm=> \d coord_way
Table "public.coord_way"
Column | Type | Modifiers
-------------+---------+-----------------------------------------------------------
coord_osmid | bigint | not null
way_osmid | bigint | not null
index | integer | not null default nextval('coord_way_index_seq'::regclass)
Indexes:
"coord_way_pkey" PRIMARY KEY, btree (coord_osmid, way_osmid, index)
Foreign-key constraints:
"coord_way_coord_osmid_fkey" FOREIGN KEY (coord_osmid) REFERENCES coord(osmid)
"coord_way_way_osmid_fkey" FOREIGN KEY (way_osmid) REFERENCES way(osmid)
osm=> \d coord
Table "public.coord"
Column | Type | Modifiers
--------+------------------+-------------------------------------------------------
osmid | bigint | not null default nextval('coord_osmid_seq'::regclass)
lon | double precision |
lat | double precision |
Indexes:
"coord_pkey" PRIMARY KEY, btree (osmid)
Referenced by:
TABLE "coord_way" CONSTRAINT "coord_way_coord_osmid_fkey" FOREIGN KEY (coord_osmid) REFERENCES coord(osmid)
osm=> select coord.lat, coord.lon from coord_way join coord on coord.osmid = coord_way.coord_osmid where coord_way.way_osmid = 384003068 order by coord_way.index;
lat | lon
------------------+------------
52.0003068000003 | 13.5857798
52.0003264000001 | 13.5859906
52.0010553000001 | 13.5915687
52.0037425 | 13.5907274
52.0043408000001 | 13.5904935
(5 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment