473,395 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Python to csharp conversion of OSM parsing

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.


Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/python
  2. import xml.sax
  3.  
  4. class Node(object):
  5.     def __init__(self, id=None, lon=None, lat=None, tags=None):
  6.         self.id = id
  7.         self.lon, self.lat = lon, lat
  8.         if tags:
  9.             self.tags = tags
  10.         else:
  11.             self.tags = {}
  12.  
  13.     def __repr__(self):
  14.         return "Node(id=%r, lon=%r, lat=%r, tags=%r)" % (self.id, self.lon, self.lat, self.tags)
  15.  
  16. class Way(object):
  17.     def __init__(self, id, nodes=None, tags=None):
  18.         self.id = id
  19.         if nodes:
  20.             self.nodes = nodes
  21.         else:
  22.             self.nodes = []
  23.         if tags:
  24.             self.tags = tags
  25.         else:
  26.             self.tags = {}
  27.  
  28.     def __repr__(self):
  29.         return "Way(id=%r, nodes=%r, tags=%r)" % (self.id, self.nodes, self.tags)
  30.  
  31. class NodePlaceHolder(object):
  32.     def __init__(self, id):
  33.         self.id = id
  34.  
  35.     def __repr__(self):
  36.         return "NodePlaceHolder(id=%r)" % (self.id)
  37.  
  38. class OSMXMLFile(object):
  39.     def __init__(self, filename):
  40.         self.filename = filename
  41.  
  42.         self.nodes = {}
  43.         self.ways = {}
  44.         self.__parse()
  45.         print repr(self.ways)
  46.  
  47.     def __parse(self):
  48.         """Parse the given XML file"""
  49.         parser = xml.sax.make_parser()
  50.         parser.setContentHandler(OSMXMLFileParser(self))
  51.         parser.parse(self.filename)
  52.  
  53.         # now fix up all the refereneces
  54.         for way in self.ways.values():
  55.             way.nodes = [self.nodes[node_pl.id] for node_pl in way.nodes]
  56.  
  57.         # convert them back to lists
  58.         self.nodes = self.nodes.values()
  59.         self.ways = self.ways.values()
  60.  
  61.  
  62. class OSMXMLFileParser(xml.sax.ContentHandler):
  63.     def __init__(self, containing_obj):
  64.         self.containing_obj = containing_obj
  65.         self.curr_node = None
  66.         self.curr_way = None
  67.  
  68.     def startElement(self, name, attrs):
  69.         #print "Start of node " + name
  70.         if name == 'node':
  71.             self.curr_node = Node(id=attrs['id'], lon=attrs['lon'], lat=attrs['lat'])
  72.         elif name == 'way':
  73.             #self.containing_obj.ways.append(Way())
  74.             self.curr_way = Way(id=attrs['id'])
  75.         elif name == 'tag':
  76.             #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)
  77.             if self.curr_node:
  78.                 self.curr_node.tags[attrs['k']] = attrs['v']
  79.             elif self.curr_way:
  80.                 self.curr_way.tags[attrs['k']] = attrs['v']
  81.         elif name == "nd":
  82.             assert self.curr_node is None, "curr_node (%r) is non-none" % (self.curr_node)
  83.             assert self.curr_way is not None, "curr_way is None"
  84.             self.curr_way.nodes.append(NodePlaceHolder(id=attrs['ref']))
  85.  
  86.  
  87.     def endElement(self, name):
  88.         #print "End of node " + name
  89.         #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)
  90.         if name == "node":
  91.             self.containing_obj.nodes[self.curr_node.id] = self.curr_node
  92.             self.curr_node = None
  93.         elif name == "way":
  94.             self.containing_obj.ways[self.curr_way.id] = self.curr_way
  95.             self.curr_way = None
  96.  
  97. if __name__ == '__main__':
  98.     osm = OSMXMLFile("./lucan.osm.xml")

Many thanks
Aug 9 '10 #1
0 2135

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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....
4
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....
7
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...
6
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
4
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...
0
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...
3
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...
3
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...
3
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....
1
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.
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.