def remove_tags(input_filename, output_fp, tags_to_keep, close_output_fp=True): output_writer = OSMWriter(fp=output_fp) remover = TagRemover(output_writer, tags_to_keep) parser = OSMParser(ways_callback=remover.ways, nodes_callback=remover.nodes, concurrency=1) parser.parse(input_filename) output_writer.close(close_file=close_output_fp)
def generate_osm(compact_formatting): stream = StringIO() xml = OSMWriter(fp=stream, compact_formatting=compact_formatting) xml.node(1, 10, 30, {"highway": "yes"}, version=2) xml.way(1, {'pub': 'yes'}, [123]) xml.relation(1, {'type': 'boundary'}, [('node', 1), ('way', 2, 'outer')]) xml.close(close_file=False) return stream.getvalue()
def testSimple(self): string = StringIO() xml = OSMWriter(fp=string) xml.node(1, 10, 30, {"highway": "yes"}, version=2) xml.way(1, {'pub': 'yes'}, [123]) xml.relation(1, {'type': 'boundary'}, [('node', 1), ('way', 2, 'outer')]) xml.close(close_file=False) # Different python versions can write XML in different ways, (eg order # of attributes). This makes simple string comparison fail. So simple # parse & dump canonicalises it output = ET.tostring(ET.fromstring(string.getvalue())) exected_output = ET.tostring(ET.fromstring('<?xml version="1.0" encoding="utf-8"?>\n<osm version="0.6" generator="osmwriter">\n <node lat="10" version="2" lon="30" id="1">\n <tag k="highway" v="yes"></tag>\n </node>\n <way id="1">\n <nd ref="123"></nd>\n <tag k="pub" v="yes"></tag>\n </way>\n <relation id="1">\n <member ref="1" type="node" />\n <member ref="2" role="outer" type="way" />\n <tag k="type" v="boundary" />\n </relation>\n</osm>')) self.assertEqual(output, exected_output)
def testSimple(self): string = StringIO() xml = OSMWriter(fp=string) xml.node(1, 10, 30, {"highway": "yes"}, version=2) xml.way(1, {'pub': 'yes'}, [123]) xml.relation(1, {'type': 'boundary'}, [('node', 1), ('way', 2, 'outer')]) xml.close(close_file=False) # Different python versions can write XML in different ways, (eg order # of attributes). This makes simple string comparison fail. So simple # parse & dump canonicalises it output = ET.tostring(ET.fromstring(string.getvalue())) exected_output = ET.tostring( ET.fromstring( '<?xml version="1.0" encoding="utf-8"?>\n<osm version="0.6" generator="osmwriter">\n <node lat="10" version="2" lon="30" id="1">\n <tag k="highway" v="yes"></tag>\n </node>\n <way id="1">\n <nd ref="123"></nd>\n <tag k="pub" v="yes"></tag>\n </way>\n <relation id="1">\n <member ref="1" type="node" />\n <member ref="2" role="outer" type="way" />\n <tag k="type" v="boundary" />\n </relation>\n</osm>' )) self.assertEqual(output, exected_output)
def create_osm(conn, tab_node, osm_file, user, visible, version, changeset, cur_time): sql_box = "SELECT ST_Extent(geom) FROM {};".format(tab_node) bbox = [ float(ii) for ii in cursor(conn, sql_box, ret=True)[0][0][4:-1].replace( ',', ' ').split(' ') ] string = StringIO() xml = OSMWriter(fp=string) xml.bounds(bbox) xml = create_node(xml, conn, tab_node, user, visible, version, changeset, cur_time) xml = create_way(xml, conn, tab_node, user, visible, version, changeset, cur_time) # xml = create_relation(xml, cur_time) xml.close(close_file=False) output = ET.tostring(ET.fromstring(string.getvalue())) head = bytes('<?xml version="1.0" encoding="UTF-8"?>\n', 'utf-8') output = head + output with open(osm_file, 'wb') as w: w.write(output)
# Skript postupne projde vsechny lezecke oblasti na strankach CHS # Ke kazde skale stahne jeji GPS souradnice, jmeno a dalsi atributy import re from urllib.request import urlopen from bs4 import BeautifulSoup from osmwriter import OSMWriter xml = OSMWriter("chs.osm.xml") # Pocet sklanich oblasti rockCount = 17220 for i in range(1, rockCount + 1): try: page = urlopen('https://www.horosvaz.cz/skaly-skala-' + str(i)) except: print(i, ':', sep='') continue # Rozdelim html soup = BeautifulSoup(page, 'html.parser') # Ziskam jmeno name_box = soup.find('h1', attrs={'class': 'menu5 small-h1'}) name = name_box.text.strip() # Ziskma souradnice coordinates_box = soup.find('a', attrs={'class': 'map mapycz'}) if (coordinates_box == None):