Beispiel #1
0
def relations_csv_handler(file_link):
    query = "USING PERIODIC COMMIT" \
            " LOAD CSV FROM {path} AS file" \
            " MATCH (a:Station {short: file[1]}), (b:Vehicle {code: file[0]})" \
            " CREATE (a)-[:FR {distance: toFloat(file[2])}]->(b)" \
            " CREATE (b)-[:TO {distance: 0.0}]->(a)"

    params = {'path': file_link}
    neo_db.cypher_query(query=query, params=params)
Beispiel #2
0
def stations_csv_handler(file_link):
    query = "USING PERIODIC COMMIT LOAD CSV FROM 'http://web.itu.edu.tr/yucelmuh/enpublic/stations.csv' AS " \
            "file WITH file CREATE (n:Station {short:file[0], name:file[1], latitude:toFloat(file[2]), " \
            "longitude:toFloat(file[3]), directed:0, nearby:0, searched:0, visited:0}) WITH n CALL spatial.addNode(" \
            "'spati',n) YIELD node WITH n CALL spatial.closest('spati', {latitude:n.latitude, longitude:n.longitude}, " \
            "0.005) YIELD node AS nd WITH n,nd CREATE (n)-[:WALK {distance: 4}]->(nd) CREATE (nd)-[:WALK {distance: " \
            "4}]->(n) "

    params = {'path': file_link}
    neo_db.cypher_query(query=query, params=params)
    delete_self_walk = "MATCH (n:Station)-[r:WALK]->(m:Station) WHERE n.short = m.short DELETE r"
    neo_db.cypher_query(query=delete_self_walk)
Beispiel #3
0
def create_neo_db():
    # Create temporary station and vehicle
    tmp_station = Station(short='STATI',
                          name='Station',
                          latitude=10.0,
                          longitude=10.0).save()
    tmp_vehicle = Vehicle(code='100').save()

    spatial_query = "CALL spatial.addPointLayer('spati')"
    neo_db.cypher_query(query=spatial_query)

    tmp_station.delete()
    tmp_vehicle.delete()
Beispiel #4
0
def get_nearby_stations(lat, lon):
    ret_object = []
    query = "CALL spatial.closest('spati', {latitude: {lat}, longitude: {lon}}, {dist}) YIELD node AS result " \
            "RETURN result.name AS name, result.latitude AS latitude, " \
            "result.longitude AS longitude, result.short AS shortn LIMIT 1"

    try:
        stations = neo_db.cypher_query(query=query,
                                       params={
                                           'lat': lat,
                                           'lon': lon,
                                           'dist': 0.05
                                       })
        stations = stations[0]
        for station in stations:
            if station[1] == lat and station[2] == lon:
                continue

            nearby_stations = {
                'name': station[0],
                'latitude': station[1],
                'longitude': station[2],
                'shortn': station[3]
            }
            ret_object.append(nearby_stations)

        return ret_object
    except Exception as e:
        print(type(e))
        return {'message': str(e)}
Beispiel #5
0
def get_directions(fr, to):
    try:
        ret_object = []

        fr.directed += 1
        to.directed += 1
        fr.save()
        to.save()

        query = "MATCH (src:Station {short:{src}}), (dest:Station {short:{dest}}), p=allShortestPaths((src)-[*]->(" \
                "dest)) RETURN extract(n in nodes(p) | n.code) AS vehicle, extract(n in nodes(p) | n.short) AS " \
                "station, reduce(traveltime = 0, r in relationships(p) | traveltime + r.distance) AS totalTime ORDER " \
                "BY totalTime ASC LIMIT 5"

        graph = neo_db.cypher_query(query=query,
                                    params={
                                        'src': fr.short,
                                        'dest': to.short
                                    })

        for result in graph[0]:
            result_obj = []
            vehicles = result[0]
            stations = result[1]

            iterator = 0

            while iterator < len(stations):
                obj = {}
                station_code = stations[iterator]
                if station_code is not None:
                    station = Station.nodes.get(short=station_code)
                    obj['shortn'] = station.short
                    obj['name'] = station.name
                    obj['latitude'] = station.latitude
                    obj['longitude'] = station.longitude
                    if iterator + 1 < len(vehicles):
                        vehicle_code = vehicles[iterator + 1]
                        if vehicle_code is not None:
                            vehicle = Vehicle.nodes.get(code=vehicle_code)
                            obj['next'] = {
                                'code': vehicle.code,
                                'color': vehicle.color
                            }
                        else:
                            obj['next'] = None
                    else:
                        obj['next'] = None

                    result_obj.append(obj)

                iterator += 1

            ret_object.append(result_obj)

        return ret_object
    except Exception as e:
        print(type(e))
        return {'message': str(e)}
Beispiel #6
0
def api_station():
    name = request.args.get('name')
    if name is None:
        ret_object = []
        query = "CALL spatial.closest('spati', {latitude: {lat}, longitude: {lon}}, {dist}) YIELD node AS result SET " \
                "result.nearby=result.nearby+1 RETURN result.name AS name, result.latitude AS latitude, " \
                "result.longitude AS longitude, result.short AS shortn "

        try:
            latitude = float(request.args.get('lat'))
            longitude = float(request.args.get('lon'))
            distance = request.args.get('dist')

            if distance is None:
                distance = 0.005
            else:
                distance = float(distance)

            stations = neo_db.cypher_query(query=query, params={'lat': latitude, 'lon': longitude, 'dist': distance})
            stations = stations[0]
            for station in stations:
                nearby_stations = {
                    'name': station[0],
                    'latitude': station[1],
                    'longitude': station[2],
                    'shortn': station[3]
                }
                ret_object.append(nearby_stations)

            return jsonify(ret_object)
        except Exception as e:
            print(type(e))
            return jsonify({'message': str(e)}), 500
    else:
        current_user.stats.searched += 1
        current_user.save()
        try:
            stations = Station.nodes.filter(name__istartswith=name)
            ret_object = []
            for station in stations:
                found = {
                    'shortn': station.short,
                    'name': station.name,
                    'latitude': station.latitude,
                    'longitude': station.longitude
                }
                ret_object.append(found)

            return jsonify(ret_object)
        except Exception as e:
            print(type(e))
            return jsonify({'message': str(e)}), 500
Beispiel #7
0
def vehicles_csv_handler(file_link):
    query = "USING PERIODIC COMMIT" \
            " LOAD CSV FROM {path} AS file" \
            " CREATE (:Vehicle {code: file[0], color: file[1], description: file[2]})"
    params = {'path': file_link}
    neo_db.cypher_query(query=query, params=params)