def parse_xml_osc_file(self, filename): """ Parse a XML file in OSM change format. Single threaded """ from imposm.parser.xml.parser import XMLParser parser = XMLParser(coords_callback=self.coords_callback, nodes_callback=self.nodes_callback, ways_callback=self.ways_callback, relations_callback=self.relations_callback) parser.nodes_tag_filter = self.nodes_tag_filter parser.ways_tag_filter = self.ways_tag_filter parser.relations_tag_filter = self.relations_tag_filter parser.marshal_elem_data = self.marshal_elem_data parser.parse(filename)
def __init__(self, mmap_pool, mmap_queue, *args, **kw): self.mmap_pool = mmap_pool self.mmap_queue = mmap_queue XMLParser.__init__(self, *args, **kw) multiprocessing.Process.__init__(self) self.daemon = True
def __init__(self, input): self._parser = OSMParser(nodes_callback=self._parse_nodes) self._nodes = deque() l.debug('Parsing %s', input) self._parser.parse(input)
class ImportDocument(object): def __init__(self, input): self._parser = OSMParser(nodes_callback=self._parse_nodes) self._nodes = deque() l.debug('Parsing %s', input) self._parser.parse(input) def _parse_nodes(self, nodes): for node in nodes: self._nodes.append(node) def _serialize_node(self, f, node): xmlnode = etree.Element('node', {'visible':'true', 'id':str(node[0]), 'lon':str(node[2][0]), 'lat':str(node[2][1])}) for (k,v) in node[1].items(): tag = etree.Element('tag', {'k':k, 'v':v}) xmlnode.append(tag) f.write(etree.tostring(xmlnode, pretty_print=True)) def _serialize_modify_node(self, f, node): # A node is a tuple of (id, version, tags, x, y) xmlnode = etree.Element('node', {'id':str(node[0]), 'version':str(node[1]), 'lon':str(node[3]), 'lat':str(node[4])}) for (k,v) in node[2].items(): tag = etree.Element('tag', {'k':k, 'v':v}) xmlnode.append(tag) f.write(etree.tostring(xmlnode, pretty_print=True)) def _serialize_modify_way(self, f, way): # A way is a tuple of (id, version, tags, nodes) xmlway = etree.Element('way', {'id':str(way[0]), 'version':str(way[1])}) for ref in way[3]: nd = etree.Element('nd', {'ref':str(ref)}) xmlway.append(nd) for (k,v) in way[2].items(): tag = etree.Element('tag', {'k':k, 'v':v}) xmlway.append(tag) f.write(etree.tostring(xmlway, pretty_print=True)) def _serialize_modify_relation(self, f, relation): # A relation is a tuple of (id, version, tags, types, ids, roles) xmlrelation = etree.Element('relation', {'id':str(relation[0]), 'version':str(relation[1])}) typelookup = {'N':'node', 'W':'way', 'R':'relation'} for i in xrange(0, len(relation[3])): member = etree.Element('member', {'type':typelookup[str(relation[3][i])], 'ref':str(relation[4][i]), 'role':str(relation[5][i])}) xmlrelation.append(member) for (k,v) in relation[2].items(): tag = etree.Element('tag', {'k':k, 'v':v}) xmlrelation.append(tag) f.write(etree.tostring(xmlrelation, pretty_print=True)) def remove_existing(self, existing): existing.load_addresses(self._nodes) duplicates = existing.find_duplicates() l.debug('Removing duplicates') self._nodes = filter(lambda node: node[0] not in duplicates, self._nodes) l.debug('%d duplicates removed', len(duplicates)) def remove_changed(self, existing, **kwargs): duplicates = existing.generate_changes(**kwargs) l.debug('Removing changed') self._nodes = filter(lambda node: node[0] not in duplicates, self._nodes) l.debug('%d changed removed', len(duplicates)) def output_osm(self, f): f.write('<?xml version="1.0"?>\n<osm version="0.6" upload="false" generator="addressmerge">\n') for node in self._nodes: self._serialize_node(f, node) f.write('</osm>\n') def output_osc(self, existing, f): f.write('<?xml version="1.0"?>\n<osmChange version="0.6" upload="false" generator="addressmerge">\n') f.write('<modify>\n') for node in existing.get_changed_nodes(): self._serialize_modify_node(f, node) for way in existing.get_changed_ways(): self._serialize_modify_way(f, way) for relation in existing.get_changed_relations(): self._serialize_modify_relation(f, relation) f.write('</modify>\n') f.write('</osmChange>\n')
class ImportDocument(object): def __init__(self, input): self._parser = OSMParser(nodes_callback=self._parse_nodes) self._nodes = deque() l.debug('Parsing %s', input) self._parser.parse(input) def _parse_nodes(self, nodes): for node in nodes: self._nodes.append(node) def _serialize_node(self, f, node): xmlnode = etree.Element( 'node', { 'visible': 'true', 'id': str(node[0]), 'lon': str(node[2][0]), 'lat': str(node[2][1]) }) for (k, v) in node[1].items(): tag = etree.Element('tag', {'k': k, 'v': v}) xmlnode.append(tag) f.write(etree.tostring(xmlnode, pretty_print=True)) def _serialize_modify_node(self, f, node): # A node is a tuple of (id, version, tags, x, y) xmlnode = etree.Element( 'node', { 'id': str(node[0]), 'version': str(node[1]), 'lon': str(node[3]), 'lat': str(node[4]) }) for (k, v) in node[2].items(): tag = etree.Element('tag', {'k': k, 'v': v}) xmlnode.append(tag) f.write(etree.tostring(xmlnode, pretty_print=True)) def _serialize_modify_way(self, f, way): # A way is a tuple of (id, version, tags, nodes) xmlway = etree.Element('way', { 'id': str(way[0]), 'version': str(way[1]) }) for ref in way[3]: nd = etree.Element('nd', {'ref': str(ref)}) xmlway.append(nd) for (k, v) in way[2].items(): tag = etree.Element('tag', {'k': k, 'v': v}) xmlway.append(tag) f.write(etree.tostring(xmlway, pretty_print=True)) def _serialize_modify_relation(self, f, relation): # A relation is a tuple of (id, version, tags, types, ids, roles) xmlrelation = etree.Element('relation', { 'id': str(relation[0]), 'version': str(relation[1]) }) typelookup = {'N': 'node', 'W': 'way', 'R': 'relation'} for i in xrange(0, len(relation[3])): member = etree.Element( 'member', { 'type': typelookup[str(relation[3][i])], 'ref': str(relation[4][i]), 'role': str(relation[5][i]) }) xmlrelation.append(member) for (k, v) in relation[2].items(): tag = etree.Element('tag', {'k': k, 'v': v}) xmlrelation.append(tag) f.write(etree.tostring(xmlrelation, pretty_print=True)) def remove_existing(self, existing): existing.load_addresses(self._nodes) duplicates = existing.find_duplicates() l.debug('Removing duplicates') self._nodes = filter(lambda node: node[0] not in duplicates, self._nodes) l.debug('%d duplicates removed', len(duplicates)) def remove_changed(self, existing, **kwargs): duplicates = existing.generate_changes(**kwargs) l.debug('Removing changed') self._nodes = filter(lambda node: node[0] not in duplicates, self._nodes) l.debug('%d changed removed', len(duplicates)) def output_osm(self, f): f.write( '<?xml version="1.0"?>\n<osm version="0.6" upload="false" generator="addressmerge">\n' ) for node in self._nodes: self._serialize_node(f, node) f.write('</osm>\n') def output_osc(self, existing, f): f.write( '<?xml version="1.0"?>\n<osmChange version="0.6" upload="false" generator="addressmerge">\n' ) f.write('<modify>\n') for node in existing.get_changed_nodes(): self._serialize_modify_node(f, node) for way in existing.get_changed_ways(): self._serialize_modify_way(f, way) for relation in existing.get_changed_relations(): self._serialize_modify_relation(f, relation) f.write('</modify>\n') f.write('</osmChange>\n')
def __init__(self, mmap_pool, mmap_queue, *args, **kw): multiprocessing.Process.__init__(self) XMLParser.__init__(self, *args, **kw) self.daemon = True self.mmap_pool = mmap_pool self.mmap_queue = mmap_queue
if len(args) >= 4: verbose = args[3] if verbose == "True": verbose = True else: verbose = False if len(args) >= 5: sheetName = args[4] if len(args) >= 6: x_index = int(args[5]) if len(args) >= 7: y_index = int(args[6]) if len(args) >= 8: FID_index = int(args[7]) # Here ways_callback tells us which method we are calling for the ways we found. Change to nodes_callback = yourmethod if want to process stores p = XMLParser(ways_callback=parseWays, nodes_callback = parseNodes, coords_callback = coords_callback) p.parse('data/' + filename) # print len(reference) print "Starting Second Round for map" # Write headers, then parse again with open ('processed/roads_' + filename+ '.csv', 'wb') as f: writer = csv.writer(f) header = ["id", "name", "lanes", "road_capacity_from_conversion", "highway", "oneway", "distance", "road_capacity", "point_lat", "point_lon"] w = writer.writerow(header)