[gdal-dev] [python-ogr] GetNumPoints from geometry

Ari Jolma ari.jolma at gmail.com
Thu Feb 10 04:08:21 EST 2011


On 02/10/2011 10:28 AM, Frank Broniewski wrote:
> Hi Ari,
>
> thanks for your answer. I am not sure what you mean with return a 
> geometry in scalar context, and I don't have a sound understanding of 
> perl, SWIG and the impacts of it on the objects created by the bindings.
>
> A very quick google search didn't reveal a good explanation of what a 
> scalar in perl is, but from what I found it seems to have something to 
> do with lists in python [1]
> It would be possible to let an object (e.g. OGRPoint) inherit from 
> list and use that as a container, but this is just speculation on my 
> side ;-)

In this case the GetPoint method exists already, so to maintain 
backwards compatibility, its behavior should not be changed. However, 
the behavior is only really defined in the context where it is expected 
to return a list (the coordinates). Thus, it would be possible to 
extend/define its behavior to scalar context, i.e., where it is expected 
to return a single value. For example in Perl, @list is a variable that 
is a list and $scalar is a variable that is a scalar (one thing):

@list = $geometry_object->GetPoint(0);
$scalar = $geometry_object->GetPoint(0);

now @list would be array (x,y) and $scalar would be a Geometry object of 
type Point.

This might be confusing and I don't know if Python supports this kind of 
play with the syntax.

Anyway, as PointN is the standard name, which is not used in GDAL so 
far, we could use that.

Ari

>
> Thanks for the tipp with the documentation. I will use the C API in 
> the future then ...
>
>
> Thanks,
>
> Frank
>
>
> [1] http://docs.python.org/library/functions.html#list
>
> Am 09.02.2011 18:15, schrieb Ari Jolma:
>> On 02/09/2011 05:36 PM, Frank Broniewski wrote:
>>> Yes, thank you. GetPointCount() works. Another thing I noticed:
>>>
>>> point = geometry.GetPoint(point_index)
>>>
>>> point is of type tuple(x, y, z), not as one would guess an OGRPoint()
>>> geometry. At least for me this was unexpected. And the python GetPoint
>>> method does not take a OGRPoint geometry, like it is mentioned here [1]
>>
>> The OGC name of GetPoint is PointN and it should return a Point object
>> according to the standard.
>>
>> The GDAL Perl (which I care for) works the same way, GetPoint returns a
>> list of coordinates. It would be possible to make it return a geometry
>> object in scalar context, but I'm not sure if Python has that 
>> possibility.
>>
>> The GDAL Perl documentation for Geometry object is at
>> http://geoinformatics.tkk.fi/doc/Geo-GDAL/html/class_geo_1_1_o_g_r_1_1_geometry.html 
>>
>> I'm not aware of GDAL Python documentation in the web. There are
>> differences between GDAL C++ API and GDAL C API. For the bindings the C
>> API documentation is usually better:
>> http://www.gdal.org/ogr/ogr__api_8h.html#67b897ffcd88a83832964c3735fa1dcb 
>>
>>
>> Best regards,
>>
>> Ari
>>
>>>
>>> Frank
>>>
>>>
>>> [1]
>>> http://www.gdal.org/ogr/classOGRLineString.html#ab63760022e49a795477f2fd31da059f 
>>>
>>>
>>>
>>> Am 09.02.2011 15:44, schrieb Ari Jolma:
>>>> On 02/09/2011 04:19 PM, Frank Broniewski wrote:
>>>>> Hi,
>>>>>
>>>>> just a quick question: isn't GetNumPoints implemented on a 
>>>>> geometry in
>>>>> ogr python?
>>>>
>>>> There are OGR_G_GetPointCount in ogr_api.h and GetPointCount method in
>>>> the Geometry class in Swig bindings.
>>>>
>>>> Hm. It seems that NumPoints is the name of the method in the OGC 
>>>> simple
>>>> features standard (it is defined for LineStrings).
>>>>
>>>> Ari
>>>>
>>>>> Neither GetNumPoints() nor getNumPoints() (small g) work.
>>>>> The passed in geometry is a geometry object from a
>>>>>
>>>>> geometry = feature.GetGeometryRef()
>>>>>
>>>>> call and the geometry is of type 'LINESTRING' taken from
>>>>>
>>>>> geometry.GetGeometryName() ...
>>>>>
>>>>>
>>>>> code:
>>>>> def line_nodes(geometry):
>>>>> nodes = list()
>>>>>
>>>>> for point_index in xrange(geometry.GetNumPoints()):
>>>>> ogr_point = ogr.OGRPoint()
>>>>> point = geometry.GetPoint(point_index, ogr_point)
>>>>> dimenion = point.GetCoordinateDimension()
>>>>> if dimension == 3:
>>>>> list.append(point_index, point.GetX(), point.GetY(), point.GetZ())
>>>>> else:
>>>>> list.append(point_index, point.GetX(), point.GetY())
>>>>>
>>>>> return nodes
>>>>>
>>>>>
>>>>> raises an AttributeError
>>>>> ...
>>>>> for point_index in xrange(geometry.getNumPoints()):
>>>>> File "C:\Python26\lib\site-packages\osgeo\ogr.py", line 2672, in
>>>>> <lambda>
>>>>> __getattr__ = lambda self, name: _swig_getattr(self, Geometry, name)
>>>>> File "C:\Python26\lib\site-packages\osgeo\ogr.py", line 34, in
>>>>> _swig_getattr
>>>>> raise AttributeError,name
>>>>> AttributeError: GetNumPoints
>>>>>
>>>>>
>>>>> Many thanks
>>>>>
>>>>> Frank
>>>>>
>>>>
>>>> _______________________________________________
>>>> gdal-dev mailing list
>>>> gdal-dev at lists.osgeo.org
>>>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>>>>
>>>
>>>
>>
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>>
>
>



More information about the gdal-dev mailing list