예제 #1
0
    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