[FWTools] Python API

Frank Warmerdam fwarmerdam at gmail.com
Tue Feb 1 23:41:19 EST 2005


On Tue, 1 Feb 2005 18:03:54 -0800, Ryan, Adam <ARyan at co.linn.or.us> wrote:
> 
> Hi.  I've been using the ogr API reference on the web and trying to learn
> how to use ogr.py and gdal.py.  I've got a few simple questions.
> 
> 1. It seems like all these classes have only functions and no properties.
> Is that just the way it is and I should just get used to it?

Adam,

That is pretty much the way it is.  

Howard Butler was experimenting a bit with having some properties on 
OGR objects but his work depended on some special magic in Python 2.3
so we didn't incorporate it into the core.

Also, the gdal.Dataset has has RasterXSize and RasterYSize
properties. 

I looked at handling ogr.Feature attributes as properties but it
turned out to be tricky and potentially a performance issue. 
 
> 2. I want to set the value of the xcoord field to the X value of the point
> features of a shapefile:
> 
> To get the X value, geomRef.GetX() doesn't work (too few arguments) so I
> have to use geomRef.GetX(0).  Is this because in ogr.py there is only a
> geometry class and not a point class?

Yes, it is because a ogr.Geometry is a generic geometry interface for all
types of geometry.  However, I have just committed a change to ogr.py
so that the index defaults to zero if no index is given for the GetX, GetY 
and GetZ methods. 
 
> GetX isn't in the Class Members reference documents but it shows up if I do
> a dir(geomRef).

Reference documents?  There are reference documents?  I normally scan
through gdal.py and ogr.py when I want to understand what methods are
available on the various objects.  Mostly I try to make them analygous to
methods in the underlying C++ API but there are exceptions. 
 
> 3. To set the value, I do:
> 
>     feature.SetField(xcoordi,geomRef.GetX(0))
>     src_layer.SetFeature(feature)
> 
> Which works but there are no values beyond the decimal place and there are
> if I print out geomRef.GetX(0).  So I tried this but it didn't seem to work:
> 
>     feature.SetField(coordXfldi,fpformat.fix(geomRef.GetX(0),4))
> 
> Any hints would be great.

You are saying that the value of the attribute on the feature seems to 
be truncated to an integer?  What is the definition of the field?  
 
> So far, this stuff is the bomb and the documentation is great.  Cheers,

Hmm, given that there isn't documentation for the Python interface, I'm glad
you feel that way!  
 
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


More information about the FWTools mailing list