Skip to content

Commit

Permalink
Merge pull request #11 from creof/release-2.2
Browse files Browse the repository at this point in the history
Release 2.2
  • Loading branch information
djlambert committed May 3, 2016
2 parents 1609d96 + 0c421b7 commit e057132
Show file tree
Hide file tree
Showing 8 changed files with 544 additions and 337 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
### Changed

## [2.2.0] - 2016-05-03
### Added
- Added Tests namespace to Composer PSR-0 dev autoload.
- Added 'dimension' key to returned array containing object dimensions (Z, M, or ZM).
- Reader::getMachineByteOrder method to detect running platform endianness.
### Changed
- Parser property with Reader instance no longer static.
- Replaced sprintf function call in Reader::unpackInput() with string concatenation.
- Updated PHPUnit config to be compliant with XSD.
- Updated PHPUnit config to use Composer autoload.
- Updated documentation with new usage pattern.
- Type name in returned array now contains only base type without dimensions (Z, M, and ZM).
- Reader::readDouble() now checks running platform endianness before byte-swapping values instead of assuming little-endian.
### Removed
- Removed now unused TestInit

## [2.1.0] - 2016-02-18
### Added
- Reader load() method to allow reusing a Reader instance.
Expand Down
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@
[![Code Climate](https://codeclimate.com/github/creof/wkb-parser/badges/gpa.svg)](https://codeclimate.com/github/creof/wkb-parser)
[![Test Coverage](https://codeclimate.com/github/creof/wkb-parser/badges/coverage.svg)](https://codeclimate.com/github/creof/wkb-parser/coverage)

Parser library for WKB/EWKB spatial object data.
Parser library for 2D, 3D, and 4D WKB/EWKB spatial object data.

## Usage

Pass value to be parsed in the constructor, then call parse() on the created object.
There are two use patterns for the parser. The value to be parsed can be passed into the constructor, then parse()
called on the returned ```Parser``` object:

```php
$parser = new Parser($input);
$value = $parser->parse();

$value = $parser->parse();
```

If many values need to be parsed, a single ```Parser``` instance can be used:

```php
$parser = new Parser();

$value1 = $parser->parse($input1);
$value2 = $parser->parse($input2);
```

## Return

The parser will return an array with the keys ```srid```, ```type```, and ```value```.
- ```srid``` is the SRID if EWKT was passed in the constructor, null otherwise.
- ```type``` is the spatial object type.
- ```value``` will contain an array with 2 numeric values, or nested arrays containing these depending on the spatial object type.
The parser will return an array with the keys ```type```, ```value```, ```srid```, and ```dimension```.
- ```type``` string, the spatial object type (POINT, LINESTRING, etc.) without any dimension.
- ```value``` array, contains integer or float values for points, or nested arrays containing these based on spatial object type.
- ```srid``` integer, the SRID if EWKT value was parsed, ```null``` otherwise.
- ```dimension``` string, will contain ```Z```, ```M```, or ```ZM``` for the respective 3D and 4D objects, ```null``` otherwise.

## Exceptions

The ```Reader``` and ```Parser``` will throw exceptions implementing interface ```CrEOF\Geo\WKB\Exception\ExceptionInterface```.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
],
"license": "MIT",
"require": {
"php": ">=5.3.3",
"ext-SPL": "*"
"php": ">=5.3.3",
"ext-SPL": "*"
},
"require-dev": {
"phpunit/phpunit": ">=4.8",
Expand All @@ -22,5 +22,10 @@
"psr-0": {
"CrEOF\\Geo\\WKB": "lib/"
}
},
"autoload-dev": {
"psr-0": {
"CrEOF\\Geo\\WKB\\Tests": "tests/"
}
}
}
61 changes: 27 additions & 34 deletions lib/CrEOF/Geo/WKB/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ class Parser
/**
* @var Reader
*/
private static $reader;
private $reader;

/**
* @param string $input
*/
public function __construct($input = null)
{
self::$reader = new Reader();
$this->reader = new Reader();

if (null !== $input) {
self::$reader->load($input);
$this->reader->load($input);
}
}

Expand All @@ -121,7 +121,7 @@ public function __construct($input = null)
public function parse($input = null)
{
if (null !== $input) {
self::$reader->load($input);
$this->reader->load($input);
}

return $this->readGeometry();
Expand All @@ -144,12 +144,13 @@ private function readGeometry()
$this->srid = $this->readSrid();
}

$typeName = $this->getBaseTypeName($this->type);
$typeName = $this->getTypeName($this->type);

return array(
'type' => $this->getTypeName($this->type),
'srid' => $this->srid,
'value' => $this->$typeName()
'type' => $typeName,
'srid' => $this->srid,
'value' => $this->$typeName(),
'dimension' => $this->getDimension($this->type)
);
}

Expand All @@ -174,7 +175,7 @@ private function hasTypeFlag($type, $flag)
* @return string
* @throws UnexpectedValueException
*/
private function getBaseTypeName($type)
private function getTypeName($type)
{
switch ($type & 0xFFFF) {
case (self::WKB_TYPE_POINT):
Expand Down Expand Up @@ -225,31 +226,23 @@ private function getBaseTypeName($type)
}

/**
* @param $type
* @param int $type
*
* @return string
* @throws UnexpectedValueException
*/
private function getTypeName($type)
private function getDimension($type)
{
$typeName = $this->getBaseTypeName($type);
$suffix = '';

switch ($type & (self::WKB_FLAG_Z | self::WKB_FLAG_M)) {
case (0):
break;
case (self::WKB_FLAG_Z):
$suffix = ' Z';
break;
return 'Z';
case (self::WKB_FLAG_M):
$suffix = ' M';
break;
return 'M';
case (self::WKB_FLAG_Z | self::WKB_FLAG_M):
$suffix = ' ZM';
break;
return 'ZM';
}

return $typeName . $suffix;
return null;
}
/**
* Parse data byte order
Expand All @@ -258,7 +251,7 @@ private function getTypeName($type)
*/
private function readByteOrder()
{
return self::$reader->readByteOrder();
return $this->reader->readByteOrder();
}

/**
Expand All @@ -268,7 +261,7 @@ private function readByteOrder()
*/
private function readType()
{
return self::$reader->readLong();
return $this->reader->readLong();
}

/**
Expand All @@ -278,7 +271,7 @@ private function readType()
*/
private function readSrid()
{
return self::$reader->readLong();
return $this->reader->readLong();
}

/**
Expand All @@ -287,7 +280,7 @@ private function readSrid()
*/
private function readCount()
{
return self::$reader->readLong();
return $this->reader->readLong();
}

/**
Expand Down Expand Up @@ -342,7 +335,7 @@ private function readLinearRings($count)
*/
private function point()
{
return self::$reader->readDoubles($this->pointSize);
return $this->reader->readDoubles($this->pointSize);
}

/**
Expand Down Expand Up @@ -483,7 +476,7 @@ private function compoundCurve()
}

$values[] = array(
'type' => $this->getBaseTypeName($type),
'type' => $this->getTypeName($type),
'value' => $value,
);
}
Expand Down Expand Up @@ -521,7 +514,7 @@ private function curvePolygon()
}

$values[] = array(
'type' => $this->getBaseTypeName($type),
'type' => $this->getTypeName($type),
'value' => $value,
);
}
Expand Down Expand Up @@ -559,7 +552,7 @@ private function multiCurve()
}

$values[] = array(
'type' => $this->getBaseTypeName($type),
'type' => $this->getTypeName($type),
'value' => $value,
);
}
Expand Down Expand Up @@ -595,7 +588,7 @@ private function multiSurface()
}

$values[] = array(
'type' => $this->getBaseTypeName($type),
'type' => $this->getTypeName($type),
'value' => $value,
);
}
Expand Down Expand Up @@ -629,7 +622,7 @@ private function polyhedralSurface()
}

$values[] = array(
'type' => $this->getBaseTypeName($type),
'type' => $this->getTypeName($type),
'value' => $value,
);
}
Expand All @@ -652,7 +645,7 @@ private function geometryCollection()
$this->readByteOrder();

$type = $this->readType();
$typeName = $this->getBaseTypeName($type);
$typeName = $this->getTypeName($type);

$values[] = array(
'type' => $typeName,
Expand Down
25 changes: 23 additions & 2 deletions lib/CrEOF/Geo/WKB/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class Reader
*/
private $input;

/**
* @var int
*/
private static $machineByteOrder;

/**
* @param string $input
*/
Expand Down Expand Up @@ -99,7 +104,7 @@ public function readDouble()
{
$double = $this->unpackInput('d');

if (self::WKB_NDR === $this->getByteOrder()) {
if ($this->getMachineByteOrder() === $this->getByteOrder()) {
return $double;
}

Expand Down Expand Up @@ -161,9 +166,25 @@ private function getByteOrder()
private function unpackInput($format)
{
$code = version_compare(PHP_VERSION, '5.5.0-dev', '>=') ? 'a' : 'A';
$result = unpack(sprintf('%sresult/%s*input', $format, $code), $this->input);
$result = unpack($format . 'result/' . $code . '*input', $this->input);
$this->input = $result['input'];

return $result['result'];
}

/**
* @return bool
*/
private function getMachineByteOrder()
{
if (null !== self::$machineByteOrder) {
return self::$machineByteOrder;
}

$result = unpack('S', "\x01\x00");

self::$machineByteOrder = $result[1] === 1 ? self::WKB_NDR : self::WKB_XDR;

return self::$machineByteOrder;
}
}
23 changes: 15 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit backupGlobals="false"
colors="true"
bootstrap="./tests/TestInit.php"
>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="./vendor/autoload.php"
>

<testsuites>
<testsuite>
<testsuite name="Tests">
<directory>./tests/CrEOF/Geo/WKB/Tests</directory>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory suffix=".php">./vendor</directory>
</blacklist>
<whitelist>
<directory suffix=".php">lib</directory>
<exclude>
<directory>tests</directory>
<directory>vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

0 comments on commit e057132

Please sign in to comment.