[Gdal-dev] ogr / geos scripting question

Ari Jolma ari.jolma at tkk.fi
Wed Sep 12 15:01:01 EDT 2007


I've done this a lot lately. I work with the Perl bindings, but 
hopefully you get the idea. Here's a typical structure of a program:

use Geo::GDAL; # bring in the capability
$datasource = Geo::OGR::Open('.', 1) or die; # I've got shapefiles in 
the current directory
$o = shift @ARGV; # read the name of the layer from command line
$out = $datasource->CreateLayer($o, undef, $Geo::OGR::wkbMultiPoint); # 
a multipoint layer
$schema = $out->GetLayerDefn(); # an empty schema since I'm interested 
in geometries now

# this is within a loop
# often the loop is from reading from another layer, for example:
# $input->ResetReading();
# while ($feature = $feature->GetNextFeature()) {

    $g = Geo::OGR::Geometry->new($Geo::OGR::wkbMultiPoint); # new geometry

    # a sub loop
      $p = Geo::OGR::Geometry->new($Geo::OGR::wkbPoint); # new point
      $p->SetPoint(0, $x, $y); # put the coords to the point
      $g->AddGeometry($p); # put the point to the multipoint

    $f = new Geo::OGR::Feature($schema); # new feature
    $f->SetGeometry($g); # put the geometry to the feature
    $out->CreateFeature($f); # put the feature to the layer

# out of the loop and finish:
$out->SyncToDisk;

Brian Hamlin kirjoitti:
> I'd like to write a script to add a few points to a datasource, then 
> make some simple geos calculations on it.
>  (I'm completely new to this)
>
> below is what I came up with at a first look ( to simply create the 
> Data).
>
> After creating the Data, I'd like to make a geos calculation, like 
> minimum radius for example.
>
> thanks in advance
>   -Brian / darkblueB
>
>
> ##
> get OGR driver count
>    iterate through drivers, looking for the desired named driver
>
> datasource = driver->CreateDataSource( dir )
> layer = datasource->CreateLayer( "layerOne" )
>
> g = OGR CreateGeometryFromWKT( "Point( 10 100)" )
>
> ##???
>
> layer->SyncToDisk
> ##------------------------------------
> I am a little lost about the need for features, or how to append my new
> OGRGeometryShadow to the layer.
>
> _______________________________________________
> Gdal-dev mailing list
> Gdal-dev at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/gdal-dev


-- 
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