[Gdal-dev] Re: C# Geometry Classes?
Mateusz Loskot
mateusz at loskot.net
Thu Mar 22 02:45:56 EDT 2007
Simon Perkins wrote:
> Frank Warmerdam wrote:
>> Simon Perkins wrote:
>>> This seems to me like a big chunk that's missing from the OGR C
>>> interface, but maybe I'm just missing some aspect of how the C
>>> API can be used. For my money, I'm thinking that this
>>> functionality should be added into the basic C API (and thence
>>> picked up from there by SWIG et al). Don't people using the C API
>>> to OGR want more geometry specific functionality? I still haven't
>>> worked out how one can deal with all but the simplest geometries
>>> through the C interface - for instance identifying inner and
>>> outer rings of a polygon.
>>
>> Can you be more specific about what you think is missing?
>
> Well if it's not obvious to you then probably the only thing that's
> missing is my knowledge of OGR... But here's an example of where I'm
> confused.
>
> Suppose I've just read a feature from a layer.
>
> I call OGR_G_GetGeometryType() to get the geometry type and get back
> wkbPolygon.
>
> Presumably I can then call OGR_G_ GetPointCount() to get the number
> of points, and then OGR_G_GetPoint() to get the polygon points. But
> if the polygon contains multiple rings, how do I know which points
> belong to which ring?
First, you need to get handle to a ring as a subgeometry (see below).
> Or can I invoke OGR_G_GetGeometryRef() on the polygon to get access
> to the rings? If this is the case, is the first ring defined to be
> the outer ring?
Yes.
> Just struggling with lack of documentation or examples using the C
> OGR API...
I hope following example will help you:
///////////////////////////////////////////////////////////////
#include <iostream>
#include <ogr_api.h>
#include <cpl_port.h>
int main()
{
using std::cout;
using std::endl;
// Polygon with 2 rings
char* wkt = "POLYGON ((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0), (1 1 0, 2 1
0, 2 2 0, 1 1 0))";
OGRGeometryH g = NULL;
OGRErr err = OGR_G_CreateFromWkt(&wkt, NULL, &g);
cout << "Rings count: " << OGR_G_GetGeometryCount(g) << endl;
// Ring 1
OGRGeometryH ring1 = NULL;
ring1 = OGR_G_GetGeometryRef(g, 0);
cout << "Ring 1: " << OGR_G_GetPointCount(ring1) << endl;
// Ring 2
OGRGeometryH ring2 = NULL;
ring2 = OGR_G_GetGeometryRef(g, 1);
cout << "Ring 2: " << OGR_G_GetPointCount(ring2) << endl;
OGR_G_DestroyGeometry(g);
return 0;
}
///////////////////////////////////////////////////////////////
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net
More information about the Gdal-dev
mailing list