Advertisement
vikramk3

way_classifer.py

Oct 16th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Oct 12 17:25:10 2014
  4.  
  5. @author: vikramk3
  6. """
  7.  
  8. #!/usr/bin/env python
  9. """
  10. Use an aggregation query to answer the following question.
  11.  
  12. Which Way, based on id, is one-way, closed, or an area?
  13.  
  14. In my analysis, if a way has two of the same nodes, it is closed. If it has all unique nodes, it
  15. is one way. If the way has three of the same nodes, it is an area.
  16.  
  17. Please modify only the 'make_pipeline' function so that it creates and returns an aggregation
  18. pipeline that can be passed to the MongoDB aggregate function. As in our examples in this lesson,
  19. the aggregation pipeline should be a list of one or more dictionary objects.
  20. Please review the lesson examples if you are unsure of the syntax.
  21.  
  22. This code was run locally on my machine and the data set is a smaller version of the
  23. the complete Austin, TX OSM data set.
  24.  
  25. """
  26.  
  27. def get_db(db_name):
  28.     from pymongo import MongoClient
  29.     client = MongoClient('localhost:27017')
  30.     db = client[db_name]
  31.     return db
  32.  
  33. def make_pipeline():
  34.     # complete the aggregation pipeline
  35.     pipeline = [{"$match":{"type":"way"}},{"$unwind":"$node_refs"},{"$group":{"_id":{"id":"$id", "node_refs":"$node_refs"}, "count":{"$sum":1}}}, {"$sort":{"count":-1}},{"$limit":100}]
  36.     return pipeline
  37.  
  38. def aggregate(db, pipeline):
  39.     result = db.cities.aggregate(pipeline)
  40.     return result
  41.  
  42. if __name__ == '__main__':
  43.     db = get_db('examples')
  44.     pipeline = make_pipeline()
  45.     result = aggregate(db, pipeline)
  46.     #print result
  47.     import pprint
  48.     pprint.pprint(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement