OGR/PHP
Extension
Introduction
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.
Download
To use the OGR/PHP extension,
the following is needed:
- PHP 4.2.3 or higher
Note that for PHP 5.x, OGR/PHP 1.1.1 is required.
- GDAL library 1.1.8
Note from OGR/PHP version 1.1.0, GDAL 1.3.0 is required.
- OGR/PHP extension module
PHP
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.
GDAL/OGR
Visit the GDAL website to download the latest
version of the library.
OGR/PHP
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.
Build/Installation/Test
GDAL
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
PHP - OGR/PHP
Building on Unix
Recommendation
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
- tar xzvf php_ogr_1.0.tar.gz
into the "ext" directory.
- Run /path/to/phpize in the
OGR/PHP extension source directory.
- Run ./configure --with-php-config=/path/to/php-config.
- Run make in the OGR/PHP extension
directory.
- Change directory to "modules"
under OGR/PHP extension.
- Copy the ogr.so library
to your PHP extensions directory.
- Rename the extension "php_ogr.so".
- Use dl() function to load
the module at the beginning of your PHP scripts.
Build as a built-in module
- Installed the PHP source
code if not already installed.
- tar xzvf php_ogr_1.0.tar.gz
into the "ext" directory.
- Run ./buildconf --force
in the PHP main directory.
- Run ./configure --with-ogr[=dir]
in the PHP main directory where dir is corresponding to the GDAL/OGR directory.
- Run make in the PHP main
directory.
- PHP is now compiled with
OGR/PHP built-in.
More
information can be found into the PHP manual.
Installing on Windows
- Unzip php-4.x.x-Win32.zip
into your prefered folder and follow the install instruction.
- Unzip php_ogr_1.0.zip.
- Copy the 'dll' into your
extensions library folder.
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:
<HTML>
<BODY>
<?php
if (PHP_OS == "WINNT" || PHP_OS == "WIN32")
{
dl("php_ogr.dll");
}
else
{
dl("php_ogr.so");
}
phpinfo();
?>
</BODY>
</HTML>
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.
Examples
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:
- Opening an existing data
source with a specific driver
- Creating a new data source
- Accessing a layer
- Creating a new layer
- Adding fields on a layer
- Getting features from a
layer
- Creating new features on
a layer
- Testing capabilities of
a data source or a layer
"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:
- Accessing a specific feature
by specifying a feature id
- Getting a field definition
- Getting a field name
- Setting a field value
- Getting a geometry from
a feature
- Setting a geometry to a
feature
"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:
- Getting geometry components
from which geometry is built from, like point coordinates for a linestring,
linestring for a polygon and so on
- Getting geometry name
- Getting the number of geometry
components
"ogr_write.php" demonstrates the
following functionalities:
- Accessing a specific layer
- Creating a new field definition
and assigning it to a layer
- Creating a new feature on
a layer
- Setting a field value
- Creating a new geometry
(point, linestring, polygon) and adding it to a feature
"ogr_setgetfield.php" shows how
to get and to set field values of various types inside a feature.