Exemplo n.º 1
0
def gdalinfo(coverage, epsg=None, update={}):

    p = Path(coverage.name)
    layername = slugify(p.name[:-len(p.suffix)]).replace('-', '_')
    table_id = update.get(layername, '{0}_{1}'.format(layername,
                                                      str(uuid4())[:7]))

    xmin, ymin, xmax, ymax = coverage.extent
    if epsg and is_valid_epsg(epsg):
        pass
    else:
        epsg = get_epsg(coverage)

    SupportedCrs = apps.get_model(app_label='idgo_admin',
                                  model_name='SupportedCrs')

    try:
        SupportedCrs.objects.get(auth_name='EPSG', auth_code=epsg)
    except SupportedCrs.DoesNotExist:
        raise NotSupportedSrsError('SRS Not Supported')

    return {
        'id': table_id,
        'epsg': epsg,
        'bbox': transform(bounds_to_wkt(xmin, ymin, xmax, ymax), epsg),
        'extent': ((xmin, ymin), (xmax, ymax))
    }
Exemplo n.º 2
0
    def update_organisation(self, organisation):
        ckan_organisation = self.get_organisation(
            str(organisation.ckan_id), include_datasets=True)

        ckan_organisation.update({
            'title': organisation.legal_name,
            'name': slugify(organisation.legal_name)[:100],
            'description': organisation.description,
            'extras': [
                {'key': 'email', 'value': organisation.email or ''},
                {'key': 'phone', 'value': organisation.phone or ''},
                {'key': 'website', 'value': organisation.website or ''},
                {'key': 'address', 'value': organisation.address or ''},
                {'key': 'postcode', 'value': organisation.postcode or ''},
                {'key': 'city', 'value': organisation.city or ''}]})

        try:
            if organisation.logo:
                ckan_organisation['image_url'] = \
                    urljoin(DOMAIN_NAME, organisation.logo.url)
        except ValueError:
            pass

        self.call_action('organization_update', **ckan_organisation)

        for package in ckan_organisation['packages']:
            self.call_action('package_owner_org_update', id=package['id'],
                             organization_id=ckan_organisation['id'])
Exemplo n.º 3
0
 def add_organisation(self, organisation):
     params = {
         'id': str(organisation.ckan_id),
         'name': slugify(organisation.legal_name)[:100],
         'title': organisation.legal_name,
         'description': organisation.description,
         'extras': [
             {'key': 'email', 'value': organisation.email or ''},
             {'key': 'phone', 'value': organisation.phone or ''},
             {'key': 'website', 'value': organisation.website or ''},
             {'key': 'address', 'value': organisation.address or ''},
             {'key': 'postcode', 'value': organisation.postcode or ''},
             {'key': 'city', 'value': organisation.city or ''}],
         'state': 'active'}
     try:
         params['image_url'] = urljoin(DOMAIN_NAME, organisation.logo.url)
     except ValueError:
         pass
     self.call_action('organization_create', **params)
Exemplo n.º 4
0
def handle_pust_request(request, dataset_name=None):
    # name -> slug
    # published -> private
    user = request.user
    dataset = None
    if dataset_name:
        for instance in handler_get_request(request):
            if instance.slug == dataset_name:
                dataset = instance
                break
        if not instance:
            raise Http404()

    # TODO: Vérifier les droits

    data = getattr(request, request.method).dict()

    organisation_slug = data.get('organisation')
    if organisation_slug:
        try:
            organisation = Organisation.objects.get(slug=organisation_slug)
        except Organisation.DoesNotExist as e:
            raise GenericException(details=e.__str__())
    elif dataset:
        organisation = dataset.organisation
    else:
        organisation = None

    license_slug = data.get('license')
    if data.get('license'):
        try:
            license = License.objects.get(slug=license_slug)
        except License.DoesNotExist as e:
            raise GenericException(details=e.__str__())
    elif dataset:
        license = dataset.license
    else:
        license = None

    data_form = {
        'title': data.get('title', dataset and dataset.title),
        'slug': data.get('name', slugify(data.get('title'))),
        'description': data.get('description'),
        # 'thumbnail' -> request.FILES
        'keywords': data.get('keywords'),
        'categories': data.get('categories'),
        'date_creation': data.get('date_creation'),
        'date_modification': data.get('date_modification'),
        'date_publication': data.get('date_publication'),
        'update_frequency': data.get('update_frequency'),
        # 'geocover'
        'granularity': data.get('granularity', 'indefinie'),
        'organisation': organisation.pk,
        'license': license.pk,
        'support': data.get('support', True),
        'data_type': data.get('type'),
        'owner_name': data.get('owner_name'),
        'owner_email': data.get('owner_email'),
        'broadcaster_name': data.get('broadcaster_name'),
        'broadcaster_email': data.get('broadcaster_email'),
        'published': not data.get('private', False),
    }

    pk = dataset and dataset.pk or None
    include = {'user': user, 'id': pk, 'identification': pk and True or False}
    form = Form(data_form, request.FILES, instance=dataset, include=include)
    if not form.is_valid():
        raise GenericException(details=form._errors)

    data = form.cleaned_data
    kvp = {
        'title': data['title'],
        'slug': data['slug'],
        'description': data['description'],
        'thumbnail': data['thumbnail'],
        # keywords
        # categories
        'date_creation': data['date_creation'],
        'date_modification': data['date_modification'],
        'date_publication': data['date_publication'],
        'update_frequency': data['update_frequency'],
        'geocover': data['geocover'],
        'granularity': data['granularity'],
        'organisation': data['organisation'],
        'license': data['license'],
        'support': data['support'],
        # data_type
        'owner_email': data['owner_email'],
        'owner_name': data['owner_name'],
        'broadcaster_name': data['broadcaster_name'],
        'broadcaster_email': data['broadcaster_email'],
        'published': data['published'],
    }

    try:
        with transaction.atomic():
            if dataset:
                for k, v in kvp.items():
                    setattr(dataset, k, v)
            else:
                kvp['editor'] = user
                save_opts = {'current_user': user, 'synchronize': False}
                dataset = Dataset.default.create(save_opts=save_opts, **kvp)

            dataset.categories.set(data.get('categories', []), clear=True)
            keywords = data.get('keywords')
            if keywords:
                dataset.keywords.clear()
                for k in keywords:
                    dataset.keywords.add(k)
            dataset.data_type.set(data.get('data_type', []), clear=True)
            dataset.save(current_user=user, synchronize=True)
    except ValidationError as e:
        form.add_error('__all__', e.__str__())
    except CkanBaseError as e:
        form.add_error('__all__', e.__str__())
    else:
        if dataset_name:
            send_dataset_update_mail(user, dataset)
        else:
            send_dataset_creation_mail(user, dataset)
        return dataset
    raise GenericException(details=form.__str__())
Exemplo n.º 5
0
def ogr2postgis(ds,
                epsg=None,
                limit_to=1,
                update={},
                filename=None,
                encoding='utf-8'):
    sql = []
    tables = []

    layers = ds.get_layers()
    if len(layers) > limit_to:
        raise ExceedsMaximumLayerNumberFixedError(count=len(layers),
                                                  maximum=limit_to)
    layers.encoding = encoding
    # else:
    for layer in layers:
        layername = slugify(layer.name).replace('-', '_')

        if layername == 'ogrgeojson':
            p = Path(ds._datastore.name)
            layername = slugify(p.name[:-len(p.suffix)]).replace('-', '_')

        if epsg and is_valid_epsg(epsg):
            pass
        else:
            epsg = get_epsg(layer)

        SupportedCrs = apps.get_model(app_label='idgo_admin',
                                      model_name='SupportedCrs')

        try:
            SupportedCrs.objects.get(auth_name='EPSG', auth_code=epsg)
        except SupportedCrs.DoesNotExist:
            raise NotSupportedSrsError('SRS Not Supported')

        xmin = layer.extent.min_x
        ymin = layer.extent.min_y
        xmax = layer.extent.max_x
        ymax = layer.extent.max_y

        table_id = update.get(layername,
                              '{0}_{1}'.format(layername,
                                               str(uuid4())[:7]))
        if table_id[0].isdigit():
            table_id = '_{}'.format(table_id)

        tables.append({
            'id': table_id,
            'epsg': epsg,
            'bbox': bounds_to_wkt(xmin, ymin, xmax, ymax),
            'extent': ((xmin, ymin), (xmax, ymax))
        })

        attrs = {}
        for i, k in enumerate(layer.fields):
            t = handle_ogr_field_type(layer.field_types[i].__qualname__,
                                      n=layer.field_widths[i],
                                      p=layer.field_precisions[i])
            attrs[k] = t

        # Erreur dans Django
        # Lorsqu'un 'layer' est composé de 'feature' de géométrie différente,
        # `ft.geom.__class__.__qualname__ == feat.geom_type.name is False`
        #
        #       > django/contrib/gis/gdal/feature.py
        #       @property
        #       def geom_type(self):
        #           "Return the OGR Geometry Type for this Feture."
        #           return OGRGeomType(capi.get_fd_geom_type(self._layer._ldefn))
        #
        # La fonction est incorrecte puisqu'elle se base sur le 'layer' et non
        # sur le 'feature'
        #
        # Donc dans ce cas on définit le type de géométrie de la couche
        # comme générique (soit 'Geometry')
        # Mais ceci est moche :
        try:
            test = set(str(feature.geom.geom_type) for feature in layer)
        except Exception as e:
            logger.exception(e)
            raise WrongDataError()
        # else:
        if test == {'Polygon', 'MultiPolygon'}:
            geometry = 'MultiPolygon'
        elif test == {'Polygon25D', 'MultiPolygon25D'}:
            geometry = 'MultiPolygonZ'
        elif test == {'LineString', 'MultiLineString'}:
            geometry = 'MultiLineString'
        elif test == {'LineString25D', 'MultiLineString25D'}:
            geometry = 'MultiLineStringZ'
        elif test == {'Point', 'MultiPoint'}:
            geometry = 'MultiPoint'
        elif test == {'Point25D', 'MultiPoint25D'}:
            geometry = 'MultiPointZ'
        else:
            geometry = len(test) > 1 \
                and 'Geometry' or handle_ogr_geom_type(layer.geom_type)

        sql.append(
            CREATE_TABLE.format(
                attrs=',\n  '.join(
                    ['"{}" {}'.format(k, v) for k, v in attrs.items()]),
                # description=layer.name,
                epsg=epsg,
                geometry=geometry,
                owner=OWNER,
                mra_datagis_user=MRA_DATAGIS_USER,
                schema=SCHEMA,
                table=str(table_id),
                the_geom=THE_GEOM,
                to_epsg=TO_EPSG))

        for feature in layer:
            attrs = {}
            for field in feature.fields:
                k = field.decode()
                try:
                    v = feature.get(k)
                except DjangoUnicodeDecodeError as e:
                    logger.exception(e)
                    raise DataDecodingError()
                if isinstance(v, type(None)):
                    attrs[k] = 'null'
                elif isinstance(
                        v, (datetime.date, datetime.time, datetime.datetime)):
                    attrs[k] = "'{}'".format(v.isoformat())
                elif isinstance(v, str):
                    attrs[k] = "'{}'".format(v.replace("'", "''"))
                else:
                    attrs[k] = "{}".format(v)

            if geometry.startswith('Multi'):
                geom = "ST_Multi(ST_GeomFromtext('{wkt}', {epsg}))"
            else:
                geom = "ST_GeomFromtext('{wkt}', {epsg})"

            sql.append(
                INSERT_INTO.format(attrs_name=', '.join(
                    ['"{}"'.format(x) for x in attrs.keys()]),
                                   attrs_value=', '.join(attrs.values()),
                                   geom=geom.format(epsg=epsg,
                                                    wkt=feature.geom),
                                   owner=OWNER,
                                   schema=SCHEMA,
                                   table=str(table_id),
                                   the_geom=THE_GEOM,
                                   to_epsg=TO_EPSG))

    for table_id in update.values():
        rename_table(table_id, '__{}'.format(table_id))

    with connections[DATABASE].cursor() as cursor:
        for q in sql:
            try:
                cursor.execute(q)
            except Exception as e:
                logger.exception(e)
                # Revenir à l'état initial
                for table_id in [table['id'] for table in tables]:
                    drop_table(table_id)
                for table_id in update.values():
                    rename_table('__{}'.format(table_id), table_id)
                # Puis retourner l'erreur
                raise SQLError(e.__str__())

    for table_id in update.values():
        drop_table('__{}'.format(table_id))

    return tables
Exemplo n.º 6
0
 def filename(self):
     if self.ftp_file:
         return self.ftp_file.name
     if self.up_file:
         return self.up_file.name
     return '{}.{}'.format(slugify(self.title), self.format.lower())
Exemplo n.º 7
0
def _up_file_upload_to(instance, filename):
    return slugify(filename, exclude_dot=False)