Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Sun Oct 12 17:25:10 2014
- @author: vikramk3
- """
- #!/usr/bin/env python
- """
- Use an aggregation query to answer the following question.
- Which Way, based on id, is one-way, closed, or an area?
- In my analysis, if a way has two of the same nodes, it is closed. If it has all unique nodes, it
- is one way. If the way has three of the same nodes, it is an area.
- Please modify only the 'make_pipeline' function so that it creates and returns an aggregation
- pipeline that can be passed to the MongoDB aggregate function. As in our examples in this lesson,
- the aggregation pipeline should be a list of one or more dictionary objects.
- Please review the lesson examples if you are unsure of the syntax.
- This code was run locally on my machine and the data set is a smaller version of the
- the complete Austin, TX OSM data set.
- """
- def get_db(db_name):
- from pymongo import MongoClient
- client = MongoClient('localhost:27017')
- db = client[db_name]
- return db
- def make_pipeline():
- # complete the aggregation pipeline
- pipeline = [{"$match":{"type":"way"}},{"$unwind":"$node_refs"},{"$group":{"_id":{"id":"$id", "node_refs":"$node_refs"}, "count":{"$sum":1}}}, {"$sort":{"count":-1}},{"$limit":100}]
- return pipeline
- def aggregate(db, pipeline):
- result = db.cities.aggregate(pipeline)
- return result
- if __name__ == '__main__':
- db = get_db('examples')
- pipeline = make_pipeline()
- result = aggregate(db, pipeline)
- #print result
- import pprint
- pprint.pprint(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement