Example #1
0
    def layer_will_be_removed(self, layer_id):
        """Function that triggered when layer will be removed.

        :param layer_id: Removed layer id.
        :type layer_id: QString
        """
        layer = QgsMapLayerRegistry.instance().mapLayer(layer_id)
        layer_names = layer.name().split('/')
        if len(layer_names) > 1 and \
                ('relationships' in layer.name() or 'parties' in layer.name()):
            try:
                organization_slug, project_slug, attribute = layer_names
                Utilities.add_tabular_layer(
                    layer,
                    organization_slug,
                    project_slug,
                    attribute
                )

                setting_value = '%s/%s' % (
                    organization_slug,
                    project_slug
                )

                setting_file = get_setting('layers_added')

                if setting_file:
                    setting_files = setting_file.split(',')
                    setting_files.remove(setting_value)
                    setting_file = ','.join(setting_files)
                    set_setting('layers_added', setting_file)
                else:
                    self.project_update_wizard.setEnabled(False)

                if not get_setting('layers_added'):
                    self.project_update_wizard.setEnabled(False)

            except ValueError:
                return
    def relationships_layer(self, vector_layers):
        """Create relationship layer.

        :param vector_layers: List of QGS vector layer in memory
        :type vector_layers: list of QgsVectorLayer

        :return: Relationship layer
        :rtype: QgsVectorLayer
        """

        organization_slug = self.project['organization']['slug']
        project_slug = self.project['slug']
        attribute = 'relationships'

        api = u'/api/v1/organizations/{organization_slug}/projects/' \
              u'{project_slug}/spatial/{spatial_unit_id}/relationships/'

        csv_path = get_csv_path(organization_slug, project_slug, attribute)

        if os.path.isfile(csv_path):
            os.remove(csv_path)

        relationship_layer = tools.create_memory_layer(
            layer_name='%s/%s/%s' %
            (organization_slug, project_slug, attribute),
            geometry=QGis.NoGeometry,
            fields=[
                QgsField('spatial_id', QVariant.String, "string"),
                QgsField('rel_id', QVariant.String, "string"),
                QgsField('rel_name', QVariant.String, "string"),
                QgsField('party_id', QVariant.String, "string"),
                QgsField('attributes', QVariant.String, "string"),
            ])

        QgsMapLayerRegistry.instance().addMapLayer(relationship_layer)

        for vector_layer in vector_layers:
            # Add relationship layer id to spatial attribute table
            spatial_id_index = vector_layer.fieldNameIndex('id')

            for index, feature in enumerate(vector_layer.getFeatures()):
                attributes = feature.attributes()
                spatial_unit_id = attributes[spatial_id_index]
                spatial_api = api.format(organization_slug=organization_slug,
                                         project_slug=project_slug,
                                         spatial_unit_id=spatial_unit_id)
                connector = ApiConnect(get_url_instance() + spatial_api)
                status, results = connector.get(paginated=True)

                if not status or len(results) == 0:
                    continue

                try:
                    for result in results:
                        relationship_layer.startEditing()
                        fet = QgsFeature()
                        questionnaire_attr = result['attributes']
                        if not questionnaire_attr:
                            questionnaire_attr = '-'
                        else:
                            questionnaire_attr = json.dumps(questionnaire_attr)
                        fet.setAttributes([
                            attributes[spatial_id_index],
                            result['id'],
                            result['tenure_type'],
                            result['party']['id'],
                            questionnaire_attr,
                        ])
                        relationship_layer.addFeature(fet, True)
                        relationship_layer.commitChanges()
                except (IndexError, KeyError):
                    continue

        self.process_attributes(relationship_layer)

        Utilities.add_tabular_layer(relationship_layer, organization_slug,
                                    project_slug, attribute)

        return relationship_layer
    def parties_layer(self):
        """Create parties layer.

        :param vector_layer: QGS vector layer in memory
        :type vector_layer: QgsVectorLayer
        """

        organization_slug = self.project['organization']['slug']
        project_slug = self.project['slug']
        attribute = 'parties'

        csv_path = get_csv_path(organization_slug, project_slug, attribute)

        if os.path.isfile(csv_path):
            os.remove(csv_path)

        api = u'/api/v1/organizations/{organization_slug}/projects/' \
              u'{project_slug}/parties/'.format(
                organization_slug=organization_slug,
                project_slug=project_slug)

        connector = ApiConnect(get_url_instance() + api)
        status, results = connector.get(paginated=True)

        if not status:
            return

        party_layer = tools.create_memory_layer(
            layer_name='%s/%s/%s' %
            (organization_slug, project_slug, attribute),
            geometry=QGis.NoGeometry,
            fields=[
                QgsField('id', QVariant.String, "string"),
                QgsField('name', QVariant.String, "string"),
                QgsField('type', QVariant.String, "string"),
                QgsField('attributes', QVariant.String, "string"),
            ])

        QgsMapLayerRegistry.instance().addMapLayer(party_layer)

        for party in results:
            party_layer.startEditing()
            feature = QgsFeature()
            questionnaire_attr = party['attributes']
            if not questionnaire_attr:
                questionnaire_attr = '-'
            else:
                questionnaire_attr = json.dumps(questionnaire_attr)
            feature.setAttributes([
                party['id'], party['name'], party['type'], questionnaire_attr
            ])
            party_layer.addFeature(feature, True)
            party_layer.commitChanges()
            QCoreApplication.processEvents()

        self.process_attributes(party_layer)

        Utilities.add_tabular_layer(party_layer, organization_slug,
                                    project_slug, attribute)

        return party_layer