Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
mbolli committed Nov 18, 2024
1 parent 4b60194 commit 05c2419
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 111 deletions.
2 changes: 0 additions & 2 deletions src/Data.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions src/Data/Feature.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/Data/FeatureCollection.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions src/Data/Geometry.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Data/Geometry/Type.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 42 additions & 46 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class Decoder {
* @throws GeobufException
*/
public static function decodeFileToJsonFile(string $geobufFile, string $jsonFile): bool|int {
return file_put_contents($jsonFile, static::decodeFileToJson($geobufFile));
return file_put_contents($jsonFile, self::decodeFileToJson($geobufFile));
}

/**
Expand All @@ -35,7 +35,7 @@ public static function decodeFileToJsonFile(string $geobufFile, string $jsonFile
* @throws GeobufException
*/
public static function decodeFileToJson(string $fileName): string {
return static::decodeToJson(file_get_contents($fileName));
return self::decodeToJson(file_get_contents($fileName));
}

/**
Expand All @@ -44,7 +44,7 @@ public static function decodeFileToJson(string $fileName): string {
* @throws GeobufException
*/
public static function decodeToJson(string $encodedInput): string {
return json_encode(static::decodeToArray($encodedInput));
return json_encode(self::decodeToArray($encodedInput));
}

/**
Expand All @@ -53,7 +53,7 @@ public static function decodeToJson(string $encodedInput): string {
* @throws GeobufException
*/
public static function decodeFileToArray(string $fileName): array {
return static::decodeToArray(file_get_contents($fileName));
return self::decodeToArray(file_get_contents($fileName));
}

/**
Expand All @@ -62,36 +62,36 @@ public static function decodeFileToArray(string $fileName): array {
* @throws GeobufException
*/
public static function decodeToArray(string $encodedInput): array {
static::$data = new Data();
static::$data->mergeFromString($encodedInput);
static::$e = 10 ** static::$data->getPrecision();
static::$dim = static::$data->getDimensions();
self::$data = new Data();
self::$data->mergeFromString($encodedInput);
self::$e = 10 ** self::$data->getPrecision();
self::$dim = self::$data->getDimensions();

try {
static::$data->mergeFromString($encodedInput);
self::$data->mergeFromString($encodedInput);
} catch (\Exception $e) {
throw new GeobufException('Error while decoding Geobuf: ' . $e->getMessage(), 0, $e);
}

switch (static::$data->getDataType()) {
switch (self::$data->getDataType()) {
case 'feature_collection':
return static::decodeFeatureCollection(static::$data->getFeatureCollection());
return self::decodeFeatureCollection(self::$data->getFeatureCollection());
case 'feature':
return static::decodeFeature(static::$data->getFeature());
return self::decodeFeature(self::$data->getFeature());
case 'geometry':
return static::decodeGeometry(static::$data->getGeometry());
return self::decodeGeometry(self::$data->getGeometry());
}

throw new GeobufException('Unknown data type ' . static::$data->getDataType());
throw new GeobufException('Unknown data type ' . self::$data->getDataType());
}

private static function decodeFeatureCollection(FeatureCollection $featureCollection): array {
$obj = ['type' => 'FeatureCollection', 'features' => []];

static::decodeProperties($featureCollection->getCustomProperties(), $featureCollection->getValues(), $obj);
self::decodeProperties($featureCollection->getCustomProperties(), $featureCollection->getValues(), $obj);

foreach ($featureCollection->getFeatures() as $feature) {
$obj['features'][] = static::decodeFeature($feature);
$obj['features'][] = self::decodeFeature($feature);
}

return $obj;
Expand All @@ -100,13 +100,13 @@ private static function decodeFeatureCollection(FeatureCollection $featureCollec
private static function decodeFeature(Feature $feature): array {
$obj = ['type' => 'Feature'];

static::decodeProperties($feature->getCustomProperties(), $feature->getValues(), $obj);
static::decodeId($feature, $obj);
self::decodeProperties($feature->getCustomProperties(), $feature->getValues(), $obj);
self::decodeId($feature, $obj);

$obj['geometry'] = static::decodeGeometry($feature->getGeometry());
$obj['geometry'] = self::decodeGeometry($feature->getGeometry());

if (\count($feature->getProperties()) > 0) {
$obj['properties'] = static::decodeProperties($feature->getProperties(), $feature->getValues());
$obj['properties'] = self::decodeProperties($feature->getProperties(), $feature->getValues());
}

return $obj;
Expand All @@ -115,14 +115,13 @@ private static function decodeFeature(Feature $feature): array {
/**
* @param RepeatedField|Value[] $values
*/
private static function decodeProperties(array|RepeatedField $props, array|RepeatedField $values, ?array &$dest = null): array {
$dest ??= [];
private static function decodeProperties(array|RepeatedField $props, array|RepeatedField $values, array &$dest = []): array {
$numProps = \count($props);
if ($numProps === 0) {
return $dest;
}

$keys = static::$data->getKeys();
$keys = self::$data->getKeys();
$r = $numProps > 2 ? range(0, $numProps - 1, 2) : [0];

foreach ($r as $i) {
Expand Down Expand Up @@ -157,45 +156,42 @@ private static function decodeId(Feature $feature, array &$objJson): void {
}
}

/**
* @return array
*/
private static function decodeGeometry(?Geometry $geometry): ?array {
if ($geometry === null) {
return null;
}
$gt = static::GEOMETRY_TYPES[$geometry->getType()];
$gt = self::GEOMETRY_TYPES[$geometry->getType()];
$obj = ['type' => $gt];

static::decodeProperties($geometry->getCustomProperties(), $geometry->getValues(), $obj);
self::decodeProperties($geometry->getCustomProperties(), $geometry->getValues(), $obj);

switch ($gt) {
case 'GeometryCollection':
$obj['geometries'] = array_map(
fn ($g) => static::decodeGeometry($g),
fn ($g) => self::decodeGeometry($g),
$geometry->getGeometries()
);
break;
case 'Point':
$obj['coordinates'] = static::decodePoint($geometry->getCoords());
$obj['coordinates'] = self::decodePoint($geometry->getCoords());
break;
case 'LineString':
case 'MultiPoint':
$obj['coordinates'] = static::decodeLine($geometry->getCoords());
$obj['coordinates'] = self::decodeLine($geometry->getCoords());
break;
case 'MultiLineString':
case 'Polygon':
$obj['coordinates'] = static::decodeMultiLine($geometry, $gt === 'Polygon');
$obj['coordinates'] = self::decodeMultiLine($geometry, $gt === 'Polygon');
break;
case 'MultiPolygon':
$obj['coordinates'] = static::decodeMultiPolygon($geometry);
$obj['coordinates'] = self::decodeMultiPolygon($geometry);
}

return $obj;
}

private static function decodeCoord(float $coord): float {
return $coord / static::$e;
return $coord / self::$e;
}

/**
Expand All @@ -204,7 +200,7 @@ private static function decodeCoord(float $coord): float {
private static function decodePoint($coords): array {
$return = [];
foreach ($coords as $coord) { // can't use array_map as $coords might be a RepeatedField
$return[] = static::decodeCoord((float) $coord);
$return[] = self::decodeCoord((float) $coord);
}

return $return;
Expand All @@ -216,22 +212,22 @@ private static function decodePoint($coords): array {
private static function decodeLine($coords, ?bool $isClosed = false): array {
$obj = [];
$numCoords = \count($coords);
$r = range(0, static::$dim - 1);
$r2 = $numCoords > static::$dim ? range(0, $numCoords - 1, static::$dim) : [0];
$p0 = array_fill(0, static::$dim, 0);
$r = range(0, self::$dim - 1);
$r2 = $numCoords > self::$dim ? range(0, $numCoords - 1, self::$dim) : [0];
$p0 = array_fill(0, self::$dim, 0);

foreach ($r2 as $i) {
$p = array_map(
fn ($j) => ($p0[$j] ?? 0) + ($coords[$i + $j] ?? 0),
$r
);
$obj[] = static::decodePoint($p);
$obj[] = self::decodePoint($p);
$p0 = $p;
}

if ($isClosed === true) {
$p = array_map(fn ($j) => $coords[$j], $r);
$obj[] = static::decodePoint($p);
$obj[] = self::decodePoint($p);
}

return $obj;
Expand All @@ -240,15 +236,15 @@ private static function decodeLine($coords, ?bool $isClosed = false): array {
private static function decodeMultiLine(Geometry $geometry, ?bool $isClosed = false): array {
$coords = $geometry->getCoords();
if (\count($geometry->getLengths()) === 0) {
return [static::decodeLine($coords, $isClosed)];
return [self::decodeLine($coords, $isClosed)];
}

$obj = [];
$i = 0;

foreach ($geometry->getLengths() as $length) {
$obj[] = static::decodeLine(\array_slice($coords, $i, $i + $length * static::$dim), $isClosed);
$i += $length * static::$dim;
$obj[] = self::decodeLine(\array_slice($coords, $i, $i + $length * self::$dim), $isClosed);
$i += $length * self::$dim;
}

return $obj;
Expand All @@ -258,7 +254,7 @@ private static function decodeMultiPolygon(Geometry $geometry): array {
$lengths = $geometry->getLengths();
$coords = $geometry->getCoords();
if (\count($lengths) === 0) {
return [[static::decodeLine($coords, true)]];
return [[self::decodeLine($coords, true)]];
}

$obj = [];
Expand All @@ -272,9 +268,9 @@ private static function decodeMultiPolygon(Geometry $geometry): array {
$rings = [];

foreach (\array_slice($coords, $j, $j + $numRings) as $l) {
$rings[] = static::decodeLine(\array_slice($coords, $i, $i + $l * static::$dim), true);
$rings[] = self::decodeLine(\array_slice($coords, $i, $i + $l * self::$dim), true);
++$j;
$i += $l * static::$dim;
$i += $l * self::$dim;
}

$obj[] = $rings;
Expand Down
Loading

0 comments on commit 05c2419

Please sign in to comment.