def build_geometry(self, osmid): # find all relation parts h = self.hierarchy_table.data parts = select([h.c.child]).where(h.c.parent == osmid) # stick them together s = self.segment_table.data sel = select([func.st_linemerge(func.st_collect(s.c.geom))])\ .where(s.c.rels.op('&& ARRAY')(parts)) return self.thread.conn.scalar(sel)
def transform_tags(self, osmid, tags): outtags, difficulty = _basic_tag_transform(osmid, tags) # we don't support hierarchy at the moment outtags['top'] = True # find all relation parts h = self.hierarchy_table.data parts = select([h.c.child]).where(h.c.parent == osmid) # get the geometry s = self.segment_table.data sel = select([func.st_linemerge(func.st_collect(s.c.geom))])\ .where(s.c.rels.op('&& ARRAY')(parts)) outtags['geom'] = self.thread.conn.scalar(sel) outtags['symbol'] = self.symbols.create_write(tags, '', difficulty) return outtags
import piecewise.config from sqlalchemy import MetaData, Table, Column, create_engine, func, select from sqlalchemy.sql.expression import label import json aggregator = piecewise.config.read_config( json.load(open("piecewise_config.json"))) spatial_bin = filter(lambda x: x.label == 'spatial_join', aggregator.bins)[0] engine = create_engine(aggregator.database_uri) metadata = MetaData() metadata.bind = engine geom = Column(spatial_bin.geometry_column) spatial_table = Table(spatial_bin.table, metadata, Column(spatial_bin.geometry_column)) import sys with engine.begin() as conn, open(sys.argv[1], 'w') as out: centroid = func.st_astext(func.st_centroid(func.st_collect(geom))) query = select([func.st_x(centroid), func.st_y(centroid)]).select_from(spatial_table) (x, y) = conn.execute(query).fetchone() out.write("var center = [{lat}, {lon}];".format(lat=y, lon=x))
import piecewise.config from sqlalchemy import MetaData, Table, Column, create_engine, func, select from sqlalchemy.sql.expression import label import json aggregator = piecewise.config.read_config(json.load(open("piecewise_config.json"))) spatial_bin = filter(lambda x: x.label == 'spatial_join', aggregator.bins)[0] engine = create_engine(aggregator.database_uri) metadata = MetaData() metadata.bind = engine geom = Column(spatial_bin.geometry_column) spatial_table = Table(spatial_bin.table, metadata, Column(spatial_bin.geometry_column)) import sys with engine.begin() as conn, open(sys.argv[1], 'w') as out: centroid = func.st_astext(func.st_centroid(func.st_collect(geom))) query = select([func.st_x(centroid), func.st_y(centroid)]).select_from(spatial_table) (x, y) = conn.execute(query).fetchone() out.write("var center = [{lat}, {lon}];".format(lat=y, lon=x))
def construct_stroke(road_section, junction, delimited_stroke, level=1): """Constructs a delimited stroke from road_section, with junction as its starting point. If a delimited_stroke is given as input, the next road_section is added to this delimited_stroke.""" session.flush() # the delimited stroke dictionary use here is based on type of the input stroke if type(delimited_stroke) == DelimitedStrokeRef: delimited_strokes = delimited_strokes_ref else: delimited_strokes = delimited_strokes_target road_section.delimited_stroke = delimited_stroke # determine which from which junction the next road section should be added if junction == road_section.begin_junction: next_junction = road_section.end_junction else: next_junction = road_section.begin_junction # if the next_junction is the same as the begin_junction of the delimited stroke, it is a loop and can be returned if next_junction.id == delimited_stroke.begin_junction_id: delimited_stroke.end_junction_id = next_junction.id return delimited_stroke # if the degree of the next junction is 2, the road section that is not equal to the input is added # to the delimited stroke if next_junction.degree == 2: for next_road_section in next_junction.road_sections: if next_road_section != road_section: delimited_stroke.geom = session.query( func.st_linemerge( func.st_collect(delimited_stroke.geom, next_road_section.geom))) delimited_strokes[delimited_stroke.id].append( next_road_section) return construct_stroke(next_road_section, next_junction, delimited_stroke, level) # only strokes at level 1, are extended with sections that have good continuity if level == 1: if next_junction.type_k3 == 2: current_angle = angle_at_junction(road_section, next_junction) if current_angle != next_junction.angle_k3: # select the next section that has good continuity for next_road_section in next_junction.road_sections: if next_road_section != road_section and next_road_section.delimited_stroke is None: next_angle = angle_at_junction(next_road_section, next_junction) if next_angle != next_junction.angle_k3: # if the next section is a loop, it is not added to the delimited stroke if next_road_section.begin_junction == next_road_section.end_junction: delimited_stroke.end_junction_id = next_junction.id return delimited_stroke # merge the selected section with the existing delimited stroke to extend it delimited_stroke.geom = session.query( func.st_linemerge( func.st_collect(delimited_stroke.geom, next_road_section.geom))) delimited_strokes[delimited_stroke.id].append( next_road_section) return construct_stroke(next_road_section, next_junction, delimited_stroke, level) # if the stroke is not extended, the end junction is set delimited_stroke.end_junction_id = next_junction.id return delimited_stroke