Skip to content

Commit

Permalink
add function proj_get_pj_info and test
Browse files Browse the repository at this point in the history
  • Loading branch information
swen100 committed Sep 13, 2021
1 parent 8094043 commit 56f1743
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A php extension to be usable with PHP7+ and next generation of libProj (6+).
### Basic API

**resource proj_create(string definition);**
Create a Proj resource coordinate system object from the projection definition suitable for use with proj_create_crs_to_crs_from_pj(). Be aware: the definitions used for proj_create have then to be in form of EPSG:xxx!
Create a Proj resource coordinate system object from the projection definition suitable for use with proj_create_crs_to_crs_from_pj(). Be aware: the definitions used for proj_create have then to be in form of "EPSG:xxx"!

**resource proj_create_crs_to_crs(string srcCrs, string tgtCrs);**
Create a Proj resource coordinate system object from the source SRID and target SRID.
Expand Down
1 change: 1 addition & 0 deletions php_proj.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ZEND_FUNCTION(proj_transform_point);
ZEND_FUNCTION(proj_is_latlong);
ZEND_FUNCTION(proj_is_geocent);
ZEND_FUNCTION(proj_get_def);
ZEND_FUNCTION(proj_get_pj_info);
ZEND_FUNCTION(proj_get_errno);
ZEND_FUNCTION(proj_get_errno_string);
ZEND_FUNCTION(proj_get_release);
Expand Down
33 changes: 33 additions & 0 deletions proj.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static zend_function_entry proj_functions[] = {
ZEND_FE(proj_is_latlong, NULL)
ZEND_FE(proj_is_geocent, NULL)
ZEND_FE(proj_get_def, NULL)
ZEND_FE(proj_get_pj_info, NULL)
ZEND_FE(proj_get_errno, NULL)
ZEND_FE(proj_get_errno_string, NULL)
ZEND_FE(proj_get_release, NULL)
Expand Down Expand Up @@ -850,6 +851,38 @@ ZEND_FUNCTION(proj_get_def)
RETURN_FALSE;
}

/**
*
* @param resource projection
* @param long
* @return mixed array or false on error
*/
ZEND_FUNCTION(proj_get_pj_info)
{
PJ_PROJ_INFO result;
zval *zpj;
PJ *pj;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(zpj)
ZEND_PARSE_PARAMETERS_END();

pj = (PJ*) zend_fetch_resource_ex(zpj, PHP_PROJ_RES_NAME, proj_destructor);

if (pj == NULL) {
RETURN_FALSE;
}

array_init(return_value);
result = proj_pj_info(pj);

add_assoc_string(return_value, "id", result.id ? result.id : "");
add_assoc_string(return_value, "definition", result.definition);
add_assoc_string(return_value, "description", result.description);
add_assoc_double(return_value, "accuracy", result.accuracy);
add_assoc_long(return_value, "has_inverse", result.has_inverse);
}

/*###########################################################################
Environment Functions
###########################################################################
Expand Down
24 changes: 19 additions & 5 deletions tests/proj_get_def.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ if (!extension_loaded('proj')) {
?>
--FILE--
<?php
$pj = proj_create_crs_to_crs('EPSG:3857', 'EPSG:25832');
if ($pj === false) {
die(proj_get_errno_string(proj_get_errno($pj)));
}
// with EPSG-code
$pj = proj_create_crs_to_crs("EPSG:3857", "EPSG:25832");
var_dump(proj_get_def($pj));

$pj = proj_create("EPSG:3857");
var_dump(proj_get_def($pj));

$pj = proj_create("EPSG:4326");
var_dump(proj_get_def($pj));

// with proj-string
$proj_wgs84 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
$proj_merc = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +units=m +k=1.0 +nadgrids=@null +no_defs";
$pj = proj_create_crs_to_crs($proj_wgs84, $proj_merc);
var_dump(proj_get_def($pj));

?>
--EXPECT--
string(109) "proj=pipeline step inv proj=webmerc lat_0=0 lon_0=0 x_0=0 y_0=0 ellps=WGS84 step proj=utm zone=32 ellps=GRS80"
string(109) "proj=pipeline step inv proj=webmerc lat_0=0 lon_0=0 x_0=0 y_0=0 ellps=WGS84 step proj=utm zone=32 ellps=GRS80"
string(0) ""
string(0) ""
string(114) "proj=pipeline step proj=unitconvert xy_in=deg xy_out=rad step proj=webmerc lat_0=0 lon_0=0 x_0=0 y_0=0 ellps=WGS84"
76 changes: 76 additions & 0 deletions tests/proj_get_pj_info.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--TEST--
proj_get_pj_info() function - basic test for proj_get_pj_info()
--SKIPIF--
<?php
if (!extension_loaded('proj')) {
echo 'skip proj extension not available';
}
?>
--FILE--
<?php
// with EPSG-code
$pj = proj_create_crs_to_crs("EPSG:3857", "EPSG:25832");
var_dump(proj_get_pj_info($pj));

$pj = proj_create("EPSG:3857");
var_dump(proj_get_pj_info($pj));

$pj = proj_create("EPSG:4326");
var_dump(proj_get_pj_info($pj));

// with proj-string
$proj_wgs84 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
$proj_merc = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +units=m +k=1.0 +nadgrids=@null +no_defs";
$pj = proj_create_crs_to_crs($proj_wgs84, $proj_merc);
var_dump(proj_get_pj_info($pj));

?>
--EXPECT--
array(5) {
["id"]=>
string(8) "pipeline"
["definition"]=>
string(109) "proj=pipeline step inv proj=webmerc lat_0=0 lon_0=0 x_0=0 y_0=0 ellps=WGS84 step proj=utm zone=32 ellps=GRS80"
["description"]=>
string(97) "Inverse of Popular Visualisation Pseudo-Mercator + Inverse of ETRS89 to WGS 84 (1) + UTM zone 32N"
["accuracy"]=>
float(1)
["has_inverse"]=>
int(1)
}
array(5) {
["id"]=>
string(0) ""
["definition"]=>
string(0) ""
["description"]=>
string(24) "WGS 84 / Pseudo-Mercator"
["accuracy"]=>
float(-1)
["has_inverse"]=>
int(0)
}
array(5) {
["id"]=>
string(0) ""
["definition"]=>
string(0) ""
["description"]=>
string(6) "WGS 84"
["accuracy"]=>
float(-1)
["has_inverse"]=>
int(0)
}
array(5) {
["id"]=>
string(8) "pipeline"
["definition"]=>
string(114) "proj=pipeline step proj=unitconvert xy_in=deg xy_out=rad step proj=webmerc lat_0=0 lon_0=0 x_0=0 y_0=0 ellps=WGS84"
["description"]=>
string(62) "axis order change (2D) + Popular Visualisation Pseudo-Mercator"
["accuracy"]=>
float(0)
["has_inverse"]=>
int(1)
}

0 comments on commit 56f1743

Please sign in to comment.