ogrgrasslayer.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,059 行 · 第 1/2 页

CPP
1,059
字号
    if ( bCursorOpened )    {	db_close_cursor ( poCursor );	bCursorOpened = false;    }    char buf[2000];    sprintf ( buf, "SELECT * FROM %s ", poLink->table );    db_set_string ( poDbString, buf);    if ( pszQuery ) {	sprintf ( buf, "WHERE %s ", pszQuery );	db_append_string ( poDbString, buf);    }    sprintf ( buf, "ORDER BY %s", poLink->key);    db_append_string ( poDbString, buf);    CPLDebug ( "GRASS", "Query: %s", db_get_string(poDbString) );        if ( db_open_select_cursor ( poDriver, poDbString, 		poCursor, DB_SCROLL) == DB_OK )     {	iCurrentCat = -1;	bCursorOpened = true;	CPLDebug ( "GRASS", "num rows = %d", db_get_num_rows ( poCursor ) );    }     else     {	CPLError( CE_Failure, CPLE_AppDefined, "Cannot open cursor.");	return false;    }    return true;}/************************************************************************//*                           ResetSequentialCursor                      *//************************************************************************/bool OGRGRASSLayer::ResetSequentialCursor(){    CPLDebug ( "GRASS", "ResetSequentialCursor" );    int more;    if( db_fetch ( poCursor, DB_FIRST, &more) != DB_OK )     {	CPLError( CE_Failure, CPLE_AppDefined, "Cannot reset cursor.");	return false;    }    if( db_fetch ( poCursor, DB_PREVIOUS, &more) != DB_OK )     {	CPLError( CE_Failure, CPLE_AppDefined, "Cannot reset cursor.");	return false;    }    return true;}/************************************************************************//*                           SetSpatialFilter                           *//************************************************************************/void OGRGRASSLayer::SetSpatialFilter( OGRGeometry * poGeomIn ){    CPLDebug ( "GRASS", "SetSpatialFilter" );    OGRLayer::SetSpatialFilter ( poGeomIn );    if ( poGeomIn == NULL ) {	// Release old if any    	if ( paSpatialMatch ) {	    CPLFree ( paSpatialMatch );	    paSpatialMatch = NULL;	}	return;    }    SetSpatialMatch();}/************************************************************************//*                           SetSpatialMatch                            *//************************************************************************/bool OGRGRASSLayer::SetSpatialMatch(){    CPLDebug ( "GRASS", "SetSpatialMatch" );    if ( !paSpatialMatch )     {	paSpatialMatch = (char *) CPLMalloc ( nTotalCount );    }    memset ( paSpatialMatch, 0x0, nTotalCount );    OGRGeometry *geom;     OGRLineString *lstring = new OGRLineString();    lstring->setNumPoints ( 5 );    geom = lstring;    for ( int i = 0; i < nTotalCount; i++ ) {	int cidx = paFeatureIndex[i];	int cat, type, id;		Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, cidx, &cat, &type, &id );	BOUND_BOX box;	switch ( type ) 	{	    case GV_POINT:	    case GV_LINE:	    case GV_BOUNDARY:		Vect_get_line_box ( poMap, id, &box );		break;	    case GV_AREA:		Vect_get_area_box ( poMap, id, &box );		break;	}			lstring->setPoint( 0, box.W, box.N, 0. );	lstring->setPoint( 1, box.W, box.S, 0. );	lstring->setPoint( 2, box.E, box.S, 0. );	lstring->setPoint( 3, box.E, box.N, 0. );	lstring->setPoint( 4, box.W, box.N, 0. );	if ( FilterGeometry(geom) ) {    	    CPLDebug ( "GRASS", "Feature %d in filter", i );	    paSpatialMatch[i] = 1;	}    }    delete lstring;    return true;}    /************************************************************************//*                           GetNextFeature()                           *//************************************************************************/OGRFeature *OGRGRASSLayer::GetNextFeature(){    CPLDebug ( "GRASS", "OGRGRASSLayer::GetNextFeature" );    OGRFeature  *poFeature = NULL;    int cat;    // Get next iNextId    while ( true ) {	if( iNextId >= nTotalCount ) // No more features	{ 	    // Close cursor / driver if opened 	    if ( bCursorOpened ) 	    {	    	db_close_cursor ( poCursor);	    	bCursorOpened = false;	    }	    if ( poDriver ) 	    {    	    	db_close_database_shutdown_driver ( poDriver );		poDriver = NULL;	    }	    return NULL;	}	// Attributes	if( pszQuery != NULL && !paQueryMatch[iNextId] ) {	    iNextId++;	    continue;	}	// Spatial	if( m_poFilterGeom && !paSpatialMatch[iNextId] ) {	    iNextId++;	    continue;	}		break; // Attributes & spatial filter match    }    OGRGeometry *poOGR = GetFeatureGeometry ( iNextId, &cat );    poFeature = new OGRFeature( poFeatureDefn );    poFeature->SetGeometryDirectly( poOGR );    poFeature->SetFID ( iNextId );    iNextId++;        // Get attributes    CPLDebug ( "GRASS", "bHaveAttributes = %d", bHaveAttributes );    if ( bHaveAttributes )     {	if ( !poDriver ) 	{	    StartDbDriver();	}	if ( poDriver ) {	    if ( !bCursorOpened ) 	    {		OpenSequentialCursor();	    }	    if ( bCursorOpened ) 	    {		dbTable  *table = db_get_cursor_table ( poCursor );		if ( iCurrentCat < cat ) 		{		    while ( true ) {			int more;			if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) 			{			    CPLError( CE_Failure, CPLE_AppDefined, 				      "Cannot fetch attributes.");			    break;			}			if ( !more ) break;			dbColumn *column = db_get_table_column ( table, iCatField );			dbValue *value = db_get_column_value ( column );			iCurrentCat = db_get_value_int ( value );			if ( iCurrentCat >= cat ) break;		    }		}		if ( cat == iCurrentCat )		{		    SetAttributes ( poFeature, table );		} 		else 		{		    CPLError( CE_Failure, CPLE_AppDefined, "Attributes not found.");		}	    }	}    }     else if ( iLayer > 0 ) // Add category    {	poFeature->SetField( 0, cat );    }	        m_nFeaturesRead++;    return poFeature;}/************************************************************************//*                             GetFeature()                             *//************************************************************************/OGRFeature *OGRGRASSLayer::GetFeature( long nFeatureId ){    CPLDebug ( "GRASS", "OGRGRASSLayer::GetFeature nFeatureId = %d", nFeatureId );    int cat;    OGRFeature *poFeature = NULL;    OGRGeometry *poOGR = GetFeatureGeometry ( nFeatureId, &cat );    poFeature = new OGRFeature( poFeatureDefn );    poFeature->SetGeometryDirectly( poOGR );    poFeature->SetFID ( nFeatureId );    // Get attributes    if ( bHaveAttributes && !poDriver )     {	StartDbDriver();    }    if ( poDriver )     {	if ( bCursorOpened ) 	{	    db_close_cursor ( poCursor);	    bCursorOpened = false;	}	CPLDebug ( "GRASS", "Open cursor for key = %d", cat );	char buf[2000];	sprintf ( buf, "SELECT * FROM %s WHERE %s = %d", 		       poLink->table, poLink->key, cat );	db_set_string ( poDbString, buf);	if ( db_open_select_cursor ( poDriver, poDbString, 		    poCursor, DB_SEQUENTIAL) == DB_OK ) 	{	    iCurrentCat = cat; // Not important	    bCursorOpened = true;	} 	else 	{	    CPLError( CE_Failure, CPLE_AppDefined, "Cannot open cursor.");	}	if ( bCursorOpened ) 	{	    int more;	    if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) 	    {		CPLError( CE_Failure, CPLE_AppDefined, "Cannot fetch attributes.");	    } 	    else 	    {		if ( !more ) 		{		    CPLError( CE_Failure, CPLE_AppDefined, "Attributes not found.");		} 		else 		{	    	    dbTable *table = db_get_cursor_table ( poCursor );		    SetAttributes ( poFeature, table );		}	    }	    db_close_cursor ( poCursor);	    bCursorOpened = false;	}    }     else if ( iLayer > 0 ) // Add category    {	poFeature->SetField( 0, cat );    }	        m_nFeaturesRead++;    return poFeature;}/************************************************************************//*                             GetFeatureGeometry()                     *//************************************************************************/OGRGeometry *OGRGRASSLayer::GetFeatureGeometry ( long nFeatureId, int *cat ){    CPLDebug ( "GRASS", "OGRGRASSLayer::GetFeatureGeometry nFeatureId = %d", nFeatureId );    int cidx = paFeatureIndex[(int)nFeatureId];    int type, id;    Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, cidx, cat, &type, &id );    //CPLDebug ( "GRASS", "cat = %d type = %d id = %d", *cat, type, id );    OGRGeometry *poOGR = NULL;    switch ( type ) {	case GV_POINT:        {	    Vect_read_line ( poMap, poPoints, poCats, id);	    poOGR = new OGRPoint( poPoints->x[0], poPoints->y[0], poPoints->z[0] );        }        break;	    	case GV_LINE:	case GV_BOUNDARY:        {	    Vect_read_line ( poMap, poPoints, poCats, id);	    OGRLineString *poOGRLine = new OGRLineString();            poOGRLine->setPoints( poPoints->n_points, 		                  poPoints->x, poPoints->y, poPoints->z );            poOGR = poOGRLine;        }        break;	case GV_AREA:        {	    Vect_get_area_points ( poMap, id, poPoints );	    	    OGRPolygon 		*poOGRPoly;	    poOGRPoly = new OGRPolygon();	    OGRLinearRing       *poRing;	    poRing = new OGRLinearRing();	    poRing->setPoints( poPoints->n_points,		               poPoints->x, poPoints->y, poPoints->z ); 	    poOGRPoly->addRingDirectly( poRing );	    // Islands	    int nisles = Vect_get_area_num_isles ( poMap, id );	    for ( int i = 0; i < nisles; i++ ) {		int isle =  Vect_get_area_isle ( poMap, id, i );		Vect_get_isle_points ( poMap, isle, poPoints );		poRing = new OGRLinearRing();		poRing->setPoints( poPoints->n_points,				   poPoints->x, poPoints->y, poPoints->z ); 		poOGRPoly->addRingDirectly( poRing );	    }	    	    poOGR = poOGRPoly;        }           break;	default: // Should not happen        {	    CPLError( CE_Failure, CPLE_AppDefined, "Unknown GRASS feature type.");	    return NULL;        }    }	        return poOGR;}/************************************************************************//*                          SetAttributes()                             *//************************************************************************/bool OGRGRASSLayer::SetAttributes ( OGRFeature *poFeature, dbTable *table ){    CPLDebug ( "GRASS", "OGRGRASSLayer::SetAttributes" );    for ( int i = 0; i < nFields; i++)     {	dbColumn *column = db_get_table_column ( table, i );	dbValue *value = db_get_column_value ( column );	int ctype = db_sqltype_to_Ctype ( db_get_column_sqltype(column) );	if ( !db_test_value_isnull(value) )	{	    switch ( ctype ) {		case DB_C_TYPE_INT:		    poFeature->SetField( i, db_get_value_int ( value ));		    break; 		case DB_C_TYPE_DOUBLE:		    poFeature->SetField( i, db_get_value_double ( value ));		    break; 		case DB_C_TYPE_STRING:		    poFeature->SetField( i, db_get_value_string ( value ));		    break; 		case DB_C_TYPE_DATETIME:		    db_convert_column_value_to_string ( column, poDbString );		    poFeature->SetField( i, db_get_string ( poDbString ));		    break; 	    }	}		db_convert_column_value_to_string ( column, poDbString );	//CPLDebug ( "GRASS", "val = %s", db_get_string ( poDbString ));    }    return true;}/************************************************************************//*                             SetFeature()                             *//************************************************************************/OGRErr OGRGRASSLayer::SetFeature( OGRFeature *poFeature ){    return OGRERR_FAILURE;}/************************************************************************//*                           CreateFeature()                            *//************************************************************************/OGRErr OGRGRASSLayer::CreateFeature( OGRFeature *poFeature ){    return OGRERR_FAILURE;}/************************************************************************//*                          GetFeatureCount()                           *//*                                                                      *//*      If a spatial filter is in effect, we turn control over to       *//*      the generic counter.  Otherwise we return the total count.      *//*      Eventually we should consider implementing a more efficient     *//*      way of counting features matching a spatial query.              *//************************************************************************/int OGRGRASSLayer::GetFeatureCount( int bForce ){    if( m_poFilterGeom != NULL || m_poAttrQuery != NULL )        return OGRLayer::GetFeatureCount( bForce );            return nTotalCount;}/************************************************************************//*                             GetExtent()                              *//*                                                                      *//*      Fetch extent of the data currently stored in the dataset.       *//*      The bForce flag has no effect on SHO files since that value     *//*      is always in the header.                                        *//*                                                                      *//*      Returns OGRERR_NONE/OGRRERR_FAILURE.                            *//************************************************************************/OGRErr OGRGRASSLayer::GetExtent (OGREnvelope *psExtent, int bForce){    BOUND_BOX box;    Vect_get_map_box ( poMap, &box );    psExtent->MinX = box.W;    psExtent->MinY = box.S;    psExtent->MaxX = box.E;    psExtent->MaxY = box.N;    return OGRERR_NONE;}/************************************************************************//*                           TestCapability()                           *//************************************************************************/int OGRGRASSLayer::TestCapability( const char * pszCap ){    if( EQUAL(pszCap,OLCRandomRead) )        return TRUE;    else if( EQUAL(pszCap,OLCFastFeatureCount) )        return TRUE;    else if( EQUAL(pszCap,OLCFastSpatialFilter) )        return FALSE;    else if( EQUAL(pszCap,OLCFastGetExtent) )        return TRUE;    else if( EQUAL(pszCap,OLCFastSetNextByIndex) )        return TRUE;    else         return FALSE;}/************************************************************************//*                            CreateField()                             *//************************************************************************/OGRErr OGRGRASSLayer::CreateField( OGRFieldDefn *poField, int bApproxOK ){    CPLError( CE_Failure, CPLE_NotSupported,                  "Can't create fields on a GRASS layer.\n");        return OGRERR_FAILURE;}/************************************************************************//*                           GetSpatialRef()                            *//************************************************************************/OGRSpatialReference *OGRGRASSLayer::GetSpatialRef(){    return poSRS;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?