[Gdal-dev] OGR code examples

Chapman, Martin MChapman at sanz.com
Wed Jul 14 16:35:40 EDT 2004


Michele,

Check out the code below.  It will give you the basic idea of how to get
geometries and features from ogr:


COgrShapeLayer* COgrDataSource::ExecuteSQL(string sStatement,
	
CSXBasicGeometry* pSpatialFilter,
	
string sDialect)
{
	try
	{
		if (!m_pDatasource)
			throw "Invalid data source null in
COgrDataSource::ExecuteSQL().";

		OGRSpatialReference* pSpatialRef =
m_pDatasource->GetLayer(0)->GetSpatialRef();
		OGRLinearRing* pRing = NULL;

		if (dynamic_cast< CSXPolygon*>(pSpatialFilter))
		{
			CSXPolygon* pPolygon = (CSXPolygon*)
pSpatialFilter;
			pRing = new OGRLinearRing();
			vector< double>::iterator it;
			vector< double>* pPoints =
pPolygon->GetPoints();
			double nX = 0.0, nY = 0.0;

			for (it = pPoints->begin(); it !=
pPoints->end(); it++)
			{
				nY = *it;
				it++;
				nX = *it;
				pRing->addPoint(nX, nY);
			}

			pRing->addPoint(pPoints->at(1), pPoints->at(0));
		}
		else
			throw "Invalid spatial reference data type in
COgrDataSource::ExecuteSQL().";

		OGRPolygon* pPolygon = new OGRPolygon();
		if (pSpatialRef)
pPolygon->assignSpatialReference(pSpatialRef);
		pPolygon->addRingDirectly(pRing);

		OGRLayer* pLayer = m_pDatasource->ExecuteSQL((const
char*) sStatement.c_str(),
	
pPolygon,
	
(const char*) sDialect.c_str());
		if (pLayer == NULL) throw (char*) CPLGetLastErrorMsg();

		COgrShapeLayer* pShapeLayer = CreateShapeLayer(pLayer);
		m_pDatasource->ReleaseResultSet(pLayer);
		if (CPLGetLastErrorNo() != OGRERR_NONE) throw (char*)
CPLGetLastErrorMsg();
		return pShapeLayer;
	}
	catch(exception e)
		{throw e;}
	catch(char* e)
		{throw e;}
	catch(...)
		{throw "An unexpected error occurred in
COgrDataSource::ExecuteSQL().";}
}

CSXFeatureTable* COgrDataSource::CreateFeatureTable(OGRLayer* pOgrLayer)
{
	try
	{
		if (!pOgrLayer)
			throw "Invalid parameter null in
COgrDataSource::CreateFeatureTable().";

		OGRFeatureDefn* pFeatureDefn =
pOgrLayer->GetLayerDefn();
		int nFieldCount = pFeatureDefn->GetFieldCount();
		const char* pszLayerName = (char*)
pFeatureDefn->GetName();

		CSXFeatureTable* pFeatureTable = new
CSXFeatureTable(pszLayerName, nFieldCount + 1);

		pFeatureTable->SetColumnName(0, "FID");
		pFeatureTable->SetType(0, DBF_TYPE_NUMERIC);
		pFeatureTable->SetLength(0, 4);
		pFeatureTable->SetDecimalCount(0, 0);

		for (int i = 0; i < nFieldCount; i++)
		{
			OGRFieldDefn* pFieldDefn =
pFeatureDefn->GetFieldDefn(i);
			const char* pszNameRef =
pFieldDefn->GetNameRef();
			unsigned char cDataType =
GetShapeDataType(pFieldDefn->GetType());
			unsigned char cWidth = (unsigned char)
pFieldDefn->GetWidth();
			unsigned char cPrecision = (unsigned char)
pFieldDefn->GetPrecision();

			pFeatureTable->SetColumnName(i + 1, pszNameRef);
			pFeatureTable->SetType(i + 1, cDataType);
			pFeatureTable->SetLength(i + 1, cWidth);
			pFeatureTable->SetDecimalCount(i + 1,
cPrecision);
		}

		OGRFeature* pOgrFeature = pOgrLayer->GetNextFeature();
		while (pOgrFeature)
		{
			vector< string>* pRecord = new vector< string>;
			char pszBuffer [sizeof(long) * 8 + 1];
            sprintf(pszBuffer, "%d", pOgrFeature->GetFID());
			pRecord->push_back(pszBuffer);

			for (long j = 0; j < nFieldCount; j++)
	
pRecord->push_back(pOgrFeature->GetFieldAsString(j));

			pFeatureTable->AddRecord(pRecord);
			delete pOgrFeature;
			pOgrFeature = pOgrLayer->GetNextFeature();
		}

		pOgrLayer->ResetReading();
		return pFeatureTable;
	}
	catch(exception e)
		{throw e;}
	catch(char* e)
		{throw e;}
	catch(...)
		{throw "An unexpected error occurred in
COgrDataSource::CreateFeatureTable().";}
}

COgrGraphicList* COgrDataSource::GetPointGeometry(OGRLayer* pOgrLayer,
CSXDrawingAttributes* pDrawingAttributes)
{
	try
	{
		if (!pOgrLayer || !pDrawingAttributes)
			throw "Invalid parameter null in
COgrDataSource::GetPointGeometry().";

		double nLat, nLon;
		CSXGraphic* pSXPoint = NULL;
		OGRPoint* pOgrPoint = NULL;
		vector< float> vXYPoint;
		COgrGraphicList* pList = new COgrPointList();

		OGRFeature* pFeature = pOgrLayer->GetNextFeature();

		while (pFeature != NULL)
		{
			OGRGeometry* pGeometry =
pFeature->GetGeometryRef();
			if (!pGeometry) continue;

			pOgrPoint = (OGRPoint*) pGeometry;
			long nFID = pFeature->GetFID();
			int nShapeType = (int)
pGeometry->getGeometryType();
			if (nShapeType == SHAPE_TYPE_NULL)
				continue;

			nLon = pOgrPoint->getX();
			nLat = pOgrPoint->getY();
			pSXPoint = new COgrPoint(nLat, nLon);
			pSXPoint->SetAppObject((unsigned int) nFID);
			pDrawingAttributes->SetTo((CSXGraphic*)
pSXPoint);

			pList->Add(pSXPoint);
			pFeature = pOgrLayer->GetNextFeature();
		}

		pOgrLayer->ResetReading();
		return pList;
	}
	catch(exception e)
		{throw e;}
	catch(char* e)
		{throw e;}
	catch(...)
		{throw "An unexpected error occurred in
COgrDataSource::GetPointGeometry().";}
}

COgrGraphicList* COgrDataSource::GetPolyGeometry(OGRLayer* pOgrLayer,
CSXDrawingAttributes* pDrawingAttributes, int nShapeType)
{
	try
	{
		if (!pOgrLayer || !pDrawingAttributes)
			throw "Invalid parameter null in
COgrDataSource::GetPolyGeometry().";

		COgrGraphicList* pList = NULL;
		OGREnvelope pEnvelope;

		if (nShapeType == SHAPE_TYPE_POLYLINE)
			pList = new COgrPolyLineList();
		else if (nShapeType == SHAPE_TYPE_POLYGON)
			pList = new COgrPolygonList();

		pOgrLayer->ResetReading();
		OGRFeature* pFeature = pOgrLayer->GetNextFeature();

		while (pFeature != NULL)
		{
			OGRGeometry* pGeometry =
pFeature->GetGeometryRef();
			if (!pGeometry)
			{
				pFeature = pOgrLayer->GetNextFeature();
				continue;
			}

			int nShapeType = SHAPE_TYPE_NULL;
			int nGeomType = pGeometry->getGeometryType();
			if (nGeomType == wkbUnknown)
			{
				pFeature = pOgrLayer->GetNextFeature();
				continue;
			}
			else if (nGeomType == wkbLineString || nGeomType
== wkbMultiLineString)
				nShapeType = SHAPE_TYPE_POLYLINE;
			else if (nGeomType == wkbPolygon || nGeomType ==
wkbMultiPolygon)
				nShapeType = SHAPE_TYPE_POLYGON;

			long nVertexCount = 0;
			long nFID = pFeature->GetFID();
			int nPartCount = 1;

			if (nGeomType == wkbLineString)
				nPartCount = 1;
			else if (nGeomType == wkbMultiLineString)
				nPartCount = ((OGRMultiLineString*)
pGeometry)->getNumGeometries();
			else if (nGeomType == wkbPolygon)
				nPartCount =
((OGRPolygon*)pGeometry)->getNumInteriorRings() + 1;
			else if (nGeomType == wkbMultiPolygon)
				nPartCount = ((OGRMultiPolygon*)
pGeometry)->getNumGeometries();

			CSXGraphic* pPolygon = NULL;
			COgrGraphicList* pSubList = NULL;

			if (nPartCount > 1)
			{
				if (nShapeType == SHAPE_TYPE_POLYLINE)
					pSubList = new
COgrPolyLineList();
				else if (nShapeType ==
SHAPE_TYPE_POLYGON)
					pSubList = new
COgrPolygonList();
			}

			for (long j = 0; j < nPartCount; j++)
			{
				vector< double>* pPoints = new vector<
double>;

				if (nShapeType == SHAPE_TYPE_POLYLINE)
				{
					if (nGeomType == wkbLineString)
					{
						OGRLineString*
pLinearString = (OGRLineString*) pGeometry;
						nVertexCount =
pLinearString->getNumPoints();

						for (int n = 0; n <
nVertexCount; n++)
						{
							double nLambda =
pLinearString->getX(n);
							double nPhi =
pLinearString->getY(n);

	
pPoints->push_back((double) nPhi);
	
pPoints->push_back((double) nLambda);
						}
					}
					else if (nGeomType ==
wkbMultiLineString)
					{
						OGRMultiLineString*
pMultiLineString = (OGRMultiLineString*) pGeometry;
						OGRLineString*
pLinearString = (OGRLineString*) pMultiLineString->getGeometryRef(j);
						nVertexCount =
pLinearString->getNumPoints();

						for (int n = 0; n <
nVertexCount; n++)
						{
							double nLambda =
pLinearString->getX(n);
							double nPhi =
pLinearString->getY(n);

	
pPoints->push_back((double) nPhi);
	
pPoints->push_back((double) nLambda);
						}
					}
				}
				else if (nShapeType ==
SHAPE_TYPE_POLYGON)
				{
					if (nGeomType == wkbPolygon)
					{
						OGRLinearRing*
pLinearRing = NULL;

						if (j == 0)
							pLinearRing =
((OGRPolygon*)pGeometry)->getExteriorRing();
						else
							pLinearRing =
((OGRPolygon*)pGeometry)->getInteriorRing(j - 1);

						nVertexCount =
pLinearRing->getNumPoints();

						for (int n = 0; n <
nVertexCount; n++)
						{
							double nLambda =
pLinearRing->getX(n);
							double nPhi =
pLinearRing->getY(n);

							//if (nFID ==
1240)
								//cout
<< "Lat: " << nPhi << " Lon: " << nLambda << endl;

	
pPoints->push_back((double) nPhi);
	
pPoints->push_back((double) nLambda);
						}
					}
					else if (nGeomType ==
wkbMultiPolygon)
					{
						OGRLinearRing*
pLinearRing = NULL;
						OGRMultiPolygon*
pMultiPolygon = (OGRMultiPolygon*) pGeometry;
						OGRPolygon* pOGRPolygon
= (OGRPolygon*) pMultiPolygon->getGeometryRef(j);

						if (j == 0)
							pLinearRing =
pOGRPolygon->getExteriorRing();
						else
							pLinearRing =
pOGRPolygon->getInteriorRing(j - 1);

						nVertexCount =
pLinearRing->getNumPoints();

						for (int n = 0; n <
nVertexCount; n++)
						{
							double nLambda =
pLinearRing->getX(n);
							double nPhi =
pLinearRing->getY(n);

	
pPoints->push_back((double) nPhi);
	
pPoints->push_back((double) nLambda);
						}
					}
				}

				if (nShapeType == SHAPE_TYPE_POLYLINE)
					pPolygon = new
COgrPolyLine(pPoints, DECIMAL_DEGREES, LINETYPE_STRAIGHT);
				else if (nShapeType ==
SHAPE_TYPE_POLYGON)
					pPolygon = new
COgrPolygon(pPoints, DECIMAL_DEGREES, LINETYPE_STRAIGHT);

				pPolygon->SetAppObject((unsigned int)
nFID);
				pPolygon->SetAppObjectPart((int) j);

				if (pDrawingAttributes != NULL)
	
pDrawingAttributes->SetTo((CSXGraphic*) pPolygon);

				if
(dynamic_cast<COgrPolyLine*>(pPolygon))
				{
					CSXColor clear(0.0f, 0.0f, 0.0f,
0.0f);
					pPolygon->SetFillColor(&clear);
				}

				if (pSubList != NULL)
	
pSubList->AddSXGraphic(pPolygon);
			}

			if (pSubList != NULL)
			{
				pGeometry->getEnvelope(&pEnvelope);
				vector< double>* pExtents = new vector<
double>;
				pExtents->push_back((double)
pEnvelope.MinY);
				pExtents->push_back((double)
pEnvelope.MinX);
				pExtents->push_back((double)
pEnvelope.MaxY);
				pExtents->push_back((double)
pEnvelope.MaxX);
				pSubList->SetExtents(pExtents);
				pSubList->SetAppObject((unsigned int)
nFID);
				pList->Add(pSubList);
			}
			else
				pList->Add(pPolygon);

			pFeature = pOgrLayer->GetNextFeature();
		}

		pOgrLayer->ResetReading();
		return pList;
	}
	catch(exception e)
		{throw e;}
	catch(char* e)
		{throw e;}
	catch(...)
		{throw "An unexpected error occurred in
COgrDataSource::GetPolyGeometry().";}
}

If you have questions let me know.
Martin


-----Original Message-----
From: Frank Warmerdam [mailto:warmerdam at pobox.com] 
Sent: Wednesday, July 14, 2004 2:30 PM
To: Sanges Michele
Cc: gdal-dev at xserve.flids.com
Subject: Re: [Gdal-dev] OGR code examples


Sanges Michele wrote:
> Are there some code examples of how reading data files by means the 
> OGR library, in C++? Thanks.

Hi,

I am not aware of C++ examples other than the utility programs like
ogrinfo.cpp and ogr2ogr.cpp.

Best regards,
-- 
---------------------------------------+--------------------------------
---------------------------------------+------
I set the clouds in motion - turn up   | Frank Warmerdam,
warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

_______________________________________________
Gdal-dev mailing list
Gdal-dev at xserve.flids.com
http://xserve.flids.com/mailman/listinfo/gdal-dev



More information about the Gdal-dev mailing list