Small library for parsing, formatting, and representing coordinates. This library supports multiple coordinate formats,
is well-tested, and is used by the software behind Wikipedia and Wikidata.

On Packagist:

To parse a string to a LatLongValue object you use one of the coordinate parsers.
$parser = new LatLongParser();
$latLongValue = $parser->parse('55.7557860 N, 37.6176330 W');
var_dump($latLongValue->getLongitude()); // float: -37.6176330
These parsers are provided:
- LatLongParser - Facade for format-specific parsers. In most cases, you will be using this one
- DdCoordinateParser - Parses decimal degree coordinates
- DmCoordinateParser - Parses decimal minute coordinates
- DmsCoordinateParser - Parses degree minute second coordinates
- FloatCoordinateParser - Parses float coordinates
- GlobeCoordinateParser - Parses coordinates into GlobeCoordinateValue objects
To turn a coordinate object into a string you use one of the coordinate formatters.
$formatter = new LatLongFormatter();
$coordinateString = $formatter->format(new LatLongValue(42.23, 13.37));
These formatters are provided:
- LatLongFormatter - Formats a LatLongValue into any of the supported formats
- GlobeCoordinateFormatter - Formats a GlobeCoordinateValue
To represent a set of coordinates you use one of the Value Objects.
LatLongValue has a float latitude and longitude. GlobeCoordinateValue wraps LatLongValue
and adds a precision and a globe identifier.
The supported coordinate formats are:
- Degree minute second (55° 45' 20.8296", -37° 37' 3.4788" or 55° 45' 20.8296" N, 37° 37' 3.4788" W)
- Decimal minute (55° 30', -37° 30' or 55° 30' N, 37° 30' W)
- Decimal degree (55.7557860°, -37.6176330° or 55.7557860° N, 37.6176330° W)
- Float (55.7557860, -37.6176330 or 55.7557860 N, 37.6176330 W)
The parsers and formatters allow you to customize the used symbols for degrees, minutes, and seconds and
to change the letters used to indicate the direction (N, E, S, W).
Geo 4.x: PHP 7.1 or later (tested with PHP 7.1 up to PHP 8.4)
Geo 3.x: PHP 5.5 or later (tested with PHP 5.5 up to PHP 7.4 and HHVM)
To add this package as a local, per-project dependency to your project, simply add a
dependency on data-values/geo to your project's composer.json file.
Here is a minimal example of a composer.json file that just defines a dependency on
version 4.x of this package:
{
"require": {
"data-values/geo": "^4.0.0"
}
}
For tests only
For style checks only
For a full CI run
DataValues Geo is based upon and contains a lot of code written by
Jeroen De Dauw for the
Maps and
Semantic MediaWiki projects.
Significant contributions were made by the Wikidata team, as Wikimedia Germany
employees for the Wikidata project.
- Updated requirement for data-values/interfaces to ^1.1.0
- Dropped support for PHP 7.4 and 8.0
- Testing on PHP 8.4
- Restored support for PHP 7.4
- Dropped support for PHP 7.3 and 7.4
- Improved support for PHP 8.4 by explicitly declaring nullable types
- Upgraded mediawiki/mediawiki-codesniffer rules to version 45.0.0
- Added testing with PHP 8.2 and 8.3
- Implemented __serialize and __unserialize in LatLongValue and GlobeCoordinateValue,
improving compatibility with PHP 8.1
- Added LatLongValue::getSerializationForHash;
classes that use the PHP serialization of a LatLongValue for a hash should instead call this method
- Raised the minimum PHP version from 7.1 to 7.3
- GlobeCoordinateValue::newFromArray now supports numeric strings (thanks @Silvan-WMDE)
- Added compatibility with data-values/data-values 3.x
- Added compatibility with data-values/interfaces 1.x
- Fixed GlobeCoordinateParser not being able to parse multiple values (4.2.0 regression)
- Added GlobeCoordinateValue::withPrecision
- Added "PHP strict types" to all files
- LatLongValue no longer extends DataValueObject
- GlobeCoordinateValue no longer extends DataValueObject
- Reordered methods in LatLongValue and GlobeCoordinateValue for readability
- Undeprecated LatLongValue::newFromArray
- Undeprecated GlobeCoordinateValue::newFromArray
- Fixed parsing of coordinates with lowercase S/W directions
- Fixed parsing DMS coordinates that omit a single minute number
- Fixed parsing of coordinates with lowercase S/W directions
- Fixed parsing of coordinates with lowercase S/W directions
- Updated the minimum required PHP version from 5.5.9 to 7.1
- Added scalar type hints
- Added return type hints
- Added nullable type hints
- Made constant visibility explicit
- Constructing an invalid LatLongValue now causes InvalidArgumentException instead of OutOfRangeException
- Removed DATAVALUES_GEO_VERSION constant
- The parsers no longer extend StringValueParser
- They no longer have public methods setOptions and getOptions
- They no longer have protected field options
- They no longer have protected methods requireOption, defaultOption and stringParse
- GlobeCoordinateParser and LatLongParser no longer have protected method getOption
- Made several protected fields and methods private
- All fields of LatLongValue
- The detect…Precision methods in GlobeCoordinateParser
- LatLongParser::getParsers
- Removed public static method LatLongParser::areCoordinates
- Dropped dependence on the DataValues Common library
- Removed long deprecated class aliases
- DataValues\GlobeCoordinateValue (now in DataValues\Geo\Values)
- DataValues\LatLongValue (now in DataValues\Geo\Values)
- DataValues\Geo\Formatters\GeoCoordinateFormatter (now LatLongFormatter)
- DataValues\Geo\Parsers\GeoCoordinateParser (now LatLongParser)
- Allow use with ~0.4.0 of DataValues/Common
- Remove MediaWiki integration
- Make use of the …::class feature
- Add .gitattributes to exclude unnecessary files from git exports
- Use Wikibase CodeSniffer instead of MediaWiki's
- Move to short array syntax
- Fixed GlobeCoordinateValue::newFromArray and LatLongValue::newFromArray not accepting mixed
values.
- Deprecated GlobeCoordinateValue::newFromArray and LatLongValue::newFromArray.
- Updated the minimum required PHP version from 5.3 to 5.5.9.
- GlobeCoordinateValue no longer accepts empty strings as globes.
- GlobeCoordinateValue no longer accept precisions outside the [-360..+360] interval.
- Changed hash calculation of GlobeCoordinateValue in an incompatible way.
- Renamed GeoCoordinateFormatter to LatLongFormatter, leaving a deprecated alias.
- Renamed GeoCoordinateParser to LatLongParser, leaving a deprecated alias.
- Renamed GeoCoordinateParserBase to LatLongParserBase.
- Deprecated LatLongParser::areCoordinates.
- Fixed multiple rounding issues in GeoCoordinateFormatter.
- Fixed another IEEE issue in GeoCoordinateFormatter.
- Added missing inline documentation to public methods and constants.
- Added a basic PHPCS rule set, can be run with composer phpcs.
- Fixed an IEEE issue in GeoCoordinateFormatter
- Fixed a PHP 7.1 compatibility issue in a test
- Made minor documentation improvements
- Added compatibility with DataValues Common 0.3.x
- The component can now be installed together with DataValues Interfaces 0.2.x
- Add a fallback to default on invalid precision to more places.
- Fall back to default on invalid precision instead of dividing by zero.
- Precision detection in GlobeCoordinateParser now has a lower bound of 0.00000001°
- Removed remaining uses of class aliases from messages and comments
- Fixed some types in documentation
- Made the component installable with DataValues 1.x
- GeoCoordinateFormatter now supports precision in degrees
- GlobeCoordinateFormatter now passes the globe precision to the GeoCoordinateFormatter it uses
- Introduced FORMAT_NAME class constants on ValueParsers to use them as expectedFormat
- Changed ValueParsers to pass rawValue and expectedFormat arguments when constructing a ParseException
- All classes and interfaces have been moved into the DataValues\Geo namespace
- DataValues\LatLongValue has been left as deprecated alias
- DataValues\GlobeCoordinateValue has been left as deprecated alias
- Globe in GlobeCoordinateValue now defaults to http://www.wikidata.org/entity/Q2
- Removed deprecated GeoCoordinateValue
- Added GlobeMath
- Added support for different levels of spacing in GeoCoordinateFormatter
- Added support for direction notation to GeoCoordinateFormatter
- Decreased complexity of GeoCoordinateFormatter
- Decreased complexity and coupling of GeoCoordinateFormatterTest
Initial release with these features:
- LatLongValue
- GlobeCoordinateValue
- GeoCoordinateFormatter
- GlobeCoordinateFormatter
- DdCoordinateParser
- DmCoordinateParser
- DmsCoordinateParser
- FloatCoordinateParser
- GeoCoordinateParser
- GlobeCoordinateParser