0

Here is a snippet of my code so far

import osmapi
import geopy
from geopy.geocoders import Nominatim
import requests
import xmltodict

geolocator = Nominatim()
location = geolocator.reverse("{}, {}".format(lat, lon))
dict = location.raw
osmid = dict.get('osm_id', 'default_value_if_null_here')
osmtype = dict.get('osm_type', 'default_value_if_null_here')

if(osmtype == 'node'):
    node_info = requests.get("http://api.openstreetmap.org/api/0.6/node/"+ osmid)
    d = xmltodict.parse(node_info.content)
    amenity_tags = [tag for tag in d['osm']['node']['tag'] if tag['@k'] == 'amenity']

    if len(amenity_tags) != 0:
        print amenity_tags

I basically want to check if a location is corresponding to a node on openstreetmap and if so, check if it is an amenity and what type of amenity. A Sample output is as follows:

[OrderedDict([(u'@k', u'amenity'), (u'@v', u'cafe')])]
[OrderedDict([(u'@k', u'amenity'), (u'@v', u'fast_food')])]
[OrderedDict([(u'@k', u'amenity'), (u'@v', u'bicycle_parking')])]
[OrderedDict([(u'@k', u'amenity'), (u'@v', u'atm')])]
[OrderedDict([(u'@k', u'amenity'), (u'@v', u'restaurant')])]
[OrderedDict([(u'@k', u'amenity'), (u'@v', u'restaurant')])]
[OrderedDict([(u'@k', u'amenity'), (u'@v', u'theatre')])]

My question is how would I split up this ordered dictionary. The outcome I am hoping for is to have a variable 'amenity_type' = cafe, restaurant, theatre etc... I really hope I have explained what my question is well enough. I am new to python so any help would be appreciated, thanks!

RyanKilkelly
  • 279
  • 1
  • 4
  • 15
  • so you want a new dictionary out of the values of the previous one? – Copperfield Feb 05 '16 at 14:06
  • 1
    "a variable 'amenity_type' = cafe, restaurant, theatre etc." => this is not a clear description. Please post the expected output for the above sample input... – bruno desthuilliers Feb 05 '16 at 14:12
  • @brunodesthuilliers I have tried 'amenity_type = (amenity_tags['@v'])' for example, I simply want to have a variable amenity_type, so that if I were to print amenity_type it would simply print 'restaurant' for example – RyanKilkelly Feb 05 '16 at 14:39
  • 2
    well, and what is stopping you? `amenity_tags` look like a list with one elements, to access it you do `amenity_tags[0]` and with that you can do `amenity_type = amenity_tags[0]['@v']` to get `'cafe'` for example – Copperfield Feb 05 '16 at 15:04
  • 1
    also don't name your variables the same as build-in names of python like `dict`, `list`, etc, because you shadow them – Copperfield Feb 05 '16 at 15:08
  • What variables do you want to split the Ordered Dictionaries into? – martineau Feb 05 '16 at 15:09
  • @Copperfield thanks that solved it, I am new to python so didn't even know to do this! – RyanKilkelly Feb 05 '16 at 16:27

1 Answers1

1

Perhaps what you're looking for is a class. A class gives you the ability to hold complex data in one bucket, as it were, can provide custom string services like formatting for print, even allows you to define operators for objects of its type. This example takes (up to) three variables in initialization or via set(), and will output them when you use str()

#!/usr/bin/python

class myThing(object):
    def __init__(self,a='',b=0,c=[]):
        self.set(a,b,c);
    def set(self,a=None,b=None,c=None):
        if a != None: self.a = a;
        if b != None: self.b = b;
        if c != None: self.c = c;
    def __str__(self):
        out  = self.a + '\n'
        out += str(self.b) + '\n'
        out += str(self.c) + '\n'
        return out

if __name__ == "__main__":
    x = myThing('boooga',42,[1,2,'Spaghetti and Pizza'])
    print x

    x.set(b=12)
    print str(x)

    print x.a
    print str(x.b)
    print str(x.c)
    for el in x.c:
        print el

If you put that in a file, and run it at the console, you get:

boooga
42
[1, 2, 'Spaghetti and Pizza']

boooga
12
[1, 2, 'Spaghetti and Pizza']

boooga
12
[1, 2, 'Spaghetti and Pizza']
1
2
Spaghetti and Pizza

There's lots more you can with a class in this vein, but perhaps that will get you started if it suits your needs.

I'm assuming you are running Linux or OS X. If that won't run in either of those, type which python at the console, and change the first line from /usr/bin/python to the instance it reports to you.

It's written for Python 2-series. Python 3 is... something else.

fyngyrz
  • 2,458
  • 2
  • 36
  • 43
  • is bad idea to use mutable objects as default value, it can get you unexpected results. Also, you don't need to define setter unless you need to verify the data – Copperfield Feb 05 '16 at 16:18
  • I have never had a problem. You should explain yourself. Under exactly what conditions would those defaults cause a problem. As for set(), it's an example, a starting place, that's all. The point is to *start* with this, you know, not to clone it. – fyngyrz Feb 05 '16 at 16:49
  • the use of mutables values can cause problem if you modify them, in that case the next call to the function will see the changes made by a previous call or outside if you return that value and if that is not the intention, then is a problem. As mention in here: [“Least Astonishment” in Python: The Mutable Default Argument](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) and here [Default Parameter Values in Python](http://effbot.org/zone/default-values.htm#valid-uses-for-mutable-defaults) – Copperfield Feb 05 '16 at 17:18
  • Oh, I see what your concern is. Allowing modification with persistence was my intent. It's a design for storage. So not a problem. Thanks for the response. – fyngyrz Feb 05 '16 at 18:37