DinoTools / python-overpy

Python Wrapper to access the Overpass API
https://python-overpy.readthedocs.io/
MIT License
242 stars 58 forks source link

Unable to print id of relations #20

Closed rkiddy closed 9 years ago

rkiddy commented 9 years ago

I am trying to go through a large set of relations, specifically all those in California.

 result = api.query('relation'
                    '["boundary"="administrative"]'
                    '(32.0,-125.0,42.0,-114.0);'
                    'out body;')

My python is not very sophisticated. I am a almost-intermediate in the language. I have the list of relations and am trying to print this out:

 print '%s\t%s\t%s\t%s\t%s' % (relation.tags.get('name','').encode('utf8'),
                                    relation.tags.get('admin_level','').encode('utf8'),
                                    relation.tags.get('place','').encode('utf8'),
                                    relation.tags.get('boundary','').encode('utf8'),
                                    relation.tags.get('type','').encode('utf8'),
                                    relation.id)

Everything but the id works great. I have tried writing out the id with "%d" and that does not work, but with "%s", it says it is a number. Looking at your recent edit, it might be None. How can one write this out?

phibos commented 9 years ago

I did a quick & dirty hack to test the code you have provided.

from overpy import Overpass

api = Overpass()

result = api.query('relation'
                    '["boundary"="administrative"]'
                    '(32.0,-125.0,42.0,-114.0);'
                    'out body;')

for relation in result.relations:
 print('%s\t%s\t%s\t%s\t%s\t%d' % (relation.tags.get('name','').encode('utf8'),
                                    relation.tags.get('admin_level','').encode('utf8'),
                                    relation.tags.get('place','').encode('utf8'),
                                    relation.tags.get('boundary','').encode('utf8'),
                                    relation.tags.get('type','').encode('utf8'),
                                    relation.id))

Looks like one argument is missing in your format string.

Your string

%s\t%s\t%s\t%s\t%s

Modified version

%s\t%s\t%s\t%s\t%s\t%d
rkiddy commented 9 years ago

I am pretty sure that was just a typo. Sorry.

Um. Nope. Pilot error. Sorry.

phibos commented 9 years ago

You are welcome.