Skip to content

Commit

Permalink
Merge pull request #30 from php-geospatial/interpolate-line-tests
Browse files Browse the repository at this point in the history
Add tests and documentation for 'interpolate_linestring'
  • Loading branch information
derickr committed Mar 5, 2024
2 parents 44028ca + f299589 commit bbe86d2
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 10 deletions.
27 changes: 27 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ you would use::

var_dump(fraction_along_gc_line($point1, $point2, 0.25));

Interpolating a GeoJSONLineString
---------------------------------

The ``interpolate_linestring`` functions takes a GeoJSONLineString. If the
Pythagorean distance in degrees between two points in the line than the
``epsilon`` value, it inserts a new point every ``epsilon / distance``
fraction of the Great Circle Line.

Given the Linestring::

$lineString = [
'type' => 'Linestring',
'coordinates' => [
[ 5, 10 ],
[ 15, 10 ],
[ 0, -50 ],
]
];

The following will return an array with 26 elements::

var_dump(interpolate_linestring($lineString, 3));

Five for the first pair, at fractions ``0.0``, ``0.3``, ``0.6``, ``0.9`` and
``1.0``, and then two times, each another ``0.0485`` fraction along the Great
Circle Line for the second pair.

Geohashing
----------

Expand Down
4 changes: 2 additions & 2 deletions php_geospatial.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ function haversine(array $from, array $to, float $radius = GEO_EARTH_RADIUS): fl
function vincenty(array $from, array $to, float $reference_ellipsoid = GEO_WGS84): float {}

function fraction_along_gc_line(array $from, array $to, float $fraction): array {}
function interpolate_linestring(array $line, float $epsilon): array {}

function initial_bearing(array $from, array $to): float {}

function rdp_simplify(array $points, float $epsilon): array {}

function interpolate_linestring(array $line, float $epsilon): array {}

function geohash_encode(array $point, int $precision = 12): string {}
function geohash_decode(string $geohash): array {}
16 changes: 8 additions & 8 deletions php_geospatial_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 52022e47a6841ea20db60c2c92eba319cfc6c563 */
* Stub hash: 0fadd3390095e7624d9586206523e5352d345729 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dms_to_decimal, 0, 3, IS_DOUBLE, 0)
ZEND_ARG_TYPE_INFO(0, degrees, IS_DOUBLE, 0)
Expand Down Expand Up @@ -58,6 +58,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_fraction_along_gc_line, 0, 3, IS
ZEND_ARG_TYPE_INFO(0, fraction, IS_DOUBLE, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_interpolate_linestring, 0, 2, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, line, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, epsilon, IS_DOUBLE, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_initial_bearing, 0, 2, IS_DOUBLE, 0)
ZEND_ARG_TYPE_INFO(0, from, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, to, IS_ARRAY, 0)
Expand All @@ -68,11 +73,6 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rdp_simplify, 0, 2, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, epsilon, IS_DOUBLE, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_interpolate_linestring, 0, 2, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, line, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, epsilon, IS_DOUBLE, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_geohash_encode, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, point, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, precision, IS_LONG, 0, "12")
Expand All @@ -92,9 +92,9 @@ ZEND_FUNCTION(transform_datum);
ZEND_FUNCTION(haversine);
ZEND_FUNCTION(vincenty);
ZEND_FUNCTION(fraction_along_gc_line);
ZEND_FUNCTION(interpolate_linestring);
ZEND_FUNCTION(initial_bearing);
ZEND_FUNCTION(rdp_simplify);
ZEND_FUNCTION(interpolate_linestring);
ZEND_FUNCTION(geohash_encode);
ZEND_FUNCTION(geohash_decode);

Expand All @@ -109,9 +109,9 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(haversine, arginfo_haversine)
ZEND_FE(vincenty, arginfo_vincenty)
ZEND_FE(fraction_along_gc_line, arginfo_fraction_along_gc_line)
ZEND_FE(interpolate_linestring, arginfo_interpolate_linestring)
ZEND_FE(initial_bearing, arginfo_initial_bearing)
ZEND_FE(rdp_simplify, arginfo_rdp_simplify)
ZEND_FE(interpolate_linestring, arginfo_interpolate_linestring)
ZEND_FE(geohash_encode, arginfo_geohash_encode)
ZEND_FE(geohash_decode, arginfo_geohash_decode)
ZEND_FE_END
Expand Down
200 changes: 200 additions & 0 deletions tests/interpolate-line-string-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
--TEST--
Test for "interpolate_linestring" #1
--FILE--
<?php
$lineString = [
'type' => 'Linestring',
'coordinates' => [
[ 5, 10 ],
[ 15, 10 ],
[ 0, -50 ],
]
];

var_dump(interpolate_linestring($lineString, 3));
?>
--EXPECTF--
array(26) {
[0]=>
array(2) {
[0]=>
float(5)
[1]=>
float(10)
}
[1]=>
array(2) {
[0]=>
float(7.9998706635703245)
[1]=>
float(10.03143197167244)
}
[2]=>
array(2) {
[0]=>
float(11.000073920872426)
[1]=>
float(10.035925156338873)
}
[3]=>
array(2) {
[0]=>
float(14.000110773799255)
[1]=>
float(10.013466491669567)
}
[4]=>
array(2) {
[0]=>
float(14.999999999999998)
[1]=>
float(10)
}
[5]=>
array(2) {
[0]=>
float(14.431497984318062)
[1]=>
float(7.0743500242581225)
}
[6]=>
array(2) {
[0]=>
float(13.87016260996529)
[1]=>
float(4.148019326790382)
}
[7]=>
array(2) {
[0]=>
float(13.312974140472463)
[1]=>
float(1.221294808989993)
}
[8]=>
array(2) {
[0]=>
float(12.756998952507347)
[1]=>
float(-1.7055449208364544)
}
[9]=>
array(2) {
[0]=>
float(12.199328445763877)
[1]=>
float(-4.632223663590317)
}
[10]=>
array(2) {
[0]=>
float(11.63701903368877)
[1]=>
float(-7.55846185163128)
}
[11]=>
array(2) {
[0]=>
float(11.067030621797189)
[1]=>
float(-10.483970622513857)
}
[12]=>
array(2) {
[0]=>
float(10.486160869758637)
[1]=>
float(-13.408445478413974)
}
[13]=>
array(2) {
[0]=>
float(9.890972221197222)
[1]=>
float(-16.331559233911236)
}
[14]=>
array(2) {
[0]=>
float(9.277708136408956)
[1]=>
float(-19.252953899216553)
}
[15]=>
array(2) {
[0]=>
float(8.642194115015831)
[1]=>
float(-22.172231059691807)
}
[16]=>
array(2) {
[0]=>
float(7.979717846837639)
[1]=>
float(-25.08894018486976)
}
[17]=>
array(2) {
[0]=>
float(7.284881023816488)
[1]=>
float(-28.002564114424196)
}
[18]=>
array(2) {
[0]=>
float(6.55141274516024)
[1]=>
float(-30.91250069881375)
}
[19]=>
array(2) {
[0]=>
float(5.771930687062061)
[1]=>
float(-33.81803917851203)
}
[20]=>
array(2) {
[0]=>
float(4.937630724808758)
[1]=>
float(-36.718329304916004)
}
[21]=>
array(2) {
[0]=>
float(4.037877612215368)
[1]=>
float(-39.61234033808264)
}
[22]=>
array(2) {
[0]=>
float(3.059657259010722)
[1]=>
float(-42.49880573947054)
}
[23]=>
array(2) {
[0]=>
float(1.9868328958488306)
[1]=>
float(-45.37614734526896)
}
[24]=>
array(2) {
[0]=>
float(0.7991194226357126)
[1]=>
float(-48.2423696103279)
}
[25]=>
array(2) {
[0]=>
float(0)
[1]=>
float(-50)
}
}
59 changes: 59 additions & 0 deletions tests/interpolate-line-string-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
Test for "interpolate_linestring" #2
--FILE--
<?php
$lineString = [
'type' => 'Linestring',
'coordinates' => [
[ 0, 0 ],
[ 90, 0 ],
]
];

var_dump(interpolate_linestring($lineString, 20));
?>
--EXPECTF--
array(6) {
[0]=>
array(2) {
[0]=>
float(0)
[1]=>
float(0)
}
[1]=>
array(2) {
[0]=>
float(19.999999999999996)
[1]=>
float(0)
}
[2]=>
array(2) {
[0]=>
float(40.00000000000001)
[1]=>
float(0)
}
[3]=>
array(2) {
[0]=>
float(59.99999999999999)
[1]=>
float(0)
}
[4]=>
array(2) {
[0]=>
float(80)
[1]=>
float(0)
}
[5]=>
array(2) {
[0]=>
float(90)
[1]=>
float(0)
}
}
Loading

0 comments on commit bbe86d2

Please sign in to comment.