def convert_field(self, layer_name, field):
        field_as_string = str(field)
        xd_col = '{0}_xd'.format(field).lower()
        parsed_col = '{0}_parsed'.format(field).lower()
        target_layer = self.data.GetLayerByName(layer_name)

        # target_layer.GetLayerDefn().GetFieldIndex(parsed_col) raises errors when the field does not
        # exist with older versions of OGR
        while target_layer.FindFieldIndex(xd_col, 1) >= 0:
            xd_col = increment(xd_col)

        while target_layer.FindFieldIndex(parsed_col, 1) >= 0:
            parsed_col = increment(parsed_col)

        target_layer.CreateField(ogr.FieldDefn(xd_col, ogr.OFTInteger64))
        xd_col_index = target_layer.GetLayerDefn().GetFieldIndex(xd_col)
        target_layer.CreateField(ogr.FieldDefn(parsed_col, ogr.OFTString))
        parsed_col_index = target_layer.GetLayerDefn().GetFieldIndex(
            parsed_col)

        for feat in target_layer:

            if not feat:
                continue

            string_field = feat[field_as_string]

            if string_field:
                xd, parsed = timeparse(str(string_field))

                feat.SetField(xd_col_index, xd)
                feat.SetField(parsed_col_index, parsed)

                target_layer.SetFeature(feat)

            # prevent segfaults
            feat = None
        conn = db.connections[settings.OSGEO_DATASTORE]
        cursor = conn.cursor()
        query = """
        DO $$
        BEGIN
        IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname='bigdate') THEN
        CREATE DOMAIN bigdate bigint;
        END IF;
        END;
        $$;

        ALTER TABLE %s ALTER COLUMN %s TYPE bigdate;
        """ % (quote_ident(layer_name), quote_ident(xd_col))

        cursor.execute(query)

        return xd_col
    def convert_field(self, layer_name, field):
        field_as_string = str(field)
        xd_col = '{0}_xd'.format(field).lower()
        parsed_col = '{0}_parsed'.format(field).lower()
        target_layer = self.data.GetLayerByName(layer_name)

        # target_layer.GetLayerDefn().GetFieldIndex(parsed_col) raises errors when the field does not
        # exist with older versions of OGR
        while target_layer.FindFieldIndex(xd_col, 1) >= 0:
            xd_col = increment(xd_col)

        while target_layer.FindFieldIndex(parsed_col, 1) >= 0:
            parsed_col = increment(parsed_col)

        target_layer.CreateField(ogr.FieldDefn(xd_col, ogr.OFTInteger64))
        xd_col_index = target_layer.GetLayerDefn().GetFieldIndex(xd_col)
        target_layer.CreateField(ogr.FieldDefn(parsed_col, ogr.OFTString))
        parsed_col_index = target_layer.GetLayerDefn().GetFieldIndex(parsed_col)

        for feat in target_layer:

            if not feat:
                continue

            string_field = feat[field_as_string]

            if string_field:
                xd, parsed = timeparse(str(string_field))

                feat.SetField(xd_col_index, xd)
                feat.SetField(parsed_col_index, parsed)

                target_layer.SetFeature(feat)

            # prevent segfaults
            feat = None
        conn = db.connections[settings.OSGEO_DATASTORE]
        cursor = conn.cursor()
        query = """
        DO $$
        BEGIN
        IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname='bigdate') THEN
        CREATE DOMAIN bigdate bigint;
        END IF;
        END;
        $$;

        ALTER TABLE %s ALTER COLUMN %s TYPE bigdate;
        """ % (quote_ident(layer_name), quote_ident(xd_col))

        cursor.execute(query)

        return xd_col
    def handle(self, layer, layer_config, *args, **kwargs):
        """
        Gets existing geonode layer and configures time extents
        by querying postgresql data table
        """

        # Configure time extent

        if self.has_start:
            logger.debug(
                'Configuring Start Date Range for column %s',
                layer_config['start_date'])
            start_date_col = quote_ident(layer_config['start_date'])

        if self.has_end:
            logger.debug(
                'Configuring End Date Range for column %s',
                layer_config['end_date'])
            end_date_col = quote_ident(layer_config['end_date'])

        conn = db.connections[settings.OSGEO_DATASTORE]
        cursor = conn.cursor()
        scrub_layer_name = quote_ident(layer)
        logger.debug('Getting min/max for %s', scrub_layer_name)

        mint = None
        maxt = None

        if self.has_start and self.has_end:
            query = 'SELECT min(%s), max(%s) FROM %s;' % (
                start_date_col, end_date_col, scrub_layer_name)
        elif self.has_start:
            query = 'SELECT min(%s), max(%s) FROM %s;' % (
                start_date_col, start_date_col, scrub_layer_name)
        elif self.has_end:
            query = 'SELECT min(%s), max(%s) FROM %s;' % (
                end_date_col, end_date_col, scrub_layer_name)
        cursor.execute(query)
        mint, maxt = cursor.fetchone()

        logger.debug('mint %s maxt %s', mint, maxt)

        self.geonode_layer.temporal_extent_start = mint
        self.geonode_layer.temporal_extent_end = maxt
        self.geonode_layer.save()

        return 'Temporal Extent Configured'
Esempio n. 4
0
    def handle(self, layer, layer_config, *args, **kwargs):
        """
        Gets existing geonode layer and configures time extents
        by querying postgresql data table
        """

        # Configure time extent

        if self.has_start:
            logger.debug('Configuring Start Date Range for column %s',
                         layer_config['start_date'])
            start_date_col = quote_ident(layer_config['start_date'])

        if self.has_end:
            logger.debug('Configuring End Date Range for column %s',
                         layer_config['end_date'])
            end_date_col = quote_ident(layer_config['end_date'])

        conn = db.connections[settings.OSGEO_DATASTORE]
        cursor = conn.cursor()
        scrub_layer_name = quote_ident(layer)
        logger.debug('Getting min/max for %s', scrub_layer_name)

        mint = None
        maxt = None

        if self.has_start and self.has_end:
            query = 'SELECT min(%s), max(%s) FROM %s;' % (
                start_date_col, end_date_col, scrub_layer_name)
        elif self.has_start:
            query = 'SELECT min(%s), max(%s) FROM %s;' % (
                start_date_col, start_date_col, scrub_layer_name)
        elif self.has_end:
            query = 'SELECT min(%s), max(%s) FROM %s;' % (
                end_date_col, end_date_col, scrub_layer_name)
        cursor.execute(query)
        mint, maxt = cursor.fetchone()

        logger.debug('mint %s maxt %s', mint, maxt)

        self.geonode_layer.temporal_extent_start = mint
        self.geonode_layer.temporal_extent_end = maxt
        self.geonode_layer.save()

        return 'Temporal Extent Configured'