FeatureStream
in package
Decode the features of a GeoJSON FeatureCollection stream with the native json_decode, by blocks of features, so the memory stays bounded.
The stream is read by chunks. The JSON structure of the features array is followed in a single pass: the depth of the objects and arrays, and whether the current position is inside a string. A feature ends when the depth goes back to the level of the features array, so the end of each feature is known exactly, whatever its content: nested objects, arrays of objects, strings containing braces, or any JSON structure in the properties.
Only the structural characters are examined one by one: the other characters are skipped with strcspn. The complete features of each chunk are then decoded together with json_decode.
Usage, with the response of a WFS GetFeature request in GeoJSON: foreach (FeatureStream::fromResponse($wfsRequest->process()) as $feature) { $value = $feature->properties->name; }
Table of Contents
Methods
- fromResponse() : Generator<int, object>
- Get the features of the GeoJSON body of a response.
- isGeoJsonResponse() : bool
- Check if a response contains GeoJSON.
- read() : Generator<int, object>
- Get the features of the GeoJSON stream.
- decodeFeatures() : array<string|int, mixed>
- Decode a block of complete features.
- findFeaturesStart() : false|int
- Find the beginning of the features array in the buffer.
- log() : void
- Log a message for the administrator.
- noFeature() : Generator<int, object>
- Get a generator without feature.
Methods
fromResponse()
Get the features of the GeoJSON body of a response.
public
static fromResponse(OGCResponse|ProxyResponse $response[, int $chunkSize = 32768 ]) : Generator<int, object>
An error response, with an HTTP error code or with a mime type which is not JSON, for example a QGIS Server exception in XML, gives no feature: the error is logged.
Parameters
- $response : OGCResponse|ProxyResponse
-
the response, for example of a WFS GetFeature request
- $chunkSize : int = 32768
-
the number of bytes read at once
Tags
Return values
Generator<int, object> —the features, as objects
isGeoJsonResponse()
Check if a response contains GeoJSON.
public
static isGeoJsonResponse(OGCResponse|ProxyResponse $response) : bool
The response must have a success code and a JSON mime type, for example application/vnd.geo+json or application/json, with or without charset.
Parameters
- $response : OGCResponse|ProxyResponse
-
the response
Return values
bool —true if the response contains GeoJSON
read()
Get the features of the GeoJSON stream.
public
static read(resource $stream[, int $chunkSize = 32768 ]) : Generator<int, object>
The stream is read by chunks, and the complete features of each chunk are decoded together. Only the current chunk and the decoded block of features are kept in memory.
Parameters
- $stream : resource
-
the GeoJSON FeatureCollection stream
- $chunkSize : int = 32768
-
the number of bytes read at once
Tags
Return values
Generator<int, object> —the features, as objects
decodeFeatures()
Decode a block of complete features.
private
static decodeFeatures(string $block) : array<string|int, mixed>
Parameters
- $block : string
-
the features separated by commas, maybe starting with the comma after the previous block
Tags
Return values
array<string|int, mixed> —the decoded features
findFeaturesStart()
Find the beginning of the features array in the buffer.
private
static findFeaturesStart(string $buffer) : false|int
Parameters
- $buffer : string
-
the beginning of the GeoJSON
Return values
false|int —the position after the "[" of the features array, false if it is not in the buffer yet
log()
Log a message for the administrator.
private
static log(string $message) : void
Parameters
- $message : string
-
the message
noFeature()
Get a generator without feature.
private
static noFeature() : Generator<int, object>
Return values
Generator<int, object> —an empty generator