OGR/PHP Extension



The OGR library is a C++ open source library (and command-line tools) providing read (and sometimes write) access to a variety of vector file formats  including ESRI Shapefiles, S-57, SDTS, PostGIS, Oracle Spatial, and Mapinfo mid/mif and TAB formats.

The C API is generally easy to call from other languages which allow call out to DLLs functions, such as Visual Basic, or Delphi.

The OGR/PHP extension enables PHP users to access the functionalities included in the OGR library.  This extension can be used as built-in PHP or as dynamic library (".dll" on Windows or ".so" on Unix).

This extension was developed and is currently being maintained by DM Solutions Group. Special thanks go to Lars V. Nielsen and Hvenegaard & Meklenborg for funding the initial development of this extension and allowing it to remain open source.


To use the OGR/PHP extension, the following is needed:


Check if you have PHP already installed (several Linux distributions have it built in).
If not, download the 4.2.3 or higher PHP version source code or binaries.

Source code and win32 binaries are available on the PHP download page.

The PHP source code is also available via anonymous CVS, see the PHP cvs page.


Visit the GDAL website to download the latest version of the library.


Source code or Win32 dll for the OGR/PHP extension can be obtained from DM Solutions. The official download location is still to be determined.



GDAL library must be built before compiling PHP and OGR/PHP extension.

Currently a build mechanism only exists for Unix, and the C wrapper functions are built and installed as a standard part of GDAL/OGR.  It does live in the GDAL/OGR source tree.

The source (such as a nightly CVS snapshot) can be downloaded from the GDAL/OGR web site.
Once unpacked, build GDAL/OGR according to the provided instructions, and then install.

% cd gdal
% ./configure
% make
% sudo make install


Building on Unix


There are two ways to build the OGR/PHP extension:  as a separate module (as a ".so" library) or as a built-in module.  It is recommended to use this extension as a separate module for simplicity reason and because there is no need to compile PHP.

Build as a separate module

Build as a built-in module

    More information can be found into the PHP manual.

Installing on Windows

Testing all

To verify that PHP and OGR/PHP were installed properly, create a '.php' file containing the following code and try to access it through your web server:


       if (PHP_OS == "WINNT" || PHP_OS == "WIN32")


If PHP and PHP/OGR were installed properly, several tables should be displayed on your page, and 'OGR' should be listed in the 'Extensions' table.

Functions documentation

The C API documentation  was created using Doxygen and is part of the GDAL/OGR documentation.  In addition to OGR functions, four CPL functions for handling error are made available to the user:  CPLErrorReset, CPLGetLastErrorNo, CPLGetLastErrorMsg and CPLGetLastErrorType.  Information on these functions can be found on this page.

Information and bug reporting

The OGR mailing list can be used for discussion of developement and user issues related to OGR and related technology.  Mailing list can be used to report bugs as well.  Subscriptions can be done, and archives reviewed on the web.


There are a few examples showing how to use the functions in the PHP/OGR extension module.  Consult the README_EXAMPLES.TXT in the examples sub-directory of the extension package to get more information on what is the purpose of each example.

Each example must be invoked from the command-line.

Example:  ./php -q ogr2tab.php /path/to/outputfilename.tab /path/to/sourcefilename.tab

"ogr2ogr.php" shows mostly the following:

"ogr2tab.php" is accomplishing the same operations as "ogr2ogr.php" but is concerned especially with the MapInfo File format.

"ogr_spatialfilter.php" shows mostly how to set a spatial filter and an attribute filter.

"ogr2tab_spatialfilter.php" is accomplishing the same operations as "ogr_spatialfilter.php" but is used exclusively with MapInfo File format.

"ogr_sql.php" demonstrates how to build an SQL statement and apply an SQL request to a data source.

"ogr2tab_sql.php" is a copy of "ogr_sql.php" and is concerned with the MapInfo File format.

"ogr_update.php" demonstrates the following items:

"ogr2tab_update.php" is a copy of "ogr_update.php" but is concerned with the MapInfo File format.

"ogr_dump.php" shows the following functionalities:

"ogr_write.php" demonstrates the following functionalities: "ogr_setgetfield.php" shows how to get and to set field values of various types inside a feature.