Exemplo n.º 1
0
def idageodf_tornado(idadb):
    """
    IdaGeoDataFrame to test geospatial methods
    This refers to the table 'GEO_TORNADO' of the 'SAMPLES' schema
    The table has one geometry column named 'SHAPE' of type 'ST_MULTILINESTRING'
    Don't use it for destructive nor non-destructive methods (modify columns)
    """
    idageodf = ibmdbpy.IdaGeoDataFrame(idadb, 'SAMPLES.GEO_TORNADO')
    return idageodf
Exemplo n.º 2
0
def idageodf_customer(idadb):
    """
    IdaGeoDataFrame to test geospatial methods
    This refers to the table 'GEO_CUSTOMER' of the 'SAMPLES' schema
    The table has one geometry column named 'SHAPE' of type 'ST_POINT'
    Don't use it for destructive nor non-destructive methods (modify columns)
    """
    idageodf = ibmdbpy.IdaGeoDataFrame(idadb,
                                       'SAMPLES.GEO_CUSTOMER',
                                       indexer='OBJECTID')
    return idageodf
Exemplo n.º 3
0
def idageodf_county_view(request, idadb):
    """
    IdaGeoDataFrame to test geospatial methods with lowercase column names.
    It refers to the view 'GEO_COUNTY_VIEW' where the columns of the
    'SAMPLES.GEO_COUNTY' table have been renamed to their lowercase counterparts.
    The view has one geometry column named 'shape' of type 'ST_MULTIPOLYGON'.
    Don't use it for destructive nor non-destructive methods (modify columns).
    """
    def fin():
        try:
            idadb.drop_view("GEO_COUNTY_VIEW")
            idadb.commit()
        except:
            pass

    request.addfinalizer(fin)
    idadb.ida_query(
        'CREATE OR REPLACE VIEW GEO_COUNTY_VIEW AS ' +
        '(SELECT "OBJECTID" as "objectid", "NAME" as "name", "SHAPE" as "shape"  FROM SAMPLES.GEO_COUNTY)'
    )
    idageodf = ibmdbpy.IdaGeoDataFrame(idadb,
                                       'GEO_COUNTY_VIEW',
                                       indexer='objectid')
    return idageodf
Exemplo n.º 4
0
    def _binary_operation_handler(self,
                                  ida2,
                                  db2gse_function,
                                  valid_types_ida1,
                                  valid_types_ida2,
                                  additional_args=None):
        """
        Returns an IdaGeoDataFrame with three columns:
        [
        INDEXERIDA1 : indexer of the first IdaGeoSeries (None if not set),
        INDEXERIDA2 : indexer of the second IdaGeoSeries (None if not set),
        RESULT : the result of the operation
        ]


        Parameters
        ----------
        db2gse_function : str
                Name of the corresponding DB2GSE function.
        valid_types_ida1 : list of str
                Valid input typenames for the first IdaGeoSeries.
        valid_types_ida2 : list of str
                Valid input typenames for the second IdaGeoSeries.
        additional_args : list of str, optional
                Additional arguments for the DB2GSE function.

        Returns
        -------
        IdaGeoDataFrame
        """
        ida1 = self

        # Check if allowed data type
        if not (ida1.dtypes.TYPENAME[0] in valid_types_ida1
                or valid_types_ida1[0] == 'ST_GEOMETRY'):
            raise TypeError("Column " + ida1.column +
                            " has incompatible type.")
        if not (ida2.dtypes.TYPENAME[0] in valid_types_ida2
                or valid_types_ida2[0] == 'ST_GEOMETRY'):
            raise TypeError("Column " + ida2.column +
                            " has incompatible type.")

        # Get the definitions of the columns, which will be the arguments for
        # the DB2GSE function
        column1_for_db2gse = 'IDA1.\"%s\"' % (ida1.geometry.column)
        column2_for_db2gse = 'IDA2.\"%s\"' % (ida2.geometry.column)
        arguments_for_db2gse_function = [
            column1_for_db2gse, column2_for_db2gse
        ]
        if additional_args is not None:
            for arg in additional_args:
                arguments_for_db2gse_function.append(arg)

        # SELECT statement
        select_columns = []
        if hasattr(ida1, '_indexer') and ida1._indexer is not None:
            select_columns.append('IDA1.\"%s\" AS \"INDEXERIDA1\"' %
                                  (ida1.indexer))
        else:
            message = (
                ida1 +
                "has no indexer defined. Please assign index column with set_indexer and retry."
            )
            raise IdaGeoDataFrameError(message)
        if hasattr(ida2, '_indexer') and ida2._indexer is not None:
            select_columns.append('IDA2.\"%s\" AS \"INDEXERIDA2\"' %
                                  (ida2.indexer))
        else:
            message = (
                ida2 +
                "has no indexer defined. Please assign index column with set_indexer and retry."
            )
            raise IdaGeoDataFrameError(message)
        result_column = (db2gse_function + '(' +
                         ','.join(map(str, arguments_for_db2gse_function)) +
                         ')')
        select_columns.append('%s AS \"RESULT\"' % (result_column))
        select_statement = 'SELECT ' + ','.join(select_columns) + ' '

        # FROM clause
        from_clause = ('FROM ' + ida1.name + ' AS IDA1, ' + ida2.name +
                       ' AS IDA2 ')

        # Create a view
        view_creation_query = '(' + select_statement + from_clause + ')'
        viewname = self._idadb._create_view_from_expression(
            view_creation_query)

        idageodf = ibmdbpy.IdaGeoDataFrame(self._idadb,
                                           viewname,
                                           indexer='INDEXERIDA1')
        return idageodf
Exemplo n.º 5
0
    def _binary_operation_handler(self, ida2, db2gse_function,
                                          valid_types_ida1, valid_types_ida2,
                                          additional_args=None):


        """
        Returns an IdaGeoDataFrame with three columns:
        [
        INDEXERIDA1 : indexer of the first IdaGeoSeries (None if not set),
        INDEXERIDA2 : indexer of the second IdaGeoSeries (None if not set),
        RESULT : the result of the operation
        ]


        Parameters
        ----------
        db2gse_function : str
                Name of the corresponding DB2GSE function.
        valid_types_ida1 : list of str
                Valid input typenames for the first IdaGeoSeries.
        valid_types_ida2 : list of str
                Valid input typenames for the second IdaGeoSeries.
        additional_args : list of str, optional
                Additional arguments for the DB2GSE function.

        Returns
        -------
        IdaGeoDataFrame
        """
        ida1 = self
        
        # Check if allowed data type
        if not (ida1.dtypes.TYPENAME[0] in valid_types_ida1 or
                        valid_types_ida1[0] == 'ST_GEOMETRY'):
            raise TypeError("Column " + ida1.column +
                            " has incompatible type.")
        if not (ida2.dtypes.TYPENAME[0] in valid_types_ida2 or
                        valid_types_ida2[0] == 'ST_GEOMETRY'):
            raise TypeError("Column " + ida2.column +
                            " has incompatible type.")

        # Get the definitions of the columns, which will be the arguments for
        # the DB2GSE function
        column1_for_db2gse = ida1.internal_state.columndict[ida1.geometry.column] 
        if column1_for_db2gse[0] == '\"' and column1_for_db2gse[-1] == '\"':
            column1_for_db2gse = column1_for_db2gse[1:-1]
        column2_for_db2gse = ida2.internal_state.columndict[ida2.geometry.column] 
        if column2_for_db2gse[0] == '\"' and column2_for_db2gse[-1] == '\"':
            column2_for_db2gse = column2_for_db2gse[1:-1]
        
        ### added : check if eligible column names ###
        # in order to fix error occuring when geometry column defined as function(other_column)
        # geometry columns must have been explicitly defined and have an eligible alias
        print("column1_for_db2gse: "+column1_for_db2gse)
        print("column2_for_db2gse: "+column2_for_db2gse)
        if "(" in column1_for_db2gse or "ST_" in column1_for_db2gse:
            print("A function pattern was detected in column1_for_db2gse. Please provide the alias of the first geometry column.")
            column1_for_db2gse = str(input("ALIAS_1: "))
        if "(" in column2_for_db2gse or "ST_" in column2_for_db2gse:
            print("A function pattern was detected in column2_for_db2gse. Please provide the alias of the second geometry column.")
            column2_for_db2gse = str(input("ALIAS_2: "))
        ### end ### 

        arguments_for_db2gse_function = []
        arguments_for_db2gse_function.append('IDA1.' + column1_for_db2gse)
        arguments_for_db2gse_function.append('IDA2.' + column2_for_db2gse)
        if additional_args is not None:
            for arg in additional_args:
                arguments_for_db2gse_function.append(arg)

        # SELECT statement
        select_columns = []
        if hasattr(ida1, '_indexer') and ida1._indexer is not None:
            select_columns.append('IDA1.\"%s\" AS \"INDEXERIDA1\"' % (ida1.indexer))
        else:
            message = (ida1 + "has no indexer defined. Please assign index column with set_indexer and retry.")
            raise IdaGeoDataFrameError(message)
        if hasattr(ida2, '_indexer') and ida2._indexer is not None:
            select_columns.append('IDA2.\"%s\" AS \"INDEXERIDA2\"' % (ida2.indexer))
        else:
            message = (ida2 + "has no indexer defined. Please assign index column with set_indexer and retry.")
            raise IdaGeoDataFrameError(message)
        result_column = (
            db2gse_function +
            '(' +
            ','.join(map(str, arguments_for_db2gse_function)) +
            ')'
        )
        select_columns.append('%s AS \"RESULT\"' % (result_column))
        select_statement = 'SELECT ' + ','.join(select_columns) + ' '        
        
        # FROM clause
        from_clause = (
            'FROM ' +
            ida1.name + ' AS IDA1, ' +
            ida2.name + ' AS IDA2 '
        )

        # Create a view
        view_creation_query = '(' + select_statement + from_clause + ')'
        viewname = self._idadb._create_view_from_expression(view_creation_query)

        idageodf = ibmdbpy.IdaGeoDataFrame(self._idadb, viewname,indexer= 'INDEXERIDA1')
        return idageodf