OpenStreetMap Project Data Wrangling with MongoDB

OpenStreetMap Project Data Wrangling with MongoDB

Wang Xiaochu


Map Area

Birmingham, Westmidlands, England

This city is where I studied in the university for a master's degree of urban and reginal planning last year. So study the city from a different perspective using new skills I've learned means a lot to me.

Problems Encountered in the Map

After initially downloading a small sample size of the Birmingham area and running it against a provisional data.py file, I noticed three main problems with the data, which I will discuss in the following order:

  • Overabbreviated & Misspelled street names ("Roxburgh G4rove", "Hidcote Aveune")

  • "Incorrect" postal codes ( Birmingham postcodes all begin with "B" however a large portion of all documented postcodes were outside this region.)

Overabbreviated & Misspelled Street Names

Before the data was imported into MongoDB, it was audited in audit.py using the following function:

street_type_re = re.compile(r'\b\S+\.?$', re.IGNORECASE)
expected = ["Street", "Avenue", "Boulevard", "Drive", "Court", "Place", "Square", "Lane", "Road", 
        "Trail", "Parkway", "Commons", "Way", "Walk"]

def audit_street_type(street_types, street_name):
    m = street_type_re.search(street_name)
    if m:
        street_type = m.group()
        if street_type not in expected:
            street_types[street_type].add(street_name)

def is_street_name(elem):
    return (elem.attrib['k'] == "addr:street")

def audit(osmfile):
    osm_file = open(osmfile, "r")
    street_types = defaultdict(set)
    for event, elem in ET.iterparse(osm_file, events=("start",)):
        if elem.tag == "node" or elem.tag == "way":
            for tag in elem.iter("tag"):
                if is_street_name(tag):
                    audit_street_type(street_types, tag.attrib['v'])
    osm_file.close()
    return street_types

This basic querying revealed street name abbreviations and misspellings. I updated all substrings in problematic address strings, such as:
"Aveune" becomes "Avenue", "G4rove" becomes "Grove".

Postal Codes

Postcodes turn out to be a non-technical problem. It revealed some of the data out of the range of Birmingham city, which force me to filter out all of nodes with postcodes start with "C" or "D" in the process of import data into JSON file with adding following codes:

if m == 'addr:postcode' and elem.attrib['v'][0] != 'B':
    return None 

Data Overview

This section contains basic statistics about the dataset and the MongoDB queries used to gather them.

File Size

birmingham_england.osm ........... 1,535 MB

birmingham_england.osm.json .... 1,780 MB

  • Number of documents

      > db.birm.find().count()
      
      7017662
    
  • Number of nodes

      > db.birm.find({"type":"node"}).count()
    
      6906846
    
  • Number of ways

      > db.birm.find({"type":"way"}).count()
    
      110816
    
  • Number of unique users

      > db.birm.distinct("created.user").length
    
      2249
    
  • Top 10 contributing user

      > db.birm.aggregate([{"$group":{"_id":"$created.user", "count":{"$sum":1}}}, 
                          {"$sort":{"count":-1}}, {"$limit":10}])
    
      { "_id" : "brianboru", "count" : 3549542 }
      { "_id" : "blackadder", "count" : 447958 }    
      { "_id" : "Miked29", "count" : 370951 }    
      { "_id" : "James Derrick", "count" : 311132 }    
      { "_id" : "mrpacmanmap", "count" : 164581 }    
      { "_id" : "Curran1980", "count" : 136896 }    
      { "_id" : "PeterP", "count" : 115926 }    
      { "_id" : "srbrook", "count" : 105316 }    
      { "_id" : "The Maarssen Mapper", "count" : 104912 }    
      { "_id" : "richardwest", "count" : 104404 }
    
  • Number of users appearing only once (having 1 post)

      > db.birm.aggregate([{"$group":{"_id":"$created.user", "count":{"$sum":1}}}, 
                          {"$group":{"_id":"$count", "num_users":{"$sum":1}}}, 
                          {"$sort":{"_id":1}}, {"$limit":1}])
    
      { "_id" : 1, "num_users" : 365 }
    

Additional Ideas

Region statistics and suggestion

Although some of nodes out of Birmingham region have been filtered by their postcodes, there are still indiscriminable nodes do not have postcode attribute left in the file. One way to solve this problem could be comparing the longitude and latitude of Birmingham city and the attribute of each node. Another way is to ensure the property “addr.city” is properly informed. Using the following query, I explore the statistics further:

    > db.birm.aggregate([{"$match":{"addr.city":{"$exists":1}}},
                        {"$group":{"_id":"$addr.city","count":{"$sum":1}}}, 
                        {"$sort":{"count":-1}}])

    { "_id" : "Birmingham", "count" : 3884 }
    { "_id" : "Solihull", "count" : 910 }
    { "_id" : "Bromsgrove", "count" : 238 }
    { "_id" : "Alcester", "count" : 158 }
    { "_id" : "Sutton Coldfield", "count" : 106 }
    { "_id" : "Tipton", "count" : 63 }
    { "_id" : "Madeley", "count" : 49 }
    .....
    { "_id" : "Ironbridge", "count" : 26 }
    { "_id" : "Wolverhampton", "count" : 22 }
    { "_id" : "bm", "count" : 16 }
    { "_id" : "West Bromwich", "count" : 16 }
    { "_id" : "Redditch", "count" : 13 }

It turns out I didn't wrangle this data properly. Of course better data wrangling should be done ideally, while in the circumstance, the citys are all in the big region of Birmingham, which is just not very influential if I take it as Birmingham metropolitan area.

Additional data exploration using MongoDB queries

  • Top 10 Contributers

      > db.birm.aggregate([{"$group":{"_id":"$created.user", 
                                      "count":{"$sum":1}}}, 
                          {"$sort":{"count":-1}}, {"$limit":10}])
    
      { "_id" : "brianboru", "count" : 3549542 }
      { "_id" : "blackadder", "count" : 447958 }
      { "_id" : "Miked29", "count" : 370951 }
      { "_id" : "James Derrick", "count" : 311132 }
      { "_id" : "mrpacmanmap", "count" : 164581 }
      { "_id" : "Curran1980", "count" : 136896 }
      { "_id" : "PeterP", "count" : 115926 }
      { "_id" : "srbrook", "count" : 105316 }
      { "_id" : "The Maarssen Mapper", "count" : 104912 }
      { "_id" : "richardwest", "count" : 104404 }
    
  • Most common building types

      > db.birm.aggregate([{'$match': {'building': {'$exists': 1, "$ne":"yes"}}}, 
                          {'$group': { '_id': '$building','count': {'$sum': 1}}},
                          {'$sort': {'count': -1}}, {'$limit': 10}])
    
      { "_id" : "residential", "count" : 2147 }
      { "_id" : "industrial", "count" : 563 }
      { "_id" : "retail", "count" : 391 }
      { "_id" : "entrance", "count" : 135 }
      { "_id" : "commercial", "count" : 109 }
      { "_id" : "school", "count" : 107 }
      { "_id" : "garages", "count" : 84 }
      { "_id" : "university", "count" : 60 }
      { "_id" : "church", "count" : 52 }
      { "_id" : "storage_tank", "count" : 48 }
    
  • Top 10 appearing amenities

      > db.birm.aggregate([{"$match":{"amenity":{"$exists":1}}},
                          {"$group":{"_id":"$amenity","count":{"$sum":1}}}, 
                          {"$sort":{"count":-1}}, {"$limit":10}])
    
      { "_id" : "post_box", "count" : 3533 }
      { "_id" : "bench", "count" : 1789 }
      { "_id" : "parking", "count" : 1458 }
      { "_id" : "fast_food", "count" : 1414 }
      { "_id" : "telephone", "count" : 1248 }
      { "_id" : "pub", "count" : 1003 }
      { "_id" : "bicycle_parking", "count" : 922 }
      { "_id" : "grit_bin", "count" : 753 }
      { "_id" : "restaurant", "count" : 720 }
      { "_id" : "cafe", "count" : 637 }
    
  • Top 5 bank branches

      > db.birm.aggregate([{"$match":{"name":{"$exists":1}, "amenity": "bank"}},
                          {"$group":{"_id":"$name","count":{"$sum":1}}}, 
                          {"$sort":{"count":-1}}, {"$limit":5}])
      
      { "_id" : "Barclays", "count" : 35 }
      { "_id" : "HSBC", "count" : 24 }
      { "_id" : "Santander", "count" : 21 }
      { "_id" : "Halifax", "count" : 18 }
      { "_id" : "NatWest", "count" : 15 }
    
  • Top 10 restaurant trends

      > db.birm.aggregate([{"$match":{"amenity":{"$exists":1},"cuisine":{"$exists":1},
                                      "amenity":"restaurant"}}, 
                          {"$group":{"_id":"$cuisine", "count":{"$sum":1}}},
                          {"$sort":{"count":-1}}, {"$limit":10}])
    
      { "_id" : "indian", "count" : 86 }
      { "_id" : "chinese", "count" : 33 }
      { "_id" : "italian", "count" : 26 }
      { "_id" : "french", "count" : 8 }
      { "_id" : "thai", "count" : 7 }
      { "_id" : "american", "count" : 7 }
      { "_id" : "regional", "count" : 6 }
      { "_id" : "pizza", "count" : 5 }
      { "_id" : "mexican", "count" : 5 }
      { "_id" : "fish_and_chips", "count" : 5 }
    
  • Top 10 fast-food trends

      > db.birm.aggregate([{"$match":{"amenity":{"$exists":1},"cuisine":{"$exists":1},
                                      "amenity":"fast_food"}}, 
                          {"$group":{"_id":"$cuisine", "count":{"$sum":1}}},
                          {"$sort":{"count":-1}}, {"$limit":10}])
    
      { "_id" : "fish_and_chips", "count" : 177 }
      { "_id" : "chinese", "count" : 137 }
      { "_id" : "pizza", "count" : 57 }
      { "_id" : "sandwich", "count" : 43 }
      { "_id" : "indian", "count" : 42 }
      { "_id" : "burger", "count" : 29 }
      { "_id" : "chicken", "count" : 23 }
      { "_id" : "italian", "count" : 7 }
      { "_id" : "caribbean", "count" : 6 }
      { "_id" : "kebab", "count" : 6 }
    
  • Top 10 leisure trends

      > db.birm.aggregate([{"$match":{"leisure":{"$exists":1}}}, 
                          {"$group":{"_id":"$leisure", "count":{"$sum":1}}},
                          {"$sort":{"count":-1}}, {"$limit":10}])
    
      { "_id" : "park", "count" : 536 }
      { "_id" : "playground", "count" : 273 }
      { "_id" : "pitch", "count" : 181 }
      { "_id" : "sports_centre", "count" : 116 }
      { "_id" : "picnic_table", "count" : 91 }
      { "_id" : "nature_reserve", "count" : 54 }
      { "_id" : "golf_course", "count" : 49 }
      { "_id" : "fitness_centre", "count" : 38 }
      { "_id" : "garden", "count" : 37 }
      { "_id" : "common", "count" : 33 }
    
  • Top 10 fast-food brands

      > db.birm.aggregate([{"$match":{"name":{"$exists":1},"amenity":"fast_food"}},
                          {"$group":{"_id":"$name","count":{"$sum":1}}},
                          {"$sort":{"count":-1}}, {"$limit":10}])
    
      { "_id" : "Subway", "count" : 67 }
      { "_id" : "McDonald's", "count" : 27 }
      { "_id" : "Greggs", "count" : 25 }
      { "_id" : "KFC", "count" : 24 }
      { "_id" : "Burger King", "count" : 17 }
      { "_id" : "Domino's Pizza", "count" : 11 }
      { "_id" : "Dixy Chicken", "count" : 9 }
      { "_id" : "Pizza Hut", "count" : 8 }
      { "_id" : "Fish & Chips", "count" : 8 }
      { "_id" : "Papa John's", "count" : 7 }
    
  • Top 5 shop trends

      > db.birm.aggregate([{"$match":{"shop":{"$exists":1,"$ne":"yes"}}}, 
                          {"$group":{"_id":"$shop", "count":{"$sum":1}}},
                          {"$sort":{"count":-1}}, {"$limit":5}])
      
      { "_id" : "hairdresser", "count" : 812 }
      { "_id" : "convenience", "count" : 742 }
      { "_id" : "clothes", "count" : 731 }
      { "_id" : "supermarket", "count" : 282 }
      { "_id" : "car_repair", "count" : 239 }
    
  • Top 5 supermarket brands

      > db.birm.aggregate([{"$match":{"name":{"$exists":1},"shop": "supermarket"}},
                          {"$group":{"_id":"$name","count":{"$sum":1}}},
                          {"$sort":{"count":-1}}, {"$limit":5}])
    
      { "_id" : "The Co-operative Food", "count" : 32 }
      { "_id" : "Tesco Express", "count" : 14 }
      { "_id" : "Spar", "count" : 13 }
      { "_id" : "Costcutter", "count" : 13 }
      { "_id" : "Aldi", "count" : 12 }
    
  • Accessibility for disabled

      > db.birm.aggregate([{"$match":{"wheelchair":{"$exists":1},"type": "way"}},
                          {"$group":{"_id":"$wheelchair","count":{"$sum":1}}},
                          {"$sort":{"count":-1}}])
    
      { "_id" : "yes", "count" : 112 }
      { "_id" : "no", "count" : 7 }
      { "_id" : "limited", "count" : 4 }
      { "_id" : "designated", "count" : 4 }
    
  • Religion of worship

      > db.birm.aggregate([{"$match":{"religion":{"$exists":1},"type": "node"}},
                          {"$group":{"_id":"$religion","count":{"$sum":1}}},
                          {"$sort":{"count":-1}}])
    
      { "_id" : "christian", "count" : 223 }
      { "_id" : "muslim", "count" : 18 }
      { "_id" : "hindu", "count" : 3 }
      { "_id" : "sikh", "count" : 1 }
      { "_id" : "jewish", "count" : 1 }
      { "_id" : "multifaith", "count" : 1 }
      { "_id" : "spiritualist", "count" : 1 }
    

Conclusion

OpenStreetMap data presents an ideal opportunity for me to practice data wrangling and a special way to explore citys. Although this review of data is cursory, I think is has been well cleaned for the purposes of the exercise.

References

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,755评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,369评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,799评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,910评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,096评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,159评论 3 411
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,917评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,360评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,673评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,814评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,509评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,156评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,123评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,641评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,728评论 2 351

推荐阅读更多精彩内容