コード例 #1
0
ファイル: ogringreslayer.cpp プロジェクト: Mavrx-inc/gdal
OGRFeature *OGRIngresLayer::RecordToFeature( char **papszRow )

{
/* -------------------------------------------------------------------- */
/*      Create a feature from the current result.                       */
/* -------------------------------------------------------------------- */
    int         iField;
    OGRFeature *poFeature = new OGRFeature( poFeatureDefn );

    poFeature->SetFID( iNextShapeId );
    m_nFeaturesRead++;

/* ==================================================================== */
/*      Transfer all result fields we can.                              */
/* ==================================================================== */
    for( iField = 0;
         iField < (int) poResultSet->getDescrParm.gd_descriptorCount;
         iField++ )
    {
        IIAPI_DATAVALUE *psDV =
            poResultSet->pasDataBuffer + iField;
        IIAPI_DESCRIPTOR *psFDesc =
            poResultSet->getDescrParm.gd_descriptor + iField;
        int     iOGRField;

/* -------------------------------------------------------------------- */
/*      Ignore NULL fields.                                             */
/* -------------------------------------------------------------------- */
        if( psDV->dv_null )
            continue;

/* -------------------------------------------------------------------- */
/*      Handle FID.                                                     */
/* -------------------------------------------------------------------- */
        if( osFIDColumn.size()
            && EQUAL(psFDesc->ds_columnName,osFIDColumn)
            && psFDesc->ds_dataType == IIAPI_INT_TYPE
            && psDV->dv_length == 4 )
        {
            if( papszRow[iField] == NULL )
            {
                CPLError( CE_Failure, CPLE_AppDefined,
                          "NULL primary key in RecordToFeature()" );
                return NULL;
            }

            GInt32 nValue;
            memcpy( &nValue, papszRow[iField], 4 );
            poFeature->SetFID( nValue );
        }

/* -------------------------------------------------------------------- */
/*      Handle Ingres geometry                                           */
/* -------------------------------------------------------------------- */
        if( osGeomColumn.size()
            && EQUAL(psFDesc->ds_columnName,osGeomColumn))
        {
            if( poDS->IsNewIngres() )
            {
                OGRGeometry *poGeometry = NULL;
                unsigned char *pszWKB = (unsigned char *) papszRow[iField];

                // GRGeometryFactory::createFromWkt(&pszWKT, NULL, &poGeometry);
                OGRGeometryFactory::createFromWkb(pszWKB, NULL, &poGeometry, -1);

                poFeature->SetGeometryDirectly(poGeometry);
            }
            else
            {
                poFeature->SetGeometryDirectly(
                    TranslateGeometry( papszRow[iField] ) );
            }
            continue;
        }

/* -------------------------------------------------------------------- */
/*      Transfer regular data fields.                                   */
/* -------------------------------------------------------------------- */
        iOGRField = poFeatureDefn->GetFieldIndex(psFDesc->ds_columnName);
        if( iOGRField < 0 )
            continue;

        switch( psFDesc->ds_dataType )
        {
          case IIAPI_CHR_TYPE:
          case IIAPI_CHA_TYPE:
          case IIAPI_LVCH_TYPE:
          case IIAPI_LTXT_TYPE:
            poFeature->SetField( iOGRField, papszRow[iField] );
            break;

          case IIAPI_VCH_TYPE:
          case IIAPI_TXT_TYPE:
            GUInt16 nLength;
            memcpy( &nLength, papszRow[iField], 2 );
            papszRow[iField][nLength+2] = '\0';
            poFeature->SetField( iOGRField, papszRow[iField]+2 );
            break;

          case IIAPI_INT_TYPE:
            if( psDV->dv_length == 8 )
            {
                GIntBig nValue;
                memcpy( &nValue, papszRow[iField], 8 );
                poFeature->SetField( iOGRField, (int) nValue );
            }
            else if( psDV->dv_length == 4 )
            {
                GInt32 nValue;
                memcpy( &nValue, papszRow[iField], 4 );
                poFeature->SetField( iOGRField, nValue );
            }
            else if( psDV->dv_length == 2 )
            {
                GInt16 nValue;
                memcpy( &nValue, papszRow[iField], 2 );
                poFeature->SetField( iOGRField, nValue );
            }
            else if( psDV->dv_length == 1 )
            {
                GByte nValue;
                memcpy( &nValue, papszRow[iField], 1 );
                poFeature->SetField( iOGRField, nValue );
            }
            break;

          case IIAPI_FLT_TYPE:
            if( psDV->dv_length == 4 )
            {
                float fValue;
                memcpy( &fValue, papszRow[iField], 4 );
                poFeature->SetField( iOGRField, fValue );
            }
            else if( psDV->dv_length == 8 )
            {
                double dfValue;
                memcpy( &dfValue, papszRow[iField], 8 );
                poFeature->SetField( iOGRField, dfValue );
            }
            break;

          case IIAPI_DEC_TYPE:
          {
              IIAPI_CONVERTPARM sCParm;
              char szFormatBuf[30];

              memset( &sCParm, 0, sizeof(sCParm) );

              memcpy( &(sCParm.cv_srcDesc), psFDesc,
                      sizeof(IIAPI_DESCRIPTOR) );
              memcpy( &(sCParm.cv_srcValue), psDV,
                      sizeof(IIAPI_DATAVALUE) );

              sCParm.cv_dstDesc.ds_dataType = IIAPI_CHA_TYPE;
              sCParm.cv_dstDesc.ds_nullable = FALSE;
              sCParm.cv_dstDesc.ds_length = sizeof(szFormatBuf);

              sCParm.cv_dstValue.dv_value = szFormatBuf;

              IIapi_convertData( &sCParm );

              poFeature->SetField( iOGRField, szFormatBuf );
              break;
          }
        }
    }

    return poFeature;
}
コード例 #2
0
OGRFeature *OGRIngresLayer::RecordToFeature( char **papszRow )

{
/* -------------------------------------------------------------------- */
/*      Create a feature from the current result.                       */
/* -------------------------------------------------------------------- */
    int         iField;
    OGRFeature *poFeature = new OGRFeature( poFeatureDefn );

    poFeature->SetFID( iNextShapeId );
    m_nFeaturesRead++;

/* ==================================================================== */
/*      Transfer all result fields we can.                              */
/* ==================================================================== */
    for( iField = 0; 
         iField < (int) poResultSet->getDescrParm.gd_descriptorCount;
         iField++ )
    {
        IIAPI_DATAVALUE *psDV = 
            poResultSet->pasDataBuffer + iField;
        IIAPI_DESCRIPTOR *psFDesc = 
            poResultSet->getDescrParm.gd_descriptor + iField;
        int     iOGRField;

/* -------------------------------------------------------------------- */
/*      Ignore NULL fields.                                             */
/* -------------------------------------------------------------------- */
        if( psDV->dv_null ) 
            continue;

/* -------------------------------------------------------------------- */
/*      Handle FID.                                                     */
/* -------------------------------------------------------------------- */
        if( osFIDColumn.size() 
            && EQUAL(psFDesc->ds_columnName,osFIDColumn) 
            && psFDesc->ds_dataType == IIAPI_INT_TYPE 
            && psDV->dv_length == 4 )
        {
            if( papszRow[iField] == NULL )
            {
                CPLError( CE_Failure, CPLE_AppDefined,
                          "NULL primary key in RecordToFeature()" );
                return NULL;
            }

            GInt32 nValue;
            memcpy( &nValue, papszRow[iField], 4 );
            poFeature->SetFID( nValue );
        }

/* -------------------------------------------------------------------- */
/*      Handle Ingres geometry                                           */
/* -------------------------------------------------------------------- */
        if( osGeomColumn.size() 
            && EQUAL(psFDesc->ds_columnName,osGeomColumn))
        {
        	if( poDS->IsNewIngres() )
        	{
        		OGRGeometry *poGeometry = NULL;
        		unsigned char *pszWKB = (unsigned char *) papszRow[iField];

//        		OGRGeometryFactory::createFromWkt(&pszWKT, NULL, &poGeometry);

                if (poDS->IsVWDB())
                {
                    // VectorWise use wkt to create geometry
                    char **pszWKT = (char **)&pszWKB;
                    OGRGeometryFactory::createFromWkt(pszWKT, NULL, &poGeometry);
                }
                else
                {
        		    OGRGeometryFactory::createFromWkb(pszWKB, NULL, &poGeometry, -1);
                }       

        		poFeature->SetGeometryDirectly(poGeometry);
        	}
        	else
        	{
        		poFeature->SetGeometryDirectly(
        			TranslateGeometry( papszRow[iField] ) );
        	}
            continue;
        }


/* -------------------------------------------------------------------- */
/*      Transfer regular data fields.                                   */
/* -------------------------------------------------------------------- */
        iOGRField = poFeatureDefn->GetFieldIndex(psFDesc->ds_columnName);
        if( iOGRField < 0 )
            continue;

        switch( psFDesc->ds_dataType )
        {
          case IIAPI_CHR_TYPE:
          case IIAPI_CHA_TYPE:
          case IIAPI_LVCH_TYPE:
          case IIAPI_LTXT_TYPE:
            poFeature->SetField( iOGRField, papszRow[iField] );
            break;

          case IIAPI_NCHA_TYPE:          
          case IIAPI_LNVCH_TYPE:
              break;

          case IIAPI_NVCH_TYPE:
              break;

          case IIAPI_VCH_TYPE:
          case IIAPI_TXT_TYPE:
            GUInt16 nLength;
            memcpy( &nLength, papszRow[iField], 2 );
            papszRow[iField][nLength+2] = '\0';
            poFeature->SetField( iOGRField, papszRow[iField]+2 );
            break;

          case IIAPI_INT_TYPE:
            if( psDV->dv_length == 8 )
            {
                GIntBig nValue;
                memcpy( &nValue, papszRow[iField], 8 );
                poFeature->SetField( iOGRField, (int) nValue );
            }
            else if( psDV->dv_length == 4 )
            {
                GInt32 nValue;
                memcpy( &nValue, papszRow[iField], 4 );
                poFeature->SetField( iOGRField, nValue );
            }
            else if( psDV->dv_length == 2 )
            {
                GInt16 nValue;
                memcpy( &nValue, papszRow[iField], 2 );
                poFeature->SetField( iOGRField, nValue );
            }
            else if( psDV->dv_length == 1 )
            {
                GByte nValue;
                memcpy( &nValue, papszRow[iField], 1 );
                poFeature->SetField( iOGRField, nValue );
            }
            break;

          case IIAPI_FLT_TYPE:
            if( psDV->dv_length == 4 )
            {
                float fValue;
                memcpy( &fValue, papszRow[iField], 4 );
                poFeature->SetField( iOGRField, fValue );
            }
            else if( psDV->dv_length == 8 )
            {
                double dfValue;
                memcpy( &dfValue, papszRow[iField], 8 );
                poFeature->SetField( iOGRField, dfValue );
            }
            break;
            
          case IIAPI_DEC_TYPE:
          {
              IIAPI_CONVERTPARM sCParm;
              char szFormatBuf[30];

              memset( &sCParm, 0, sizeof(sCParm) );
              
              memcpy( &(sCParm.cv_srcDesc), psFDesc, 
                      sizeof(IIAPI_DESCRIPTOR) );
              memcpy( &(sCParm.cv_srcValue), psDV, 
                      sizeof(IIAPI_DATAVALUE) );

              sCParm.cv_dstDesc.ds_dataType = IIAPI_CHA_TYPE;
              sCParm.cv_dstDesc.ds_nullable = FALSE;
              sCParm.cv_dstDesc.ds_length = sizeof(szFormatBuf);

              sCParm.cv_dstValue.dv_value = szFormatBuf;

              IIapi_convertData( &sCParm );
              
              poFeature->SetField( iOGRField, szFormatBuf );
              break;
          }

          case IIAPI_DTE_TYPE:
          case IIAPI_DATE_TYPE:
          case IIAPI_TIME_TYPE:
              // First convert to string type then to parse
              char szFormatBuf[30];
              char szDateTime[30];
              IIAPI_CONVERTPARM sCParm;              

              memset( &sCParm, 0, sizeof(sCParm) );

              memcpy( &(sCParm.cv_srcDesc), psFDesc, 
                  sizeof(IIAPI_DESCRIPTOR) );
              memcpy( &(sCParm.cv_srcValue), psDV, 
                  sizeof(IIAPI_DATAVALUE) );

              sCParm.cv_dstDesc.ds_dataType = IIAPI_CHA_TYPE;
              sCParm.cv_dstDesc.ds_nullable = FALSE;
              sCParm.cv_dstDesc.ds_length = sizeof(szFormatBuf);
              sCParm.cv_dstDesc.ds_precision = 0;
              sCParm.cv_dstDesc.ds_scale = 0;
              sCParm.cv_dstDesc.ds_columnType = IIAPI_COL_QPARM;

              sCParm.cv_dstValue.dv_value = szFormatBuf;

              IIapi_convertData( &sCParm );

              /* need to convert 'dd-mmm-yyyy' to  
              ** 'yyyy-mm-dd' which is known to ogr  
              */
              if (!FormatDTE(szFormatBuf, szDateTime))
              {
                  break;
              }

              OGRField psDTE;

              if (!OGRParseDate(szDateTime, &psDTE, 0))
              {
                  CPLError(CE_Failure, CPLE_AppDefined, "can't Parse data time : %s", szDateTime);
              }
              else
              {
                  poFeature->SetField( iOGRField, &psDTE);
              }
              break;

          case IIAPI_BYTE_TYPE:
          case IIAPI_VBYTE_TYPE:
          case IIAPI_LBYTE_TYPE:
              // need to copy?
              poFeature->SetField( iOGRField, papszRow[iField] );
              break;

          default:
              CPLError(CE_Failure, CPLE_AppDefined, "unrecognize column type %d", psFDesc->ds_dataType);
        }
    }

    return poFeature;
}