Пример #1
0
    def read_osm(self, ql, filename):
        """
        Reads a OSM data set from a OSM XML file. If the file not exists,
        downloads data from overpass using ql query

        Args:
            ql (str): Query to put in the url
            filename (str): File to read/write

        Returns
            Osm: OSM data set
        """
        osm_path = os.path.join(self.path, filename)
        if not os.path.exists(osm_path):
            log.info(_("Downloading '%s'") % filename)
            query = overpass.Query(self.cat.boundary_search_area).add(ql)
            if log.getEffectiveLevel() == logging.DEBUG:
                query.download(osm_path, log)
            else:
                query.download(osm_path)
        with open(osm_path, 'rb') as fo:
            data = osmxml.deserialize(fo)
        if len(data.elements) == 0:
            msg = _("No OSM data were obtained from '%s'") % filename
            log.warning(msg)
            report.warnings.append(msg)
        else:
            log.info(_("Read '%s': %d nodes, %d ways, %d relations"), filename,
                     len(data.nodes), len(data.ways), len(data.relations))
        return data
Пример #2
0
 def __init__(self):
     mun = '38012'
     address_fn = 'test/address.gml'
     address_fn = BASEPATH + '{0}/A.ES.SDGC.AD.{0}.gml'.format(mun)
     self.address_gml = QgsVectorLayer(address_fn, 'address', 'ogr')
     self.tn_gml = QgsVectorLayer(address_fn + '|layername=thoroughfarename', 'tn', 'ogr')
     self.pd_gml = QgsVectorLayer(address_fn + '|layername=postaldescriptor', 'pd', 'ogr')
     self.au_gml = QgsVectorLayer(address_fn + '|layername=adminUnitname', 'au', 'ogr')
     assert(self.address_gml.isValid())
     assert(self.tn_gml.isValid())
     assert(self.pd_gml.isValid())
     assert(self.au_gml.isValid())
     osm_path = BASEPATH + mun + '/current_highway.osm'
     fo = open(osm_path, 'r')
     highway_osm = osmxml.deserialize(fo)
     self.highway = layer.HighwayLayer()
     self.highway.read_from_osm(highway_osm)
     self.highway.reproject(self.address_gml.crs())
     self.obj = layer.AddressLayer()
     self.obj.append(self.address_gml)
     self.obj.join_field(self.tn_gml, 'TN_id', 'gml_id', ['text'], 'TN_')
     self.obj.join_field(self.au_gml, 'AU_id', 'gml_id', ['text'], 'AU_')
     self.obj.join_field(self.pd_gml, 'PD_id', 'gml_id', ['postCode'])
     fid = random.randrange(self.obj.featureCount())
     request = QgsFeatureRequest().setFilterFids([fid])
     self.ad = self.obj.getFeatures(request).next()
     self.index = self.highway.get_index()
     self.features = {feat.id(): feat for feat in self.highway.getFeatures()}
Пример #3
0
 def set_up(self):
     mun = '38012'
     osm_path = BASEPATH + mun + '/current_building.osm'
     fo = open(osm_path, 'r')
     self.obj = osmxml.deserialize(fo)
     self.to_clean = []
     for i, el in enumerate(self.obj.elements):
         if i % 10 == 0:
             self.to_clean.append(el)
Пример #4
0
 def test_deserialize(self):
     attrs = dict(upload='1', version='2', generator='3')
     root = etree.Element('osm', attrs)
     nodexml = etree.Element('node', dict(id='-1', lon='4', lat='0'))
     nodexml.append(etree.Element('tag', dict(k='entrance', v='yes')))
     nodexml.append(
         etree.Element('tag', dict(k='addr:street', v="Calle la Ñ")))
     nodexml.append(etree.Element('tag', dict(k='addr:housenumber', v='7')))
     root.append(nodexml)
     wayxml = etree.Element('way', dict(id='-100'))
     for (i, node) in enumerate([(12, 0), (14, 0), (14, 2), (12, 2),
                                 (12, 0)]):
         nodexml = etree.Element(
             'node',
             dict(id=str(-i - 10), lon=str(node[0]), lat=str(node[1])))
         root.append(nodexml)
         wayxml.append(etree.Element('nd',
                                     dict(ref=str(nodexml.get('id')))))
     wayxml.append(etree.Element('tag', dict(k='leisure',
                                             v='swiming_pool')))
     root.append(wayxml)
     wayxml = etree.Element('way', dict(id='-101'))
     for (i, node) in enumerate([(0, 0), (10, 0), (10, 6), (0, 6), (0, 0)]):
         nodexml = etree.Element(
             'node',
             dict(id=str(-i - 20), lon=str(node[0]), lat=str(node[1])))
         root.append(nodexml)
         wayxml.append(etree.Element('nd',
                                     dict(ref=str(nodexml.get('id')))))
     root.append(wayxml)
     wayxml = etree.Element('way', dict(id='-102'))
     for (i, node) in enumerate([(8, 1), (9, 1), (9, 2), (8, 2), (8, 1)]):
         nodexml = etree.Element(
             'node',
             dict(id=str(-i - 30), lon=str(node[0]), lat=str(node[1])))
         root.append(nodexml)
         wayxml.append(etree.Element('nd',
                                     dict(ref=str(nodexml.get('id')))))
     root.append(wayxml)
     relxml = etree.Element('relation', dict(id='-200'))
     relxml.append(
         etree.Element('member', dict(type='way', ref='-101',
                                      role='outter')))
     relxml.append(
         etree.Element('member', dict(type='way', ref='-102',
                                      role='inner')))
     relxml.append(
         etree.Element('member', dict(type='relation', ref='-201')))
     relxml.append(etree.Element('tag', dict(k='building',
                                             v='residential')))
     root.append(relxml)
     relxml = etree.Element('relation', dict(id='-201'))
     relxml.append(etree.Element('tag', dict(k='test',
                                             v='nested relation')))
     relxml.append(
         etree.Element('member', dict(type='way', ref='-101',
                                      role='dummy')))
     root.append(relxml)
     root.append(etree.Element('dummy'))
     fo = BytesIO(etree.tostring(root))
     result = osmxml.deserialize(fo)
     self.assertEqual(result.upload, '1')
     self.assertEqual(result.version, '2')
     self.assertEqual(result.generator, '3')
     self.assertEqual(len(result.nodes), 16)
     for osmnode in result.nodes:
         xmlnode = root.find("node[@id='%d']" % osmnode.id)
         self.assertEqual(float(xmlnode.get('lon')), osmnode.x)
         self.assertEqual(float(xmlnode.get('lat')), osmnode.y)
     n = result.get('-1')
     self.assertEqual(n.tags['entrance'], 'yes')
     self.assertEqual(n.tags['addr:street'], "Calle la Ñ")
     self.assertEqual(n.tags['addr:housenumber'], '7')
     self.assertEqual(len(result.ways), 3)
     for osmway in result.ways:
         xmlway = root.find("way[@id='%d']" % osmway.id)
         for nd in xmlway.findall('nd'):
             self.assertIn(nd.get('ref'), [str(w.id) for w in osmway.nodes])
     w = result.get('-100', 'w')
     self.assertEqual(w.tags['leisure'], 'swiming_pool')
     self.assertEqual(len(result.relations), 2)
     osmrel = result.get(-200, 'r')
     xmlrel = root.find("relation[@id='%d']" % osmrel.id)
     roles = []
     for m in xmlrel.findall('member'):
         self.assertIn(m.get('ref'), [str(x.ref) for x in osmrel.members])
         roles.append(m.get('role'))
     self.assertEqual(roles, ['outter', 'inner', None])
     r = result.get(-200, 'r')
     self.assertEqual(r.tags['building'], 'residential')
     nxml = etree.Element('note')
     nxml.text = 'foobar'
     root.append(nxml)
     mxml = etree.Element('meta')
     mxml.set('foo', 'bar')
     root.append(mxml)
     csxml = etree.Element('changeset')
     csxml.append(etree.Element('tag', dict(k='type', v='import')))
     root.append(csxml)
     fo = BytesIO(etree.tostring(root))
     result = osmxml.deserialize(fo)
     self.assertEqual(result.tags, dict(type='import'))
     self.assertEqual(result.note, 'foobar')
     self.assertEqual(result.meta, dict(foo='bar'))
     root = etree.Element('osm')
     nodexml = etree.Element('node', dict(id='-50', lon='0', lat='4'))
     root.append(nodexml)
     fo = BytesIO(etree.tostring(root))
     result = osmxml.deserialize(fo, result)
     self.assertEqual(len(result.nodes), 17)
     root = etree.Element('osm')
     wayxml = etree.Element('way', dict(id='-103', version='1'))
     wayxml.append(etree.Element('nd', dict(ref='-99')))
     root.append(wayxml)
     relxml = etree.Element('relation', dict(id='-202'))
     relxml.append(
         etree.Element('member', dict(type='way', ref='-199',
                                      role='dummy')))
     root.append(relxml)
     fo = BytesIO(etree.tostring(root))
     result = osmxml.deserialize(fo, result)
     self.assertEqual(len(result.nodes), 17)
     self.assertEqual(len(result.ways), 4)
     self.assertEqual(len(result.relations), 3)
     self.assertEqual(result.get(-103, 'w').version, '2')
     self.assertEqual(result.get(-202, 'r').version, None)