Exemple #1
0
def update_idea(user: directives.user, id: hug.types.text, body: IdeaSchema()) -> IdeaSchema():
    idea = Idea.objects(user=user, id=bson.ObjectId(id)).first()
    if not idea:
        raise hug.HTTPNotFound()
    result = IdeaSchema().update(idea, IdeaSchema().dump(body).data)
    result.data.save()
    return result.data
Exemple #2
0
def geometry(conn: directive.connection,
             tables: directive.tables,
             locale: directive.locale,
             oid: hug.types.number,
             geomtype: hug.types.one_of(('geojson', 'kml', 'gpx')),
             simplify: int = None):
    """ Return the geometry of the function. Supported formats are geojson,
        kml and gpx.
    """
    r = tables.routes.data

    geom = r.c.geom
    if simplify is not None:
        geom = geom.ST_Simplify(r.c.geom.ST_NPoints() / int(simplify))
    if geomtype == 'geojson':
        geom = geom.ST_AsGeoJSON()
    else:
        geom = geom.ST_Transform(4326)

    rows = [r.c.name, r.c.intnames, r.c.ref, r.c.id, geom.label('geom')]

    obj = conn.execute(sa.select(rows).where(r.c.id == oid)).first()

    if obj is None:
        raise hug.HTTPNotFound()

    return RouteGeometry(obj, locales=locale, fmt=geomtype)
def geometry(conn: directive.connection,
             tables: directive.tables,
             locale: directive.locale,
             oid: hug.types.number,
             geomtype: hug.types.one_of(('geojson', 'kml', 'gpx')),
             simplify: int = None):
    """ Return the geometry of the way. Supported formats are geojson,
        kml and gpx.
    """
    w = tables.ways.data
    ws = tables.joined_ways.data

    geom = gf.ST_LineMerge(gf.ST_Collect(w.c.geom))

    if simplify is not None:
        geom = geom.ST_Simplify(r.c.geom.ST_NPoints() / int(simplify))

    if geomtype == 'geojson':
        geom = geom.ST_AsGeoJSON()
    else:
        geom = geom.ST_Transform(4326)

    sql = sa.select([w.c.name, w.c.intnames, w.c.ref, ws.c.id, geom.label('geom')])\
            .where(w.c.id == ws.c.child)\
            .where(ws.c.id == oid)\
            .group_by(w.c.name, w.c.intnames, w.c.ref, ws.c.id)

    obj = conn.execute(sql).first()

    if obj is None:
        raise hug.HTTPNotFound()

    return RouteGeometry(obj, locales=locale, fmt=geomtype)
Exemple #4
0
def from_tags(style: str, factory: shield_factory,
              **kwargs) -> 'SVG image of a shield':
    """ Create a route shield from a set of OSM tags. The tag list must be
        given as keyword parameters."""
    sym = factory.create(kwargs, '', style=style)
    if sym is None:
        raise hug.HTTPNotFound()

    return sym
Exemple #5
0
def uuid(symbol: str, cfg: db_config):
    """ Retrive a symbol SVG by its ID. These are the IDs returned by the API
        for the routes."""
    if not '.' in symbol:
        symbol += '.svg'
    filename = osp.join(cfg.ROUTES.symbol_datadir, symbol)

    if not osp.exists(filename):
        raise hug.HTTPNotFound()

    return filename
Exemple #6
0
def elevation(conn: directive.connection,
              tables: directive.tables,
              dem: directive.dem_file,
              oid: hug.types.number,
              segments: hug.types.in_range(1, 500) = 100):
    "Return the elevation profile of the way."

    if dem is None:
        raise hug.HTTPNotFound()

    r = tables.ways.data

    sel = sa.select([
        gf.ST_Points(
            gf.ST_Collect(
                gf.ST_PointN(r.c.geom, 1),
                gf.ST_LineInterpolatePoints(r.c.geom, 1.0 / segments))),
        sa.func.ST_Length2dSpheroid(
            gf.ST_Transform(r.c.geom, 4326),
            'SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]'
        )
    ]).where(r.c.id == oid)

    res = conn.execute(sel).first()

    if res is None:
        raise hug.HTTPNotFound()

    geom = to_shape(res[0])
    ele = RouteElevation(oid, dem, geom.bounds)

    xcoord, ycoord = zip(*((p.x, p.y) for p in geom))
    geomlen = res[1]
    pos = [geomlen * i / float(segments) for i in range(segments + 1)]

    ele.add_segment(xcoord, ycoord, pos)

    return ele.as_dict()
Exemple #7
0
def info(conn: directive.connection, tables: directive.tables,
         osmdata: directive.osmdata, locale: directive.locale,
         oid: hug.types.number):
    "Return general information about the way."

    r = tables.ways.data
    o = osmdata.way.data

    sql = sa.select(DetailedRouteItem.make_selectables(r, o))\
                            .where(r.c.id==oid)\
                            .where(o.c.id==oid)

    row = conn.execute(sql).first()

    if row is None:
        raise hug.HTTPNotFound()

    return DetailedRouteItem(row, locale, objtype='way')
def info(conn: directive.connection, tables: directive.tables,
         osmdata: directive.osmdata, locale: directive.locale,
         oid: hug.types.number):
    "Return general information about the guide post."

    r = tables.guideposts.data
    o = osmdata.node.data

    sql = sa.select(NodeItem.make_selectables(r, o))\
            .where(r.c.id == oid)\
            .where(o.c.id == oid)

    res = conn.execute(sql).first()

    if res is None:
        raise hug.HTTPNotFound()

    return NodeItem('guidepost', oid).add_row_data(res, locale)
Exemple #9
0
def info(conn: directive.connection, tables: directive.tables,
         osmdata: directive.osmdata, locale: directive.locale,
         oid: hug.types.number):
    "Return general information about the route."

    r = tables.routes.data
    o = osmdata.relation.data
    h = tables.hierarchy.data

    sql = sa.select(DetailedRouteItem.make_selectables(r, o))\
                            .where(r.c.id==oid)\
                            .where(o.c.id==oid)

    row = conn.execute(sql).first()

    if row is None:
        raise hug.HTTPNotFound()

    res = DetailedRouteItem(row, locale, objtype='relation')

    # add hierarchy where applicable
    for rtype in ('subroutes', 'superroutes'):
        if rtype == 'subroutes':
            w = sa.select([h.c.child], distinct=True)\
                    .where(h.c.parent == oid).where(h.c.depth == 2)
        else:
            w = sa.select([h.c.parent], distinct=True)\
                     .where(h.c.child == oid).where(h.c.depth == 2)

        sections = conn.execute(sa.select(RouteItem.make_selectables(r))\
                                 .where(r.c.id != oid).where(r.c.id.in_(w)))

        if sections.rowcount > 0:
            res.add_extra_info(rtype, [RouteItem(s) for s in sections])

    return res
def info(conn: directive.connection, tables: directive.tables,
         osmdata: directive.osmdata, locale: directive.locale,
         oid: hug.types.number):
    "Return general information about the way."

    r = tables.ways.data
    o = osmdata.way.data
    ws = tables.joined_ways.data

    w = tables.ways.data
    geom = sa.select([gf.ST_Collect(w.c.geom).label('geom')])\
             .where(ws.c.id == oid)\
             .where(w.c.id == ws.c.child)\
             .alias()

    fields = [
        r.c.id, r.c.name, r.c.intnames, r.c.symbol, r.c.ref, r.c.piste,
        o.c.tags, geom
    ]

    sql = sa.select(fields).where(r.c.id == oid).where(o.c.id == oid).alias()

    fields = list(sql.c)
    fields.append(
        sa.func.ST_Length2dSpheroid(
            gf.ST_Transform(sql.c.geom, 4326),
            'SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]'
        ).label("length"))
    fields.append(sql.c.geom.ST_Envelope().label('bbox'))

    row = conn.execute(sa.select(fields)).first()

    if row is None:
        raise hug.HTTPNotFound()

    return DetailedRouteItem(row, locale, objtype='wayset')
Exemple #11
0
 def my_endpoint2(hug_api):
     raise hug.HTTPNotFound()
Exemple #12
0
def elevation(conn: directive.connection,
              tables: directive.tables,
              dem: directive.dem_file,
              oid: hug.types.number,
              segments: hug.types.in_range(1, 500) = 100):
    "Return the elevation profile of the route."

    if dem is None:
        raise hug.HTTPNotFound()

    r = tables.routes.data

    sel = sa.select([gf.ST_Points(gf.ST_Collect(
                         gf.ST_PointN(r.c.geom, 1),
                         gf.ST_LineInterpolatePoints(r.c.geom, 1.0/segments))),
                     sa.func.ST_Length2dSpheroid(gf.ST_Transform(r.c.geom, 4326),
                           'SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]')
                    ]).where(r.c.id == oid)\
            .where(r.c.geom.ST_GeometryType() == 'ST_LineString')

    res = conn.execute(sel).first()

    if res is not None:
        geom = to_shape(res[0])
        ele = RouteElevation(oid, dem, geom.bounds)

        xcoord, ycoord = zip(*((p.x, p.y) for p in geom))
        geomlen = res[1]
        pos = [geomlen * i / float(segments) for i in range(segments + 1)]

        ele.add_segment(xcoord, ycoord, pos)

        return ele.as_dict()

    # special treatment for multilinestrings
    sel = sa.select([r.c.geom,
                     sa.literal_column("""ST_Length2dSpheroid(ST_MakeLine(ARRAY[ST_Points(ST_Transform(geom,4326))]),
                             'SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY["EPSG",\"7030\"]]')"""),
                     r.c.geom.ST_NPoints(),
                     gf.ST_Length(r.c.geom)])\
                .where(r.c.id == oid)

    res = conn.execute(sel).first()

    if res is None or res[0] is None:
        raise hug.HTTPNotFound()

    geom = to_shape(res[0])
    # Computing length in Mercator is slightly off, correct it via the
    # actual length.
    dist_fac = res[1] / res[3]
    ele = RouteElevation(oid, dem, geom.bounds)

    if res[2] > 10000:
        geom = geom.simplify(res[2] / 500, preserve_topology=False)
    elif res[2] > 4000:
        geom = geom.simplify(res[2] / 1000, preserve_topology=False)

    prev = None
    for seg in geom:
        p = seg.coords[0]
        xcoords = array('d', [p[0]])
        ycoords = array('d', [p[1]])
        pos = array('d')
        if prev is not None:
            pos.append(prev[2][-1] + \
                    Point(prev[0][-1], prev[1][-1]).distance(Point(*p)) * dist_fac)
        else:
            pos.append(0.0)
        for p in seg.coords[1:]:
            pos.append(pos[-1] +
                       Point(xcoords[-1], ycoords[-1]).distance(Point(*p)) *
                       dist_fac)
            xcoords.append(p[0])
            ycoords.append(p[1])

        ele.add_segment(xcoords, ycoords, pos)
        prev = (xcoords, ycoords, pos)

    return ele.as_dict()
def elevation(conn: directive.connection,
              tables: directive.tables,
              dem: directive.dem_file,
              oid: hug.types.number,
              segments: hug.types.in_range(1, 500) = 100):
    "Return the elevation profile of the way."

    if dem is None:
        raise hug.HTTPNotFound()

    w = tables.ways.data
    ws = tables.joined_ways.data

    sql = sa.select([gf.ST_LineMerge(gf.ST_Collect(w.c.geom)).label('geom')])\
            .where(w.c.id == ws.c.child)\
            .where(ws.c.id == oid)\
            .alias()

    sel = sa.select([
        sql.c.geom,
        sa.literal_column(
            """ST_Length2dSpheroid(ST_MakeLine(ARRAY[ST_Points(ST_Transform(geom,4326))]),
                             'SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY["EPSG",\"7030\"]]')"""
        ),
        sql.c.geom.ST_NPoints(),
        gf.ST_Length(sql.c.geom)
    ])

    res = conn.execute(sel).first()

    if res is None or res[0] is None:
        raise hug.HTTPNotFound()

    geom = to_shape(res[0])
    # Computing length in Mercator is slightly off, correct it via the
    # actual length.
    dist_fac = res[1] / res[3]

    ele = RouteElevation(oid, dem, geom.bounds)

    if res[2] > 10000:
        geom = geom.simplify(res[2] / 500, preserve_topology=False)
    elif res[2] > 4000:
        geom = geom.simplify(res[2] / 1000, preserve_topology=False)

    if geom.geom_type == 'LineString':
        geom = [geom]

    prev = None
    for seg in geom:
        p = seg.coords[0]
        xcoords = array('d', [p[0]])
        ycoords = array('d', [p[1]])
        pos = array('d')
        if prev is not None:
            pos.append(prev[2][-1] + \
                    Point(prev[0][-1], prev[1][-1]).distance(Point(*p)) * dist_fac)
        else:
            pos.append(0.0)
        for p in seg.coords[1:]:
            pos.append(pos[-1] +
                       Point(xcoords[-1], ycoords[-1]).distance(Point(*p)) *
                       dist_fac)
            xcoords.append(p[0])
            ycoords.append(p[1])

        ele.add_segment(xcoords, ycoords, pos)
        prev = (xcoords, ycoords, pos)

    return ele.as_dict()
Exemple #14
0
def delete_idea(user: directives.user, id: hug.types.text):
    idea = Idea.objects(user=user, id=bson.ObjectId(id)).first()
    if not idea:
        raise hug.HTTPNotFound()
    idea.delete()