Ejemplo n.º 1
0
    def all_data(self):
        """Return all data from this step.

        :returns: step 1 data
        :rtype: dict
        """
        data = dict()
        data['organisation'] = self.selected_organisation()
        data['project_name'] = self.project_name()
        data['project_url'] = self.project_url()
        data['description'] = self.project_description_text.toPlainText()
        data['private'] = self.is_project_private.isChecked()

        # Get contacts
        data['contacts'] = []
        contact_item_list = self.project_contact_list.selectedItems()
        for contact_item in contact_item_list:
            contact = contact_item.data(Qt.UserRole)
            contact_data = {}
            if contact.name:
                contact_data['name'] = contact.name
            if contact.phone:
                contact_data['tel'] = contact.phone
            if contact.email:
                contact_data['email'] = contact.email
            data['contacts'].append(contact_data)

        # Get extent
        layer = self.selected_layer()
        if self.use_layer_extents.isChecked():
            # Layer extent
            extent = layer.extent()
        else:
            # Canvas extent
            extent = self.parent.iface.mapCanvas().extent()

        geometry = QgsGeometry().fromRect(extent)
        data['extent'] = geometry.exportToGeoJSON()

        # Save layer to geojson format
        output_file = get_path_data(project_slug='temp_project')
        output_file = os.path.join(output_file, '%s.geojson' % 'temp_project')

        result = QgsVectorFileWriter.writeAsVectorFormat(
            layer, output_file, 'utf-8', layer.crs(), 'GeoJson')

        if result == QgsVectorFileWriter.NoError:
            LOGGER.debug('Wrote layer to geojson: %s' % output_file)
            with open(output_file) as json_data:
                layer_data = json.load(json_data)
                data['locations'] = layer_data
            os.remove(output_file)
        else:
            LOGGER.error('Failed with error: %s' % result)
        return data