Exemplo n.º 1
0
 def __init__(self):
     super(TimerVertices, self).__init__()
     d = time.time()
     self.obj_shp = self.obj.get_vertices()
     print 'vertices_shp', 1000 * (time.time() - d), self.obj_shp.featureCount()
     d = time.time()
     self.obj = layer.BaseLayer('Point', 'vertices', 'memory')
     self.obj.append(self.obj_shp)
     print 'vertices_mem', 1000 * (time.time() - d), self.obj.featureCount()
Exemplo n.º 2
0
 def get_gml_from_zip(self, gml_path, zip_path, group, layername):
     """Return gml layer from zip if exists and is valid or none"""
     try:
         zf = zipfile.ZipFile(zip_path)
         gml_fp = self.get_path_from_zip(zf, gml_path)
         vsizip_path = "/".join(
             ('/vsizip', zip_path, gml_fp)).replace('\\', '/')
         if group == 'AD':
             vsizip_path += "|layername=" + layername
         gml = layer.BaseLayer(vsizip_path, layername + '.gml', 'ogr')
         if not gml.isValid():
             gml = None
     except IOError:
         gml = None
     return gml
Exemplo n.º 3
0
 def read(self, prov_code):
     if prov_code not in list(andalucia.keys()):
         raise ValueError(_("Province code '%s' not valid") % prov_code)
     csv_fn = csv_name.format(andalucia[prov_code])
     csv_path = os.path.join(self.path, csv_fn)
     url = cdau_url.format(csv_fn)
     if not os.path.exists(csv_path):
         log.info(_("Downloading '%s'"), csv_path)
         download.wget(url, csv_path)
     csv = layer.BaseLayer(csv_path, csv_fn, 'ogr')
     if not csv.isValid():
         raise IOError(_("Failed to load layer '%s'") % csv_path)
     csv.setCrs(QgsCoordinateReferenceSystem(cdau_crs))
     log.info(_("Read %d features in '%s'"), csv.featureCount(), csv_path)
     self.get_metadata(csv_path.replace('.csv', '.txt'))
     csv.source_date = self.src_date
     return csv
Exemplo n.º 4
0
    def read(self, layername, allow_empty=False, force_zip=False):
        """
        Create a QGIS vector layer for a Cadastre layername. Derives the GML 
        filename from layername. Downloads the file if not is present. First try
        to read the ZIP file, if fails try with the GML file.

        Args:
            layername (str): Short name of the Cadastre layer. Any of 
                'building', 'buildingpart', 'otherconstruction', 
                'cadastralparcel', 'cadastralzoning', 'address', 
                'thoroughfarename', 'postaldescriptor', 'adminunitname'
            allow_empty (bool): If False (default), raise a exception for empty
                layer, else returns None
            force_zip (bool): Force to use ZIP file.

        Returns:
            QgsVectorLayer: Vector layer.
        """
        (md_path, gml_path, zip_path, group) = self.get_layer_paths(layername)
        url = setup.prov_url[group].format(code=self.prov_code)
        if not os.path.exists(zip_path) and (not os.path.exists(gml_path)
                                             or force_zip):
            self.get_atom_file(url)
        self.get_metadata(md_path, zip_path)
        if self.is_empty(gml_path, zip_path):
            if not allow_empty:
                raise IOError(_("The layer '%s' is empty") % gml_path)
            else:
                log.info(_("The layer '%s' is empty"), gml_path)
                return None
        gml = self.get_gml_from_zip(gml_path, zip_path, group, layername)
        if gml is None:
            gml = layer.BaseLayer(gml_path, layername + '.gml', 'ogr')
            if not gml.isValid():
                raise IOError(_("Failed to load layer '%s'") % gml_path)
        crs = QgsCoordinateReferenceSystem(self.crs_ref)
        if not crs.isValid():
            raise IOError(_("Could not determine the CRS of '%s'") % gml_path)
        gml.setCrs(crs)
        log.info(_("Read %d features in '%s'"), gml.featureCount(), gml_path)
        gml.source_date = self.src_date
        return gml
Exemplo n.º 5
0
def run():
    qgs = catatom2osm.QgsSingleton()
    for prov_code in setup.valid_provinces:
        url = setup.prov_url['BU'].format(code=prov_code)
        response = download.get_response(url)
        root = etree.fromstring(response.content)
        for entry in root.findall("atom:entry[atom:title]", namespaces=ns):
            title = entry.find('atom:title', ns).text
            zip_code = title[1:6]
            mun = title.replace('buildings', '').strip()[6:]
            url = u"{0}{1}/{2}-{3}/A.ES.SDGC.BU.{2}.zip".format(baseurl, prov_code, zip_code, mun)
            gml_fn = ".".join((setup.fn_prefix, 'BU', zip_code, 'building.gml'))
            download.wget(url, 'temp.zip')
            gml = layer.BaseLayer('/vsizip/temp.zip/'+gml_fn, 'temp', 'ogr')
            sys.stdout.write(' '*70+'\r')
            c = gml.featureCount()
            print zip_code, mun, c
            fh.write(u'{}\t{}\t{}\n'.format(zip_code, mun, c))
    if os.path.exists('temp'):
        os.remove('temp')
Exemplo n.º 6
0
 def __init__(self):
     self.fixture = QgsVectorLayer('test/building.gml', 'building', 'ogr')
     self.obj = layer.BaseLayer("Polygon", "test", "memory")