def _construct_row(self, obj, conn):
        tags = TagStore(obj['tags'])
        is_node_network = tags.get('network:type') == 'node_network'

        outtags = RouteRow(id=obj['id'],
                           name=tags.get('name'),
                           ref=tags.get('ref'),
                           intnames=tags.get_prefixed('name:'),
                           itinerary=make_itinerary(tags))

        if 'symbol' in tags:
            outtags.intnames['symbol'] = tags['symbol']

        if is_node_network:
            outtags.level = Network.LOC.min()
        elif 'network' in tags:
            outtags.level = self._compute_route_level(tags['network'])

        # child relations
        members, relids = self._filter_members(obj['id'], obj['members'])

        # geometry
        geom = make_geometry(conn, members, self.ways, self.data)

        if geom is None:
            return None

        # find the country
        outtags.country = self._find_country(relids, geom)

        # create the symbol
        outtags.symbol = self._write_symbol(
            tags, outtags.country,
            Network.from_int(outtags.level).name)

        # custom filter callback
        if self.config.tag_filter is not None:
            self.config.tag_filter(outtags, tags)

        if outtags.network is None and is_node_network:
            outtags.network = 'NDS'

        if 'network' in tags and not is_node_network:
            h = self.rtree.data
            r = self.rels.data
            sel = sa.select([sa.text("'a'")])\
                    .where(h.c.child == obj['id'])\
                    .where(r.c.id == h.c.parent)\
                    .where(h.c.depth == 2)\
                    .where(r.c.tags['network'].astext == tags['network'])\
                    .limit(1)

            top = self.thread.conn.scalar(sel)

            outtags.top = (top is None)

        outtags = dataclasses.asdict(outtags)
        outtags['geom'] = geom

        return outtags
def basic_tag_transform(tags: TagStore, config):
    difficulty = tags.get('piste:difficulty')
    difficulty = config.difficulty_map.get(difficulty, 0)

    return dict(intnames=tags.get_prefixed('name:'),
                name=tags.firstof('piste:name', 'name'),
                ref=tags.firstof('piste:ref', 'ref'),
                difficulty=difficulty,
                piste=config.piste_type.get(tags.get('piste:type'), 0))
    def transform(self, obj):
        tags = TagStore(obj['tags'])
        # filter by subtype
        if self.config.subtype is not None:
            booltags = tags.get_booleans()
            if len(booltags) > 0:
                if not booltags.get(self.config.subtype, False):
                    return None
            else:
                if self.config.require_subtype:
                    return None

        outtags = {'name': tags.get('name'), 'ele': None}
        if 'ele' in tags:
            m = self.elepattern.search(tags['ele'])
            if m:
                outtags['ele'] = m.group(0)
            # XXX check for ft

        if self.srid == self.src.c.geom.type.srid:
            outtags['geom'] = obj['geom']
        else:
            outtags['geom'] = obj['geom'].ST_Transform(self.srid)

        return outtags
    def _construct_row(self, obj, conn):
        tags = TagStore(obj['tags'])
        outtags = RouteRow(obj['id'])

        # determine name and level
        for k, v in tags.items():
            if k in ('name', 'ref'):
                outtags[k] = v
            elif k.startswith('name:'):
                outtags.intnames[k[5:]] = v
            elif k == 'network':
                outtags.level = self._compute_route_level(v)

        if tags.get('network:type') == 'node_network':
            outtags.level = Network.LOC.min()

        # child relations
        relids = [ r['id'] for r in obj['members'] if r['type'] == 'R']

        members = obj['members']
        if len(relids) > 0:
            # Is this relation part of a cycle? Then drop the relation members
            # to not get us in trouble with geometry building.
            h1 = self.rtree.data.alias()
            h2 = self.rtree.data.alias()
            sql = sa.select([h1.c.parent])\
                    .where(h1.c.parent == obj['id'])\
                    .where(h1.c.child == h2.c.parent)\
                    .where(h2.c.child == obj['id'])
            if (self.thread.conn.execute(sql).rowcount > 0):
                members = [ m for m in obj['members'] if m['type'] == 'W' ]
                relids = []

        # geometry
        geom = build_route_geometry(conn, members, self.ways, self.data)

        if geom is None:
            return None

        if geom.geom_type not in ('MultiLineString', 'LineString'):
            raise RuntimeError("Bad geometry %s for %d" % (geom.geom_type, obj['id']))

        # if the route is unsorted but linear, sort it
        if geom.geom_type == 'MultiLineString':
            fixed_geom = linemerge(geom)
            if fixed_geom.geom_type == 'LineString':
                geom = fixed_geom

        outtags.geom = from_shape(geom, srid=self.data.c.geom.type.srid)

        # find the country
        if len(relids) > 0:
            sel = sa.select([self.c.country], distinct=True)\
                    .where(self.c.id.in_(relids))
        else:
            c = self.countries
            sel = sa.select([c.column_cc()], distinct=True)\
                    .where(c.column_geom().ST_Intersects(outtags.geom))

        cur = self.thread.conn.execute(sel)

        # should be counting when rowcount > 1
        if cur.rowcount >= 1:
            cntry = cur.scalar()
        else:
            cntry = None

        outtags.country = cntry

        sym = self.symbols.create(tags, cntry,
                                  style=Network.from_int(outtags.level).name)
        if sym is None:
            outtags.symbol = 'None'
        else:
            outtags.symbol = sym.uuid()
            sym.to_file(os.path.join(self.config.symbol_datadir,
                                     outtags.symbol + '.svg'), format='svg')

        # custom filter callback
        if self.config.tag_filter is not None:
            self.config.tag_filter(outtags, tags)

        if outtags.network is None:
            if tags.get('network:type') == 'node_network':
                outtags.network = 'NDS'

        if outtags.top is None:
            if 'network' in tags and tags.get('network:type') != 'node_network':
                h = self.rtree.data
                r = self.rels.data
                sel = sa.select([sa.text("'a'")]).where(h.c.child == obj['id'])\
                                         .where(r.c.id == h.c.parent)\
                                         .where(h.c.depth == 2)\
                                         .where(r.c.tags['network'].astext == tags['network'])\
                                         .limit(1)

                top = self.thread.conn.scalar(sel)

                outtags.top = (top is None)
            else:
                outtags.top = True

        return outtags