[gdal-dev] Re: Layer->InsertFeature

Ari Jolma ari.jolma at tkk.fi
Thu Dec 27 04:06:55 EST 2007


Brian Hamlin kirjoitti:
>   Row() and Tuple() calls. These look useful, but without any 
> description, how can they be really used?
> Reading the short lines of documentation on the web site does little 
> to really explain the parameters or how they are written. Following 
> the example code closely, I still do not run..
>
> $layer = $datasource->CreateLayer( 'ptsLyr', undef, 'Point' );
> $layer->Schema( Fields => [ { Name => 'EAS_ID', Type => 'Integer' ),
>         { ShortName => 'String' } ] );
>
> $layer->InsertFeature( { EAS_ID => 0,
>         Geometry => { Points => [3,2] } });
>
> this Points thing .. ??  mysteriously fails...
> Cant Call method Points on an undefined value


Brian,

There are indeed new methods "Row" and "Tuple" in the GDAL-Perl API. The 
idea is to supply one-line methods for getting and setting data of a 
feature or a layer. Both methods are defined for Layer and Feature 
(Geo::OGR::Layer and Geo::OGR::Feature) objects. The methods are 
essentially similar except that "Row" takes a hash and "Tuple" takes a 
list as a parameter, or, if getting data, "Row" returns a hashref and 
"Tuple" returns a list.

If hashes are used, data is referred to with keys: FID, Geometry, or a 
field name. If lists are used, data is in the order: FID, Geometry, 
field names in the order in which they were defined. If hash is used, 
only those fields are set for which data is provided. If list is used 
FID and Geometry may be undefined, and if they are, then they are not 
set. In the get methods, the FID may be undefined, as GetNextFeature is 
then called internally.

The Geometry value in the set method, may be a Geometry 
(Geo::OGR::Geometry) object or a hash from which one can be created 
(using the new "create" constructor). If the geometry type of the layer 
was well defined when it was created, GeometryType does not need to be 
given as it is queried internally. The one can use for example the 
simple "Points" structure.

I admit that these methods may be difficult to understand and hard to 
explain but they should be simple to use. However, the current error 
messages may not be very good if something goes wrong. If I rewrite your 
code as:

$datasource = Geo::OGR::Driver('ESRI Shapefile')->Create('test') or die;
$layer = $datasource->CreateLayer( 'ptsLyr', undef, 'Point' );
$layer->Schema( Fields => [ { Name => 'EAS_ID', Type => 'Integer', Width 
=> 10 }]);
$layer->InsertFeature( { EAS_ID => 0,
                                      Geometry => { Points => [3,2] } });

I get a shapefile, which consists of one feature, and whose schema is 
EAS_ID: Integer (The Width is needed to make sure the type is Integer) 
and point geometry. The one feature has a EAS_ID value 0 and a point 
geometry at (3,2).

Best regards,

Ari


-- 
Prof. Ari Jolma
Geoinformaatio- ja paikannustekniikka / Geoinformation and positioning
Teknillinen Korkeakoulu / Helsinki University of Technology
tel: +358 9 451 3886 address: POBox 1200, 02015 TKK, Finland
Email: ari.jolma at tkk.fi URL: http://www.tkk.fi/~jolma




More information about the gdal-dev mailing list