Hello, can anybody convert python code that read openstreetmap xml file from http://github.com/rory/python-osm/blob/master/pyosm.py
i try to translate but with the lack of knowledge of python , never get it done. Even approximate conversion that will be greate.
If the whole file is too much , if someone can translate a couple of class from this file like class Node(object):
class Way(object):
that will be great as well, and i will carry on trying with these sample. - #! /usr/bin/python
-
import xml.sax
-
-
class Node(object):
-
def __init__(self, id=None, lon=None, lat=None, tags=None):
-
self.id = id
-
self.lon, self.lat = lon, lat
-
if tags:
-
self.tags = tags
-
else:
-
self.tags = {}
-
-
def __repr__(self):
-
return "Node(id=%r, lon=%r, lat=%r, tags=%r)" % (self.id, self.lon, self.lat, self.tags)
-
-
class Way(object):
-
def __init__(self, id, nodes=None, tags=None):
-
self.id = id
-
if nodes:
-
self.nodes = nodes
-
else:
-
self.nodes = []
-
if tags:
-
self.tags = tags
-
else:
-
self.tags = {}
-
-
def __repr__(self):
-
return "Way(id=%r, nodes=%r, tags=%r)" % (self.id, self.nodes, self.tags)
-
-
class NodePlaceHolder(object):
-
def __init__(self, id):
-
self.id = id
-
-
def __repr__(self):
-
return "NodePlaceHolder(id=%r)" % (self.id)
-
-
class OSMXMLFile(object):
-
def __init__(self, filename):
-
self.filename = filename
-
-
self.nodes = {}
-
self.ways = {}
-
self.__parse()
-
print repr(self.ways)
-
-
def __parse(self):
-
"""Parse the given XML file"""
-
parser = xml.sax.make_parser()
-
parser.setContentHandler(OSMXMLFileParser(self))
-
parser.parse(self.filename)
-
-
# now fix up all the refereneces
-
for way in self.ways.values():
-
way.nodes = [self.nodes[node_pl.id] for node_pl in way.nodes]
-
-
# convert them back to lists
-
self.nodes = self.nodes.values()
-
self.ways = self.ways.values()
-
-
-
class OSMXMLFileParser(xml.sax.ContentHandler):
-
def __init__(self, containing_obj):
-
self.containing_obj = containing_obj
-
self.curr_node = None
-
self.curr_way = None
-
-
def startElement(self, name, attrs):
-
#print "Start of node " + name
-
if name == 'node':
-
self.curr_node = Node(id=attrs['id'], lon=attrs['lon'], lat=attrs['lat'])
-
elif name == 'way':
-
#self.containing_obj.ways.append(Way())
-
self.curr_way = Way(id=attrs['id'])
-
elif name == 'tag':
-
#assert not self.curr_node and not self.curr_way, "curr_node (%r) and curr_way (%r) are both non-None" % (self.curr_node, self.curr_way)
-
if self.curr_node:
-
self.curr_node.tags[attrs['k']] = attrs['v']
-
elif self.curr_way:
-
self.curr_way.tags[attrs['k']] = attrs['v']
-
elif name == "nd":
-
assert self.curr_node is None, "curr_node (%r) is non-none" % (self.curr_node)
-
assert self.curr_way is not None, "curr_way is None"
-
self.curr_way.nodes.append(NodePlaceHolder(id=attrs['ref']))
-
-
-
def endElement(self, name):
-
#print "End of node " + name
-
#assert not self.curr_node and not self.curr_way, "curr_node (%r) and curr_way (%r) are both non-None" % (self.curr_node, self.curr_way)
-
if name == "node":
-
self.containing_obj.nodes[self.curr_node.id] = self.curr_node
-
self.curr_node = None
-
elif name == "way":
-
self.containing_obj.ways[self.curr_way.id] = self.curr_way
-
self.curr_way = None
-
-
if __name__ == '__main__':
-
osm = OSMXMLFile("./lucan.osm.xml")
Many thanks
0 2135 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Uwe Schmitt |
last post by:
Hi,
I'd like to wrap a C++ class with a member which returns std::pair<int,int>.
Is there an easy way to force boost to do automitic conversion to a tuple ?
Greetings, Uwe
--
Dr. rer. nat....
|
by: chris |
last post by:
hi,
i am looking for a way to structure the text in XML comments to produce
a XHTML doc format for the XMLs (in this case XSLT).
so is was thinking about using a wiki like text format. e.g....
|
by: Sanjay Kedare |
last post by:
Hi,
I am evaluating various alternatives of converting Delphi (Ver 4.0) screens
to C# screens. Are there any tools available for such conversion? if yes how
much conversion do they achieve? The...
|
by: arun |
last post by:
Hi
Any one kno what is the C3 equivalent of vb
DataSetObj .Tables(0).Rows(0).Item("ID")
Thanks in Advance
|
by: R Wood |
last post by:
Greetings -
A recent Perl experiment hasn't turned out so well, which has piqued my
interest in Python. The project is this: take a Vcard file exported from
Apple's Addressbook and use a...
|
by: ronysk |
last post by:
Hi,
I am posting here to seek for help on type conversion between Python (Numeric Python) and C.
Attachment A is a math function written in C, which is called by a Python program. I had...
|
by: linksterman |
last post by:
I am developing a program for the PSP, and so far the methods for processing XML's depend on dll's, so i cannot use them as they are not compiled for use with the PlayStation Portable, bout...
|
by: Phillip B Oldham |
last post by:
Hi. I'm stretching my boundaries in programming with a little python
shell-script which is going to loop through a list of domain names,
grab the whois record, parse it, and put the results into a...
|
by: bruce |
last post by:
Hi...
got a short test app that i'm playing with. the goal is to get data off the
page in question.
basically, i should be able to get a list of "tr" nodes, and then to
iterate/parse them....
|
by: bruce |
last post by:
Hi Paul...
Thanks for the reply. Came to the same conclusion a few minutes before I saw
your email.
Another question:
tr=d.xpath(foo)
gets me an array of nodes.
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |