예제 #1
0
    def execute(trial=False, logging=True):
        startTime = datetime.datetime.now()

        if logging:
            print("in make_graph.py")

        # Set up the database connection.
        client = dml.pymongo.MongoClient()
        repo = client.repo
        repo.authenticate('adsouza_bmroach_mcaloonj_mcsmocha',
                          'adsouza_bmroach_mcaloonj_mcsmocha')

        points = repo[
            'adsouza_bmroach_mcaloonj_mcsmocha.signal_placements'].find()
        coords = points[0]['signal_placements']
        df_geo = pd.DataFrame(coords, columns=['Lats', "Longs"])

        # Turn a dataframe containing point data into a geojson formatted python dictionary
        def df_to_geojson(df, properties, lat='Lats', lon='Longs'):
            geo_json = {'type': 'FeatureCollection', 'features': []}
            for _, row in df.iterrows():
                feature = {
                    'type': 'Feature',
                    'properties': {},
                    'geometry': {
                        'type': 'Point',
                        'coordinates': []
                    }
                }
                feature['geometry']['coordinates'] = [row[lon], row[lat]]
                for prop in properties:
                    feature['properties'][prop] = row[prop]
                geo_json['features'].append(feature)
            return geo_json

        placements_geojson = df_to_geojson(df_geo, properties="")
        with open('./adsouza_bmroach_mcaloonj_mcsmocha/placements.html',
                  'w') as output:
            output.write(
                geoleaflet.html(placements_geojson))  # Create visualization.

        repo.logout()
        endTime = datetime.datetime.now()
        return {"start": startTime, "end": endTime}
예제 #2
0
        def execute(trial=False, logging=True):
            startTime = datetime.datetime.now()

            #__________________________
            #Parameters
            mean_skew = 1.0
            # ^ allows mean (for checking bottom 50th percent of distances) to be skewed, to allow in more or less.
            # decreasing value decreases the mean, so fewer are allowed in

            assert type(mean_skew) == float and mean_skew > 0

            if trial:
                keep_within_value = .5
            else:
                keep_within_value = 2

            #End Parameters
            #__________________________

            if logging:
                print("in fetch_nodes.py")

            client = dml.pymongo.MongoClient()
            repo = client.repo
            repo.authenticate('adsouza_bmroach_mcaloonj_mcsmocha', 'adsouza_bmroach_mcaloonj_mcsmocha')
            g = geoql.loads(requests.get('http://bostonopendata-boston.opendata.arcgis.com/datasets/cfd1740c2e4b49389f47a9ce2dd236cc_8.geojson').text, encoding="latin-1")

            g = g.keep_within_radius((42.3551, -71.0656), keep_within_value, 'miles')

            g = g.node_edge_graph()

            g.dump(open('example_extract.geojson', 'w'))

            f = open('example_extract.geojson', 'r')
            f = f.read()
            j = json.loads(f)["features"]


            #Creates dictionary with only nodes of the graph
            nodes= [[obj['coordinates'][1],obj['coordinates'][0]] for obj in j if (obj['type'] == "Point")]
            #print (len(nodes))
            open('leaflet.html', 'w').write(geoleaflet.html(g)) # Create visualization.

            clusters = repo['adsouza_bmroach_mcaloonj_mcsmocha.accident_clusters'].find_one()
            clusters = clusters["accident_clusters"]

            #Insert clusters into cKDTree
            cluster_tree = cKDTree(clusters)

            distances = []
            #For each node, get distance of nearest cluster
            for i in range(len(nodes)):
                #find the k nearest neighbors
                dist, idx = cluster_tree.query(nodes[i], k=1, p=2) #p=2 means euclidean distance
                distances.append((dist,idx))

            #Get average distance to closest cluster
            mean = (sum([x[0] for x in distances])/len(distances)) * mean_skew

            #Filter out nodes that have distance to nearest cluster that is less than the mean
            filtered_nodes = [nodes[x[1]] for x in distances if x[0] >= mean]


            #Insert into repo {"nodes": [[lat,long], [lat,long]..............]}
            tmp = dict()
            tmp['nodes'] = filtered_nodes
            filtered_nodes = tmp


            repo.dropCollection("nodes")
            repo.createCollection("nodes")

            repo['adsouza_bmroach_mcaloonj_mcsmocha.nodes'].insert(filtered_nodes)
            repo['adsouza_bmroach_mcaloonj_mcsmocha.nodes'].metadata({'complete':True})

            repo.logout()
            endTime = datetime.datetime.now()
            return {"start":startTime, "end":endTime}
예제 #3
0
파일: make_graph.py 프로젝트: mfyc/speed
import geojson
from geoql import geoql
import geoleaflet
import requests

#url = 'https://raw.githubusercontent.com/Data-Mechanics/geoql/master/examples/'

# Boston ZIP Codes regions.
#z = geoql.loads(requests.get(url + 'example_zips.geojson').text, encoding="latin-1")

# Extract of street data.
#g = geoql.loads(requests.get(url + 'example_extract.geojson').text, encoding="latin-1")
g = geoql.loads(requests.get(
    'http://bostonopendata-boston.opendata.arcgis.com/datasets/cfd1740c2e4b49389f47a9ce2dd236cc_8.geojson'
).text,
                encoding="latin-1")

# g = g.properties_null_remove()\
#      .tags_parse_str_to_dict()\
#      .keep_by_property({"highway": {"$in": ["residential", "secondary", "tertiary"]}})

g = g.keep_within_radius((42.3551, -71.0656), 2,
                         'miles')  # 0.75 miles from Boston Common.
#g = g.keep_that_intersect(z) # Only those entries found in a Boston ZIP Code regions.
g = g.node_edge_graph()  # Converted into a graph with nodes and edges.
g.dump(open('example_extract.geojson', 'w'))
open('leaflet.html', 'w').write(geoleaflet.html(g))  # Create visualization.
    for f in tqdm(students.features,
                  desc='Updating student data with bus assignments'):
        coords = f.geometry.coordinates
        f['properties']['bus_id'] = school_stop_to_bus[(tuple(coords[2]),
                                                        tuple(coords[1]))]
    open(file_students, 'w').write(geojson.dumps(students, indent=2))

    return routes


if __name__ == "__main__":
    grid = Grid('input/segments-prepared.geojson')
    buses = json.load(open('output/buses.json', 'r'))
    students = geojson.load(open('output/students.geojson', 'r'))
    stops = stops_to_dict('output/stops.json')
    routes = school_stops_to_routes(grid,
                                    'output/students.geojson',
                                    school_to_stops(stops),
                                    buses,
                                    max_dist_miles=20,
                                    max_stops=30)
    open('output/routes.geojson', 'w').write(
        geojson.dumps(
            geojson.FeatureCollection(
                [f for r in routes for f in r.features()])))
    open('output/routes.html', 'w').write(
        geoleaflet.html(
            geojson.FeatureCollection(
                [f for r in routes for f in r.features()])))

## eof