PHPackages                             ballen/cartographer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ballen/cartographer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ballen/cartographer
===================

Cartographer is a PHP library for constructing GeoJSON objects.

2.0.4(3y ago)6539[1 PRs](https://github.com/allebb/cartographer/pulls)GPL-3.0-or-laterPHPPHP ^7.3.0|^8.0.0CI failing

Since Aug 22Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/allebb/cartographer)[ Packagist](https://packagist.org/packages/ballen/cartographer)[ RSS](/packages/ballen-cartographer/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (3)Versions (12)Used By (0)

Cartographer
============

[](#cartographer)

[![Build](https://github.com/allebb/cartographer/workflows/build/badge.svg)](https://github.com/allebb/cartographer/actions)[![Code Coverage](https://camo.githubusercontent.com/74c144b6995f3848877501d79876dbfc5c80674ff1fe0eeaa10885e595e659bd/68747470733a2f2f636f6465636f762e696f2f67682f616c6c6562622f636172746f677261706865722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/allebb/cartographer)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ecc274372cbff9a23bd427dc050ec7675ba0c564171e48080c240a618a6d0384/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c6c6562622f636172746f677261706865722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/allebb/cartographer/?branch=master)[![Code Climate](https://camo.githubusercontent.com/9b97f2595fc8d32b822123683babc258704da26bf964d2701a19cbb629fdf2ab/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f616c6c6562622f636172746f677261706865722f6261646765732f6770612e737667)](https://codeclimate.com/github/allebb/cartographer)[![Latest Stable Version](https://camo.githubusercontent.com/2f25f6b92d1a4ac4f81eb2a3f546e2777ee0c20fb31a560bc0e3e27b4f910ed7/68747470733a2f2f706f7365722e707567782e6f72672f62616c6c656e2f636172746f677261706865722f762f737461626c65)](https://packagist.org/packages/ballen/cartographer)[![Latest Unstable Version](https://camo.githubusercontent.com/a3fb2dacd520734f4175d987ccd131f2effd349aa3fe690ab9a746f226c1a53a/68747470733a2f2f706f7365722e707567782e6f72672f62616c6c656e2f636172746f677261706865722f762f756e737461626c65)](https://packagist.org/packages/ballen/cartographer)[![License](https://camo.githubusercontent.com/e09b78fe20f4fc17c20dfb83ef4f287d0365cea9484cff0322790bb99538d4e9/68747470733a2f2f706f7365722e707567782e6f72672f62616c6c656e2f636172746f677261706865722f6c6963656e7365)](https://packagist.org/packages/ballen/cartographer)

Cartographer is a PHP library providing the ability to programmatically generate GeoJSON objects.

GeoJSON is a format for encoding a variety of geographic data structures. A GeoJSON object may represent a geometry, a feature, or a collection of features. GeoJSON supports the following geometry types: `Point`, `LineString`, `Polygon`, `MultiPoint`, `MultiLineString`, `MultiPolygon`, and `GeometryCollection`. Features in GeoJSON contain a geometry object and additional properties, and a feature collection represents a list of features.

Cartographer was written to adhere to the GeoJSON specification, information can be found here:

Requirements
------------

[](#requirements)

- PHP &gt;= 7.3.0

This library is unit tested against PHP 7.3, 7.4, 8.0, 8.1 and 8.2!

If you need to use an older version of PHP, you should instead install the 1.x version of this library (see below for details).

License
-------

[](#license)

This client library is released under the [GPLv3](https://raw.githubusercontent.com/allebb/cartographer/master/LICENSE) license, you are welcome to use it, improve it and contribute your changes back!

Installation
------------

[](#installation)

The recommended way of installing this library is via. [Composer](http://getcomposer.org); To install using Composer type the following command at the console:

```
composer require ballen/cartographer
```

**If you need to use an older version of PHP, version 1.x.x supports PHP 5.3, 5.4, 5.5, 5.6, 7.0, 7.1 and 7.2, you can install this version using Composer with this command instead:**

```
composer require ballen/cartographer ^1.0
```

Example usage
-------------

[](#example-usage)

### Point

[](#point)

The "Point" type is the most basic to construct, this example shows an example of plotting a single point on a map.

```
use Ballen\Cartographer\Core\LatLong;
use Ballen\Cartographer\Point;

$point = new Point(new LatLong(52.005523, 1.045936));
echo $point->generate();
// {"type":"Point","coordinates":[1.045936,52.005523]}
```

Check out the GitHub Gist rendition of the GeoJSON output:

### Linestring

[](#linestring)

The "LineString" type contains a list of geographic points (Lat/Longs) of which are joined together to display a line.

```
use Ballen\Cartographer\Core\LatLong;
use Ballen\Cartographer\LineString;

$points = [
    new LatLong(51.973683,1.044497),
    new LatLong(51.974067,1.044134),
    new LatLong(51.974355,1.045795),
    new LatLong(51.975010,1.049768),
    new LatLong(51.976018,1.055869),
    new LatLong(51.976195,1.056060),
    new LatLong(51.976432,1.056083),
    new LatLong(51.976774,1.056036),
    new LatLong(51.977023,1.056115),
    new LatLong(51.977107,1.056379),
    new LatLong(51.977102,1.056658),
 ];
$linestring = new LineString($points);
echo $linestring->generate();
// {"type":"LineString","coordinates":[[1.044497,51.973683],[1.044134,51.974067],[1.045795,51.974355],[1.049768,51.97501],[1.055869,51.976018],[1.05606,51.976195],[1.056083,51.976432],[1.056036,51.976774],[1.056115,51.977023],[1.056379,51.977107],[1.056658,51.977102]]}
```

Check out the GitHub Gist rendition of the GeoJSON output:

#### Polygon

[](#polygon)

A Polygon type object, contains a list of coordinates, the first and last coordinate must match. For Polygons with multiple rings, the first must be the exterior ring and any others must be interior rings or holes.

```
use Ballen\Cartographer\Core\LatLong;
use Ballen\Cartographer\Polygon;
use Ballen\Cartographer\Core\LinearRing;

$points = [
    (new LatLong(52.064761, 1.174470))->lngLatArray(),
    (new LatLong(52.065045, 1.176098))->lngLatArray(),
    (new LatLong(52.064964, 1.176156))->lngLatArray(),
    (new LatLong(52.065172, 1.177106))->lngLatArray(),
    (new LatLong(52.064146, 1.177594))->lngLatArray(),
    (new LatLong(52.063968, 1.176768))->lngLatArray(),
    (new LatLong(52.063714, 1.174875))->lngLatArray(),
    (new LatLong(52.064761, 1.174470))->lngLatArray(),
];
$linestring = new Polygon((new LinearRing())->addRing($points));
echo $linestring->generate();
// {"type":"Polygon","coordinates":[[[1.17447,52.064761],[1.176098,52.065045],[1.176156,52.064964],[1.177106,52.065172],[1.177594,52.064146],[1.176768,52.063968],[1.174875,52.063714],[1.17447,52.064761]]]}
```

Check out the GitHub Gist rendition of the GeoJSON object:

#### Feature

[](#feature)

A feature enables you to plot a single GeoJSON object on a map with associated properties, Google Maps enable you to render `Feature` and `FeatureCollection` types.

A example of a `Point` feature type is as follows:

```
use Ballen\Cartographer\Core\LatLong;
use Ballen\Cartographer\Feature;
use Ballen\Cartographer\Point;

$feature = new Feature(new Point(new LatLong(52.063186, 1.157385)), [
    // Your own personal marker points (appear when you click on the Feature point)
    'Park' => 'Christchurch Park',
    'Post code' => 'IP4 2BX',
    'Link' => 'http://focp.org.uk/',
    // Optional Mapbox supported properties (See: https://www.mapbox.com/help/markers/)
    'marker-color' => '#3bb2d0', // A light blue marker colour
    'marker-symbol' => 'park',
    'marker-size' => 'large',
]);
echo $feature->generate();
// {"type":"Feature","geometry":{"type":"Point","coordinates":[1.157385,52.063186]},"properties":{"Park":"Christchurch Park","Post code":"IP4 2BX","Link":"http:\/\/focp.org.uk\/","marker-color":"#3bb2d0","marker-symbol":"park","marker-size":"large"}}
```

Check out the GitHub Gist rendition of the GeoJSON output:

#### Feature Collection

[](#feature-collection)

A feature collection can contain any number of GeoJSON objects with their own properties, all of the features in a collection with their own properties.

```
use Ballen\Cartographer\Core\LatLong;
use Ballen\Cartographer\Feature;
use Ballen\Cartographer\Point;

$park = new Feature(new Point(new LatLong(52.063186, 1.157385)), [
    // Your own personal marker points (appear when you click on the Feature point)
    'Park' => 'Christchurch Park',
    'Post code' => 'IP4 2BX',
    'Link' => 'http://focp.org.uk/',
    // Optional Mapbox supported properties (See: https://www.mapbox.com/help/markers/)
    'marker-color' => '#3bb2d0', // A light blue marker colour
    'marker-symbol' => 'park',
    'marker-size' => 'large',
]);

// Train Station Specific codes
$station_properties = [
    'marker-color' => '#F6546A',
    'marker-symbol' => 'rail',
    'marker-size' => 'medium'
];

// Set some train stations with their own names (merge the standard train station details)
$station_central = new Feature(new Point(new LatLong(52.050743, 1.143012)), array_merge($station_properties, ['Name' => 'Ipswich Train Station']));
$station_derbyroad = new Feature(new Point(new LatLong(52.050808, 1.182638)), array_merge($station_properties, ['Name' => 'Derby Road Station']));
$station_westerfield = new Feature(new Point(new LatLong(52.081026, 1.166773)), array_merge($station_properties, ['Name' => 'Westerfield Train Station']));

// Create the new collection and add each of the GeoJSON objects to it...
$collection = new Ballen\Cartographer\FeatureCollection([$station_central, $park, $station_westerfield, $station_derbyroad]);
echo $collection->generate();
// {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1.143012,52.050743]},"properties":{"marker-color":"#F6546A","marker-symbol":"rail","marker-size":"medium","Name":"Ipswich Train Station"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.157385,52.063186]},"properties":{"Park":"Christchurch Park","Post code":"IP4 2BX","Link":"http:\/\/focp.org.uk\/","marker-color":"#3bb2d0","marker-symbol":"park","marker-size":"large"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.166773,52.081026]},"properties":{"marker-color":"#F6546A","marker-symbol":"rail","marker-size":"medium","Name":"Westerfield Train Station"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.182638,52.050808]},"properties":{"marker-color":"#F6546A","marker-symbol":"rail","marker-size":"medium","Name":"Derby Road Station"}}]}
```

Check out the GitHub Gist rendition of the GeoJSON output:

#### Other examples

[](#other-examples)

Other examples of the types of GeoJSON object type, see the [examples/test.php](https://github.com/allebb/cartographer/blob/master/examples/test.php) file.

Tests and coverage
------------------

[](#tests-and-coverage)

This library is fully unit tested using [PHPUnit](https://phpunit.de/).

I use [GitHub Actions](https://github.com/) for continuous integration, which triggers tests for PHP 7.3, 7.4, 8.0, 8.1 and 8.2 each time a commit is pushed.

If you wish to run the tests yourself you should run the following:

```
# Install the Cartographer Library (which will include PHPUnit as part of the require-dev dependencies)
composer install

# Now we run the unit tests (from the root of the project) like so:
./vendor/bin/phpunit
```

Code coverage can also be run, and a report generated (this does require XDebug to be installed)...

```
./vendor/bin/phpunit --coverage-html ./report
```

Support
-------

[](#support)

I am happy to provide support via. my personal email address, so if you need a hand drop me an email at: .

###  Health Score

43

—

FairBetter than 90% of packages

Maintenance50

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~257 days

Recently: every ~184 days

Total

10

Last Release

1280d ago

Major Versions

1.0.4 → 2.0.02020-12-22

PHP version history (4 changes)1.0.0PHP &gt;=5.5.0

2.0.0PHP &gt;=7.2.0

2.0.1PHP &gt;=7.3.0

2.0.3PHP ^7.3.0|^8.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4729a376aa8281d9456a90c000e991aa605c96f15251469acd5a7a5c6cdde2e6?d=identicon)[allebb](/maintainers/allebb)

---

Top Contributors

[![allebb](https://avatars.githubusercontent.com/u/767628?v=4)](https://github.com/allebb "allebb (54 commits)")

---

Tags

geojsongeometrygoogle-maps-apijsonlatlnglinestringmapsmarkersmultilinestringphppolygonpolygonsgeojsoncartographergeo json

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ballen-cartographer/health.svg)

```
[![Health](https://phpackages.com/badges/ballen-cartographer/health.svg)](https://phpackages.com/packages/ballen-cartographer)
```

###  Alternatives

[jmikola/geojson

GeoJSON implementation for PHP

3069.4M86](/packages/jmikola-geojson)[georgique/world-geojson

GeoJson for all the countries, areas (regions) and some states.

4955.3k](/packages/georgique-world-geojson)[funiq/geophp

Open-source native PHP library for doing geometry operations. Can read and write a wide variety of formats: (E)WKT, (E)WKB, TWKB, GeoJSON, KML, GPX, GeoRSS. Works with all Simple-Feature geometries (Point, LineString, Polygon...) and can be used to get centroids, bounding-boxes, area, etc.

21116.6k1](/packages/funiq-geophp)[hannesvdvreken/pip

The Polygon class

1838.7k1](/packages/hannesvdvreken-pip)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
