This project is an extension for PHP 7 that enables geospatial rendering with Mapnik. Create tile generation scripts, dynamic tile services, or static maps with Mapnik XML and PHP.
- PHP >= 7.0
- php7.0-gd
- php7.0-dev
- Available through
sudo apt-get install php-dev php-gd
on Ubuntu 16.04 Xenial
- Mapnik 3.0.x
- mapnik-config
- Available through
sudo apt-get install libmapnik-dev
on Ubuntu 16.04 Xenial
git clone https://github.com/garrettrayj/php7-mapnik.git && cd php7-mapnik
phpize
./configure --with-mapnik
make test
make install
echo "extension=mapnik.so" > /etc/php.d/mapnik.ini
Note: Your PHP configuration directory may be different depending on OS and PHP settings.
Run the example...
./example/run.sh
...then visit http://localhost:8000/
<?php
// Register datasource plugins
// Use `mapnik-config --input-plugins` to get input plugin directory
\Mapnik\DatasourceCache::registerDatasources('/usr/local/lib/mapnik/input');
// Create map
$map = new \Mapnik\Map(640, 480);
// Register fonts
// Use `mapnik-config --fonts` to get Mapnik fonts directory
$map->registerFonts('/usr/local/lib/mapnik/fonts');
// Load Mapnik XML
$map->loadXmlFile('my_awesome_map.xml', false, $basePath);
// Situate map content within canvas
$map->zoomAll();
// Create image
$image = new \Mapnik\Image(640, 480);
// Render
$renderer = new \Mapnik\AggRenderer($map, $image);
$renderer->apply();
// Save PNG image file
$image->saveToFile('my_awesome_map.png');
See the API Documentation for a complete list of available objects and methods.