PHP extension for geospatial rendering with Mapnik

Last update: Dec 17, 2021

PHP7 Mapnik

PHP7 Mapnik

Introduction

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.

Build Status

Requirements

  • PHP >= 7.0
    • Available through brew intall php on MacOS with Homebrew.
    • Available through sudo apt-get install php-dev php-gd on Ubuntu 16.04 Xenial.
  • Mapnik 3.0.x
    • Available through brew install mapnik on MacOS with Homebrew.
    • Available through sudo apt-get install libmapnik-dev on Ubuntu 16.04 Xenial.

Installation

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

Notes:

  • The PHP configuration directory may differ from above. Run php -i | grep "Scan this dir" to find your system's location.
  • The ICU library path may need to be exported before building on MacOS. export LDFLAGS="-L/usr/local/opt/icu4c/lib"

Tile Server Example

Run the example...

./example/run.sh

...then visit http://localhost:8000/

Usage

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

Dev Container

The included Dockerfile builds an image with all the dependencies needed for extension development and testing, including testing for memory leaks.

docker build -t php7-mapnik-dev .
docker run -it --mount type=bind,source=$(pwd),target=/opt/php7-mapnik php7-mapnik-dev:latest

GitHub

https://github.com/garrettrayj/php7-mapnik
Comments
  • 1. render bounding box question

    is there any way we can get the bbox and scale of what is to be rendered by the map? the zoomToBox zooms the image to fit the bounds of map, but if say you produce a 800X600 image based on a zoomToBox that's 16:9 then the rendered bounding box will be significantly different to the original 16:9. I'm trying to manipulate the data layer to exclude any label on the edge of the map but without knowing what the true bounding box is and without knowing the scale will be makes this difficult. any ideas here?

    Reviewed by africanrhino at 2017-03-06 07:02
  • 2. make test fails on imagecreatefromstring()

    Not sure what is going on here but cannot complete the installation steps because make test fails on imagecreatefromstring() image.phpt.

    Naturally, the first thing I checked is the presence of gd and it was present. Next I copied the imagecreatefromstring() example from the PHP docs into a regular .php file and, oddly enough, it works as expected.

    php_test_results_20160416_1939.txt

    Most strange :S

    -Peter

    Reviewed by 3XE at 2016-04-16 20:38
  • 3. Code Structure

    Hey @garrettrayj I saw your post on the mapnik dev mailing list, I don't have to give a big review but thought I would open a few tickets on your repo here on issues that I might see.

    The first issue I see is that you have all your source in the main directory, its not required but its a lot easier if you put it all in a directory. Is this something required by phpize?

    Confused by this as it shouldn't be any issue: Do NOT compile Boost with C++11. On Mac at least, you'll have a bad time with missing symbols.

    Reviewed by flippmoke at 2016-03-02 15:20
  • 4. Proj support

    Hi When I call ProjTransform->backward() or ProjTransform->forward() with any projection different from 4326 and 3857 i get segmentation fault. Mapnik compiled from source with proj4 support, tested on debian and alpine. example:

    $boundingBox = new Box2D($minX, $minY, $maxX, $maxY);
    $source = new Projection('+init=epsg:4326');
    $destination = new Projection('+init=epsg:32638');
    $transform = new ProjTransform($source, $destination);
    $tileBoundingBox = $transform->backward($boundingBox);
    
    Reviewed by sokrat at 2017-06-09 15:32
  • 5. Update requriments in README.md

    The './configure --with-mapnik' part of the installation process will fail unless mapnik-config is available. It seems to be packaged separately from mapnik3.0 on Ubuntu.

    -Peter

    Reviewed by 3XE at 2016-04-16 18:53
  • 6. Render outside map

    I would highly suggest for the ease of users who might use your library that you make the agg render method a part of the map object. We do that in node-mapnik, so that you can do map.render(...). I wouldn't suggest that the C++ interface be directly ported basically, I would write it so that the code is more in a "PHP" style.

    Reviewed by flippmoke at 2016-03-02 15:22
  • 7. Fix image test broken by PHP upgrade.

    After PHP 7.1.17 update from Webtatic repository (https://webtatic.com/news/2018/05/latest-updates-php-7.2.5-7.1.17-7.0.30-5.6.36/) issues with loading GD and the image.phpt test appeared. This change is a quick fix that simply skips that test if GD is not loaded. It may never be loaded as the --EXTENTIONS-- section of the test can't seem to find the module.

    Reviewed by garrettrayj at 2018-05-15 16:12
  • 8. PECL package

    As an end-user I want php7-mapnik available in PECL, so I may easily install it with a one liner.

    Feasibility

    Mapnik requires a tall stack of dependencies that can be hit or miss in repos. While availability has continuously improved, it's still very common to compile from source code. Does it make sense to package the extension when its main dependency is often compiled itself?

    The project may need to be renamed and relicensed (PHP License 3.0.1) to comply with PECL standards...

    Products derived from this software may not be called "PHP", nor may "PHP" appear in their name, without prior written permission from [email protected] You may indicate that your software works in conjunction with PHP by saying "Foo for PHP" instead of calling it "PHP Foo" or "phpfoo"

    ...and...

    We strongly encourage contributors to choose the PHP License 3.01 for their extensions, in order to avoid possible troubles for end-users of the extension.

    Todo

    • [ ] Reach out to pecl-dev mailing list. Inquire about license and project name.
    • [ ] Research packaging a C++ extension... Pyrus, pickle, package.xml, etc.
    Reviewed by garrettrayj at 2019-05-04 15:13
  • 9. Additional Box2D functions

    Currently PHP7 Mapnik only implements the bare minimum requirements for constructing Box2D objects. There's plenty of low hanging fruit in the C++ API that would make working with envelopes easier...

    • width() and height()
    • expand_to_include()
    • contains()
    • intersects()
    • re_center()
    • clip()
    • pad()
    • etc.

    Should probably do a PHP implementation of the coord class before tackling this.

    Reviewed by garrettrayj at 2017-04-08 10:21
GeoSpatial integration on Laravel 5.2+ that supports MySQL and PostgreSQL.

Features GeoSpatial integration on Laravel 5.2+: Create geospatial columns using Schema and migrations Save and retrieve geospatial attributes using d

May 22, 2022
Free database of geographic place names and corresponding geospatial data

FreeGeoDB Free database of geographic place names and corresponding geospatial data Entities airports cities countries (admin-0) lakes ports railroads

Jul 27, 2022
GeoJSON implementation for PHP

GeoJson PHP Library This library implements the GeoJSON format specification. The GeoJson namespace includes classes for each data structure defined i

Aug 4, 2022
ESRI ShapeFile library for PHP

shapefile ShapeFile library for PHP Features Currently the 2D and 3D variants except MultiPatch of the ShapeFile format as defined in https://www.esri

Jun 29, 2022
The most featured Geocoder library written in PHP.

Geocoder Important: You are browsing the documentation of Geocoder 4.x. Documentation for version 3.x is available here: Geocoder 3.x documentation. D

Aug 9, 2022
Simple Yet Powerful Geo Library for PHP

phpgeo - A Simple Geo Library for PHP phpgeo provides abstractions to geographical coordinates (including support for different ellipsoids) and allows

Jul 29, 2022
Geo-related tools PHP 5.4+ library built atop Geocoder and React libraries

Geotools Geotools is a PHP geo-related library, built atop Geocoder and React libraries. Features Batch geocode & reverse geocoding request(s) in seri

Aug 7, 2022
PHP library to access the OpenCage geocoding API
PHP library to access the OpenCage geocoding API

OpenCage Geocoding API Library for PHP A PHP library to use the OpenCage geocoding API. Build Status / Code Quality Overview This library attempts to

Jun 20, 2022
PHP library for determining the physical location of binaries

Bin Locator Library for searching binary files in the operating system. Requirements PHP >= 7.4 Installation Library is available as composer reposito

Jul 27, 2022
PHP library to easily get static image from French Cadastral Government map with markers and lines.
PHP library to easily get static image from French Cadastral Government map with markers and lines.

PHP French Cadastral Map Static API PHP library to easily get static image from French Cadastral Government map with markers and lines. Map source : c

May 24, 2022
PHP library to easily get static image from OpenStreetMap (OSM) with markers and lines.
PHP library to easily get static image from OpenStreetMap (OSM) with markers and lines.

PHP OpenStreetMap Static API PHP library to easily get static image from OpenStreetMap with markers and lines. ✨ Supporting ⭐ Star this repository to

Jul 28, 2022
Official PHP library for IPinfo (IP geolocation and other types of IP data)

This is the official PHP client library for the IPinfo.io IP address API, allowing you to lookup your own IP address,

Aug 14, 2022
PHP Extension to handle common geospatial functions.

geospatial - PHP Geospatial Extension PHP Extension to handle common geospatial functions. The extension currently has implementations of the Haversin

Aug 11, 2022
GeoSpatial integration on Laravel 5.2+ that supports MySQL and PostgreSQL.

Features GeoSpatial integration on Laravel 5.2+: Create geospatial columns using Schema and migrations Save and retrieve geospatial attributes using d

May 22, 2022
Free database of geographic place names and corresponding geospatial data

FreeGeoDB Free database of geographic place names and corresponding geospatial data Entities airports cities countries (admin-0) lakes ports railroads

Jul 27, 2022
Laravel package to work with geospatial data types and functions.

Laravel Spatial Laravel package to work with geospatial data types and functions. For now it supports only MySql Spatial Data Types and Functions. Sup

Jul 17, 2022
Library that provides collection, processing, and rendering functionality for PHP code coverage information.

phpunit/php-code-coverage Provides collection, processing, and rendering functionality for PHP code coverage information. Installation You can add thi

Aug 14, 2022
A PHP class for querying the Twitter API and rendering tweets as an HTML list

TweetPHP A PHP class for querying the Twitter API and rendering tweets as an HTML list. Features Works with Twitter API v1.1 Tweets are cached to avoi

Nov 18, 2021
Rendering markdown from PHP code

JBZoo / Markdown Installing composer require jbzoo/markdown Usage Rendering Table <?php declare(strict_types=1); use JBZoo\Markdown\Table; echo (new

Dec 26, 2021
PHP SIP Parsing/Rendering Library
 PHP SIP Parsing/Rendering Library

PHP SIP Parsing/Rendering Library RFC 3261 compliant SIP parsing and rendering library for PHP 7.4. Quickstart SIP Message Parsing Once installed, you

Jul 28, 2022