Example #1
0
def get_paths_by_intersection(hike_name):
    # search database for paths by length
    ip_addr = get_ip_addr(request)
    curr_latlng = get_curr_loc(ip_addr)
    curr_latlng_point = latlng_to_point(curr_latlng)
    session = Session()
    try:
        curr_hike = session.query(Hike).filter_by(name=hike_name).first()
        hike_results = (session.query(Hike, Hike.path.ST_AsText()).filter(
            Hike.path.ST_Intersects(curr_hike.path),
            Hike.id != curr_hike.id).order_by(
                Hike.path.ST_Distance(curr_latlng_point)).all())
        if hike_results:
            for hike, path in hike_results[:min(MAX_RESULTS, len(hike_results)
                                                )]:
                hike.path_arr = linestring_to_latlngarr(path)
            colors = get_color_list(len(hike_results))
            return render_template(
                'search_results.html',
                hikes=[
                    hike for (hike, path) in
                    hike_results[:min(MAX_RESULTS, len(hike_results))]
                ],
                colors=colors,
                curr_latlng=curr_latlng)
        else:
            return 'No hikes intersect'
    except:
        return "bad hike name"
Example #2
0
def get_paths_by_intersection(hike_name):
    # search database for paths by length
    ip_addr = get_ip_addr(request)
    curr_latlng = get_curr_loc(ip_addr)
    curr_latlng_point = latlng_to_point(curr_latlng)
    session = Session()
    try:
        curr_hike = session.query(Hike).filter_by(name=hike_name).first()
        hike_results = (session
                        .query(Hike, Hike.path.ST_AsText())
                        .filter(Hike.path.ST_Intersects(curr_hike.path),
                                Hike.id != curr_hike.id)
                        .order_by(Hike.path.ST_Distance(curr_latlng_point))
                        .all())
        if hike_results:
            for hike, path in hike_results[:min(MAX_RESULTS, len(hike_results))]:
                hike.path_arr = linestring_to_latlngarr(path)
            colors = get_color_list(len(hike_results))
            return render_template('search_results.html',
                                   hikes=[hike for (hike, path) in hike_results[:min(MAX_RESULTS, len(hike_results))]],
                                   colors=colors,
                                   curr_latlng=curr_latlng)
        else:
            return 'No hikes intersect'
    except:
        return "bad hike name"
Example #3
0
def get_paths_bylength(min_max, length):
    # search database for paths by length near user location
    session = Session()

    # get user's latlng
    ip_addr = get_ip_addr(request)
    curr_latlng = get_curr_loc(ip_addr)
    curr_latlng_point = latlng_to_point(curr_latlng)
    length_in_units = miles_to_units(float(length))
    initial_query = session.query(Hike, Hike.path.ST_AsText())

    if min_max == "min":
        ## OLD VERSION
        # query_with_comparison = initial_query.filter(Hike.path.ST_Length() >= float(length))
        # ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),2163)
        query_with_comparison = initial_query.filter(Hike.path.ST_Transform(2163).ST_Length() >= length_in_units)
    elif min_max == "max":
        query_with_comparison = initial_query.filter(Hike.path.ST_Transform(2163).ST_Length() <= length_in_units)
    else:
        return "Bad min_max parameter: use 'min' or 'max'"
    hike_results = query_with_comparison.order_by(Hike.path.ST_Distance(curr_latlng_point)).all()

    # no results
    if len(hike_results) == 0:
        return "No results; modify search"
    for hike, path in hike_results:
        hike.path_arr = linestring_to_latlngarr(path) # json.dumps(linestring_to_latlngarr(path))
    colors = get_color_list(len(hike_results))
    return render_template('search_results.html',
                           hikes=[hike for (hike, path) in hike_results],
                           colors=colors,
                           curr_latlng=curr_latlng)
Example #4
0
def get_paths_by_location(latlng):
    #get latlng for searching
    search_latlng = url_latlng_to_dict(latlng)
    search_latlng_point = url_latlng_to_point(latlng)
    # search database for paths by length
    session = Session()
    hike_results = (session
                    .query(Hike, Hike.path.ST_AsText())
                    .order_by(Hike.path.ST_Distance(search_latlng_point))
                    .all())
    if hike_results:
        for hike, path in hike_results[:min(MAX_RESULTS, len(hike_results))]:
            hike.path_arr = linestring_to_latlngarr(path)  # json.dumps(linestring_to_latlngarr(path))
        colors = get_color_list(len(hike_results))
        return render_template('search_results.html',
                               hikes=[hike for (hike, path) in hike_results[:min(MAX_RESULTS, len(hike_results))]],
                               colors=colors,
                               curr_latlng=search_latlng)
    else:
        return 'Bad location'
Example #5
0
def get_paths_by_user(username):
    # get current location of user
    ip_addr = get_ip_addr(request)
    curr_latlng = get_curr_loc(ip_addr)
    curr_latlng_point = latlng_to_point(curr_latlng)
    # search database for paths entered by username
    session = Session()
    user = session.query(User).filter_by(name=username).first()
    if user:
        user_id = user.id
        hike_results = (session.query(
            Hike, Hike.path.ST_AsText()).filter_by(user_id=user_id).order_by(
                Hike.path.ST_Distance(curr_latlng_point)).all())
        for hike, path in hike_results:
            hike.path_arr = linestring_to_latlngarr(path)
        colors = get_color_list(len(hike_results))
        return render_template('search_results.html',
                               hikes=[hike for (hike, path) in hike_results],
                               colors=colors,
                               curr_latlng=curr_latlng)
    return "bad user"
Example #6
0
def get_my_paths():
    # get current location of user
    ip_addr = get_ip_addr(request)
    curr_latlng = get_curr_loc(ip_addr)
    curr_latlng_point = latlng_to_point(curr_latlng)
    # search database for paths entered by username
    session = Session()
    user = session.query(User).filter_by(id=flask_login.current_user.get_id()).first()
    if user:
        user_id = user.id
        hike_results = (session
                        .query(Hike, Hike.path.ST_AsText())
                        .filter_by(user_id=user_id)
                        .order_by(Hike.path.ST_Distance(curr_latlng_point))
                        .all())
        for hike, path in hike_results:
            hike.path_arr = linestring_to_latlngarr(path)
        colors = get_color_list(len(hike_results))
        return render_template('search_results.html', hikes=[hike for (hike, path) in hike_results], colors=colors,
                               curr_latlng=curr_latlng)
    return "Bad user"
Example #7
0
def get_paths_by_location(latlng):
    #get latlng for searching
    search_latlng = url_latlng_to_dict(latlng)
    search_latlng_point = url_latlng_to_point(latlng)
    # search database for paths by length
    session = Session()
    hike_results = (session.query(Hike, Hike.path.ST_AsText()).order_by(
        Hike.path.ST_Distance(search_latlng_point)).all())
    if hike_results:
        for hike, path in hike_results[:min(MAX_RESULTS, len(hike_results))]:
            hike.path_arr = linestring_to_latlngarr(
                path)  # json.dumps(linestring_to_latlngarr(path))
        colors = get_color_list(len(hike_results))
        return render_template(
            'search_results.html',
            hikes=[
                hike for (
                    hike,
                    path) in hike_results[:min(MAX_RESULTS, len(hike_results))]
            ],
            colors=colors,
            curr_latlng=search_latlng)
    else:
        return 'Bad location'
Example #8
0
def get_paths_bylength(min_max, length):
    # search database for paths by length near user location
    session = Session()

    # get user's latlng
    ip_addr = get_ip_addr(request)
    curr_latlng = get_curr_loc(ip_addr)
    curr_latlng_point = latlng_to_point(curr_latlng)
    length_in_units = miles_to_units(float(length))
    initial_query = session.query(Hike, Hike.path.ST_AsText())

    if min_max == "min":
        ## OLD VERSION
        # query_with_comparison = initial_query.filter(Hike.path.ST_Length() >= float(length))
        # ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),2163)
        query_with_comparison = initial_query.filter(
            Hike.path.ST_Transform(2163).ST_Length() >= length_in_units)
    elif min_max == "max":
        query_with_comparison = initial_query.filter(
            Hike.path.ST_Transform(2163).ST_Length() <= length_in_units)
    else:
        return "Bad min_max parameter: use 'min' or 'max'"
    hike_results = query_with_comparison.order_by(
        Hike.path.ST_Distance(curr_latlng_point)).all()

    # no results
    if len(hike_results) == 0:
        return "No results; modify search"
    for hike, path in hike_results:
        hike.path_arr = linestring_to_latlngarr(
            path)  # json.dumps(linestring_to_latlngarr(path))
    colors = get_color_list(len(hike_results))
    return render_template('search_results.html',
                           hikes=[hike for (hike, path) in hike_results],
                           colors=colors,
                           curr_latlng=curr_latlng)
Example #9
0
def advanced_search():
    biomes = [['marsh', 'Marsh'], ['grassy', 'Grassy'],
              ['river', 'River/Creek'], ['wooded', 'Wooded'],
              ['near_water', 'Near Water'], ['mountain', 'Mountain'],
              ['desert', 'Desert'], ['view', 'Has a view']]

    elevations = [['flat', 'Flat'], ['hilly', 'Hilly'],
                  ['mountains',
                   'Mountainous'], ['gradual_up', 'Gradual Uphill'],
                  ['steep_up', 'Steep Uphill'],
                  ['gradual_down', 'Gradual Downhill'],
                  ['steep_down', 'Steep Downhill']]

    terrains = [['rocky', 'Rocky'], ['boulders',
                                     'Boulders'], ['sandy', 'Sandy'],
                ['overgrown', 'High Grass'],
                ['water_crossings', 'Water Crossings'], ['paved', 'Paved'],
                ['trail', 'Dirt Trail'],
                ['bushwacking', 'No trail/Bushwacking']]

    if request.method == 'GET':
        return render_template('search.html',
                               biomes=biomes,
                               elevations=elevations,
                               terrains=terrains)
    else:  # POST
        # get location from IP address
        ip_addr = get_ip_addr(request)
        curr_latlng = get_curr_loc(ip_addr)
        curr_latlng_point = latlng_to_point(curr_latlng)

        session = Session()
        query = session.query(Hike, Hike.path.ST_AsText())

        query = (query.filter(
            Hike.difficulty >= request.form["min_difficulty"],
            Hike.difficulty <= request.form["max_difficulty"],
            Hike.path.ST_Transform(2163).ST_Length() >= miles_to_units(
                float(request.form["min_distance"])),
            Hike.path.ST_Transform(2163).ST_Length() <= miles_to_units(
                float(request.form["max_distance"])),
            or_(Hike.sun_shade == request.form["sun_shade"],
                request.form["sun_shade"] == "don't care"),
            or_(
                len(request.form.getlist("markings")) == 0,
                Hike.markings == "well_marked")))

        for biome in biomes:
            query = query.filter(
                or_(
                    request.form[biome[0]] == "don't care",
                    and_(request.form[biome[0]] == "yes",
                         Hike.surrounding_biome.contains([biome[0]])),
                    and_(request.form[biome[0]] == "no",
                         not_(Hike.surrounding_biome.contains([biome[0]])))))

        for elevation in elevations:
            query = query.filter(
                or_(
                    request.form[elevation[0]] == "don't care",
                    and_(request.form[elevation[0]] == "yes",
                         Hike.elevation.contains([elevation[0]])),
                    and_(request.form[elevation[0]] == "no",
                         not_(Hike.elevation.contains([elevation[0]])))))

        for terrain in terrains:
            query = query.filter(
                or_(
                    request.form[terrain[0]] == "don't care",
                    and_(request.form[terrain[0]] == "yes",
                         Hike.trail_terrain.contains([terrain[0]])),
                    and_(request.form[terrain[0]] == "no",
                         not_(Hike.trail_terrain.contains([terrain[0]])))))

        hike_results = query.order_by(
            Hike.path.ST_Distance(curr_latlng_point)).all()
        # no results
        if len(hike_results) == 0:
            return "No results; modify search"
        for hike, path in hike_results:
            hike.path_arr = linestring_to_latlngarr(
                path)  # json.dumps(linestring_to_latlngarr(path))
        colors = get_color_list(len(hike_results))
        return render_template('search_results.html',
                               hikes=[hike for (hike, path) in hike_results],
                               colors=colors,
                               curr_latlng=curr_latlng)
Example #10
0
def advanced_search():
    biomes = [['marsh', 'Marsh'], ['grassy', 'Grassy'], ['river', 'River/Creek'],
              ['wooded', 'Wooded'], ['near_water', 'Near Water'], ['mountain', 'Mountain'], ['desert', 'Desert'],
              ['view', 'Has a view']]

    elevations = [['flat', 'Flat'], ['hilly', 'Hilly'], ['mountains', 'Mountainous'], ['gradual_up', 'Gradual Uphill'],
                  ['steep_up', 'Steep Uphill'], ['gradual_down', 'Gradual Downhill'],
                  ['steep_down', 'Steep Downhill']]

    terrains = [['rocky', 'Rocky'], ['boulders', 'Boulders'], ['sandy', 'Sandy'], ['overgrown', 'High Grass'],
                ['water_crossings', 'Water Crossings'], ['paved', 'Paved'], ['trail', 'Dirt Trail'],
                ['bushwacking', 'No trail/Bushwacking']]

    if request.method == 'GET':
        return render_template('search.html', biomes=biomes, elevations=elevations, terrains=terrains)
    else:  # POST
        # get location from IP address
        ip_addr = get_ip_addr(request)
        curr_latlng = get_curr_loc(ip_addr)
        curr_latlng_point = latlng_to_point(curr_latlng)

        session = Session()
        query = session.query(Hike, Hike.path.ST_AsText())

        query = (query.filter(
            Hike.difficulty >= request.form["min_difficulty"],
            Hike.difficulty <= request.form["max_difficulty"],
            Hike.path.ST_Transform(2163).ST_Length() >= miles_to_units(float(request.form["min_distance"])),
            Hike.path.ST_Transform(2163).ST_Length() <= miles_to_units(float(request.form["max_distance"])),
            or_(Hike.sun_shade == request.form["sun_shade"], request.form["sun_shade"] == "don't care"),
            or_(len(request.form.getlist("markings")) == 0,  Hike.markings == "well_marked")
        )
        )

        for biome in biomes:
            query = query.filter(
                or_(request.form[biome[0]] == "don't care",
                    and_(request.form[biome[0]] == "yes", Hike.surrounding_biome.contains([biome[0]])),
                    and_(request.form[biome[0]] == "no", not_(Hike.surrounding_biome.contains([biome[0]])))
                    )
            )

        for elevation in elevations:
            query = query.filter(
                or_(request.form[elevation[0]] == "don't care",
                    and_(request.form[elevation[0]] == "yes", Hike.elevation.contains([elevation[0]])),
                    and_(request.form[elevation[0]] == "no", not_(Hike.elevation.contains([elevation[0]])))
                    )
            )

        for terrain in terrains:
            query = query.filter(
                or_(request.form[terrain[0]] == "don't care",
                    and_(request.form[terrain[0]] == "yes", Hike.trail_terrain.contains([terrain[0]])),
                    and_(request.form[terrain[0]] == "no", not_(Hike.trail_terrain.contains([terrain[0]])))
                    )
            )


        hike_results = query.order_by(Hike.path.ST_Distance(curr_latlng_point)).all()
        # no results
        if len(hike_results) == 0:
            return "No results; modify search"
        for hike, path in hike_results:
            hike.path_arr = linestring_to_latlngarr(path)  # json.dumps(linestring_to_latlngarr(path))
        colors = get_color_list(len(hike_results))
        return render_template('search_results.html',
                               hikes=[hike for (hike, path) in hike_results],
                               colors=colors,
                               curr_latlng=curr_latlng)