Skip to content
/ gdalada Public

Low-level Ada bindings to the GDAL library

Notifications You must be signed in to change notification settings

nbrk/gdalada

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ada bindings to GDAL

Gdalada is a thin, low-level, auto-generated Ada bindings for the following subset of GDAL (Geospatial Data Abstraction Library):

  • cpl_*.h: Common Portability Library C API
  • gdal.h: Raster C API
  • ogr_core.h, ogr_api.h: Vector C API
  • gdal_alg.h: GDAL Algorithms C API
  • ogr_srs_api.h: Spatial Reference System C API
  • gdal_utils.h: GDAL Algorithms C API

Ada specifications were generated by gcc -fdump-ada-spec from the C headers of GDAL version 3.3.2.

Gdalada + gtkada/cairo

Usage

The specification source code was amalgamated, slightly edited by hand and organized in the following Ada package hierarchy for convenience:

Gdalada
└───CPL
└───GDAL
└───GDAL_Alg
└───GDAL_Utils
└───OGR
└───OGR_SRS
└───Gdalwarper

You probably want to add something like that to your application's .gpr file to link with the system gdal library:

package Linker is
  for Switches("Ada") use ("-lgdal");
end Linker;

Example

The following code is a rough translation of the GDAL ``Vector API tutorial'' example.

with Gdalada.GDAL; use Gdalada.GDAL;
with Gdalada.OGR;  use Gdalada.OGR;

with Interfaces.C;         use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with System;               use System;

with Ada.Text_IO;      use Ada.Text_IO;
with Ada.Assertions;   use Ada.Assertions;
with Ada.Command_Line; use Ada.Command_Line;

procedure Gdal_Test is
  Dataset     : GDALDatasetH;
  Layer       : OGRLayerH;
  Featuredefn : OGRFeatureDefnH;
  Feature     : OGRFeatureH;
begin
  if Argument_Count /= 1 then
    Put_Line ("Usage : " & Command_Name & " <file>");
    return;
  end if;

  Put_Line ("Using file path: " & Argument (1));

  GDALAllRegister;

  Dataset :=
   GDALOpenEx
    (New_String (Argument (1)), GDAL_OF_VECTOR, Null_Address, Null_Address,
     Null_Address);
  Assert (Dataset /= Null_Address);

  Layer := GDALDatasetGetLayer (Dataset, 0);
  Assert (Layer /= Null_Address);

  Put_Line ("Layer: " & Value (OGR_L_GetName (Layer)));

  Featuredefn := OGR_L_GetLayerDefn (Layer);

  OGR_L_ResetReading (Layer);

  Features_Loop :
  loop
    Feature := OGR_L_GetNextFeature (Layer);
    exit when Feature = Null_Address;

    Put_Line ("Feature FID " & OGR_F_GetFID (Feature)'Image);

    -- Look through all fields of the feature
    Fields_Loop :
    for I in 0 .. (OGR_FD_GetFieldCount (Featuredefn) - 1) loop
      declare
        Fielddefn : OGRFieldDefnH := OGR_FD_GetFieldDefn (Featuredefn, I);
      begin
        case OGR_Fld_GetType (Fielddefn) is
          when OFTInteger =>
            Put_Line
             (" Integer field: " & OGR_F_GetFieldAsInteger (Feature, I)'Image);
          when OFTInteger64 =>
            Put_Line
             (" Integer64 field: " &
              OGR_F_GetFieldAsInteger64 (Feature, I)'Image);
          when OFTReal =>
            Put_Line
             (" Real field: " & OGR_F_GetFieldAsDouble (Feature, I)'Image);
          when OFTString =>
            Put_Line
             (" String field: " & Value (OGR_F_GetFieldAsString (Feature, I)));
          when others =>
            Put_Line
             (" Other field: " & Value (OGR_F_GetFieldAsString (Feature, I)));
        end case;
      end;
    end loop Fields_Loop;

    -- Feature's geometry
    declare
      Geometry : OGRGeometryH := OGR_F_GetGeometryRef (Feature);
    begin
      if Geometry /= Null_Address and
       OGR_GT_Flatten (OGR_G_GetGeometryType (Geometry)) = WkbPoint
      then
        Put_Line
         (" " & OGR_G_GetX (Geometry, 0)'Image & ", " &
          OGR_G_GetY (Geometry, 0)'Image);
      else
        Put_Line (" No point geometry");
      end if;
    end;

    OGR_F_Destroy (Feature);
  end loop Features_Loop;

  GDALClose (Dataset);
end Gdal_Test;

Testing

Unit tests are needed.

Known bugs, limitations

The library is Work-In-Progress. Some functions from GDAL's CPL/VSI dealing with OS-specific types like time_t and stat that have no direct equivalent in standard Ada are commented out for now.

About

Low-level Ada bindings to the GDAL library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages