From 138285037de24e00edafae55fbd75b3f85e8dc92 Mon Sep 17 00:00:00 2001 From: nono303 Date: Tue, 31 Jan 2023 16:33:35 +0100 Subject: [PATCH 01/20] fix gdal lib possible names add header gdal.h check php version add message if failure --- config.w32 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/config.w32 b/config.w32 index 1e16c41..691e050 100644 --- a/config.w32 +++ b/config.w32 @@ -1,7 +1,20 @@ +// vim:ft=javascript + ARG_WITH("ogr", "OGR support", "no"); if (PHP_OGR != "no") { - if(CHECK_LIB("gdal_i.lib", "ogr", PHP_OGR) && CHECK_HEADER_ADD_INCLUDE("ogr_srs_api.h", "CFLAGS_OGR", PHP_OGR+"\\include") + if( + CHECK_LIB("gdal.lib;gdal_i.lib", "ogr", PHP_OGR) && + CHECK_HEADER_ADD_INCLUDE("ogr_srs_api.h", "CFLAGS_OGR", PHP_OGR + "\\include") && + CHECK_HEADER_ADD_INCLUDE("gdal.h", "CFLAGS_OGR", PHP_OGR + "\\include") ) { - EXTENSION("ogr", "ogr.c", true); + if (get_define('PHPDLL').match(/^php[78]/) != null) { + EXTENSION("ogr", "ogr.c", true); + AC_DEFINE('HAVE_OGR', 1, 'Have ogr module'); + PHP_INSTALL_HEADERS("ext/ogr/", "php_ogr.h"); + } else { + WARNING("gdal support can't be enabled: " + dll + " unsupported") + } + } else { + WARNING("ogr support can't be enabled: libraries or headers are missing") } -} +} \ No newline at end of file From 4dc0466e9ba5f308ff1e85d3e1ce93df312b3b5e Mon Sep 17 00:00:00 2001 From: nono303 Date: Tue, 31 Jan 2023 16:34:28 +0100 Subject: [PATCH 02/20] define "ogr" as PHP_OGR_EXTNAME --- php_ogr.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php_ogr.h b/php_ogr.h index 5f2165b..5f48cae 100755 --- a/php_ogr.h +++ b/php_ogr.h @@ -30,9 +30,10 @@ #ifndef PHP_OGR_H -#define PHP_OGR_H +#define PHP_OGR_H 1 #define PHP_OGR_VERSION "1.6.1" +#define PHP_OGR_EXTNAME "ogr" extern zend_module_entry ogr_module_entry; #define phpext_ogr_ptr &ogr_module_entry From 0cd9a93d28fac1f6db8a1e74cc09791e14c0e8a6 Mon Sep 17 00:00:00 2001 From: nono303 Date: Tue, 31 Jan 2023 16:35:08 +0100 Subject: [PATCH 03/20] add PHP_FUNCTION gdal_open_dataset, gdal_locationinfo, gdal_free --- ogr.c | 1454 +++++++++++++++++++++++++++---------------------- php_ogr_api.h | 19 + 2 files changed, 827 insertions(+), 646 deletions(-) diff --git a/ogr.c b/ogr.c index 5555cfd..27a3c9b 100755 --- a/ogr.c +++ b/ogr.c @@ -51,6 +51,7 @@ #include "cpl_error.h" #include "cpl_conv.h" #include "cpl_string.h" +#include "gdal.h" /* If you declare any globals in php_ogr.h uncomment this: ZEND_DECLARE_MODULE_GLOBALS(ogr) @@ -72,7 +73,7 @@ zend_module_entry ogr_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif - "ogr", + PHP_OGR_EXTNAME, ogr_functions, PHP_MINIT(ogr), PHP_MSHUTDOWN(ogr), @@ -95,6 +96,7 @@ ZEND_GET_MODULE(ogr) /* True global resources - no need for thread safety here */ +static int le_Dataset; static int le_Datasource; static int le_SFDriver; static int le_Layer; @@ -114,6 +116,17 @@ static int le_Feature; /*Resource destructors function.*/ /* }}} */ +/* {{{ gdal_free_Dataset() */ +static void +gdal_free_Dataset(zend_resource *resource) +{ + GDALDatasetH *hSrcDS = (GDALDatasetH*) resource; + if (hSrcDS != NULL) + GDALClose(hSrcDS); +} + +/* }}} */ + /* {{{ ogr_free_Datasource() */ static void @@ -295,11 +308,16 @@ ogr_free_Feature(zend_resource_t *rsrc TSRMLS_DC) */ PHP_MINIT_FUNCTION(ogr) { - + // register GDAL Drivers + GDALAllRegister(); /* If you have INI entries, uncomment these lines ZEND_INIT_MODULE_GLOBALS(ogr, php_ogr_init_globals, NULL); REGISTER_INI_ENTRIES(); */ + le_Dataset = zend_register_list_destructors_ex(gdal_free_Dataset, + NULL, "GDALDataset", + module_number); + le_Datasource = zend_register_list_destructors_ex(ogr_free_Datasource, NULL, "OGRDatasource", module_number); @@ -569,6 +587,8 @@ PHP_MSHUTDOWN_FUNCTION(ogr) /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ + GDALDumpOpenDatasets(stderr); + GDALDestroyDriverManager(); return SUCCESS; } /* }}} */ @@ -599,6 +619,7 @@ PHP_MINFO_FUNCTION(ogr) php_info_print_table_header(2, "ogr support", "enabled"); php_info_print_table_row(2, "PHP_OGR Version", PHP_OGR_VERSION); php_info_print_table_row(2, "GDAL/OGR Version", GDAL_RELEASE_NAME); + php_info_print_table_row(2, "GDAL Build Info", GDALVersionInfo("BUILD_INFO")); php_info_print_table_end(); /* Remove comments if you have entries in php.ini @@ -691,6 +712,147 @@ PHP_FUNCTION(cplgetlasterrormsg) _RETURN_DUPLICATED_STRING((char *)pszMsg); } +/********************************************************************** + * GDAL functions + **********************************************************************/ + + /** + * @param resource Proj + * @return void + */ +PHP_FUNCTION(gdal_free) { + GDALDatasetH *hSrcDS; + zval *zgdal; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_RESOURCE(zgdal) + ZEND_PARSE_PARAMETERS_END(); + + hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); + if (hSrcDS != NULL) + GDALClose(hSrcDS); +} + +/** + * + * @param string gdal dataset filename + * @return resource + */ +PHP_FUNCTION(gdal_open_dataset) { + GDALDatasetH *hSrcDS; + zend_string *pszSrcFilename; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STR(pszSrcFilename) + ZEND_PARSE_PARAMETERS_END(); + + hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, NULL, NULL, NULL); + if (hSrcDS == NULL){ + char error[128]; + sprintf(error, "Can not open dataset '%s'", ZSTR_VAL(pszSrcFilename)); + zend_throw_exception(NULL, error,0); + } + RETURN_RES(zend_register_resource(hSrcDS, le_Dataset)); +} + +/** + * https://gis.stackexchange.com/a/393550 + * @param float lonX + * @param float latY + * @param string file + * @return array with dataset infos & required band value + */ +PHP_FUNCTION(gdal_locationinfo) { + double lonX, latY = 0; + zend_string *pszSrcFilename; + GDALDatasetH *hSrcDS; + GDALRasterBandH hBand; + zval *zgdal; + + ZEND_PARSE_PARAMETERS_START(3, 3) + Z_PARAM_RESOURCE(zgdal) + Z_PARAM_DOUBLE(lonX) + Z_PARAM_DOUBLE(latY) + ZEND_PARSE_PARAMETERS_END(); + + hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); + if (hSrcDS == NULL) + zend_throw_exception(NULL, "Gdal is null",0); + // Assume there is only one band in the raster source and use that + hBand = GDALGetRasterBand(hSrcDS, 1); + if (hBand == NULL) + zend_throw_exception(NULL, "Gdal has no band",0); + double adfGeoTransform[6]; + if (GDALGetGeoTransform(hSrcDS, adfGeoTransform) != CE_None) + zend_throw_exception(NULL, "Cannot get geotransform",0); + double adfInvGeoTransform[6]; + if (!GDALInvGeoTransform(adfGeoTransform, adfInvGeoTransform)) + zend_throw_exception(NULL, "Cannot invert geotransform",0); + int rasterXSize = GDALGetRasterXSize(hSrcDS); + int rasterYSize = GDALGetRasterYSize(hSrcDS); + int iPixel = floor(adfInvGeoTransform[0] + + adfInvGeoTransform[1] * lonX + + adfInvGeoTransform[2] * latY); + int iLine = floor(adfInvGeoTransform[3] + + adfInvGeoTransform[4] * lonX + + adfInvGeoTransform[5] * latY); + if (iPixel < 0 || iLine < 0 || iPixel >= rasterXSize || iLine >= rasterYSize){ + char error[64]; + sprintf(error, "Pixel [%u,%u] is off this file [%ux%u]", iPixel, iLine, rasterXSize, rasterYSize); + zend_throw_exception(NULL, error,0); + } + double adfPixel[2]; + char iovalue[30]; + if (GDALRasterIO(hBand, GF_Read, iPixel, iLine, 1, 1, adfPixel, 1, 1, GDT_CFloat64, 0, 0) == CE_None){ + if (GDALDataTypeIsComplex(GDALGetRasterDataType(hBand))){ + sprintf(iovalue, "%.15g+%.15gi", adfPixel[0], adfPixel[1]); + } else { + sprintf(iovalue, "%.15g", adfPixel[0]); + } + + } else { + zend_throw_exception(NULL, "Cannot get GDALRasterIO",0); + } + array_init(return_value); + + // LocationInfo for vrt + char osItem[32]; + sprintf(osItem,"Pixel_%d_%d", iPixel, iLine); + const char *pszLI = GDALGetMetadataItem(hBand, osItem, "LocationInfo"); + if(pszLI != NULL){ + CPLXMLNode *psRoot = CPLParseXMLString(pszLI); + if (psRoot != NULL && psRoot->psChild != NULL && + psRoot->eType == CXT_Element && + EQUAL(psRoot->pszValue, "LocationInfo")) { + for (CPLXMLNode *psNode = psRoot->psChild; + psNode != NULL; psNode = psNode->psNext) { + if (psNode->eType == CXT_Element && + EQUAL(psNode->pszValue, "File") && + psNode->psChild != NULL) { + char *pszUnescaped = + CPLUnescapeString(psNode->psChild->pszValue, NULL, CPLES_XML); + add_assoc_string(return_value, "locationInfo", pszUnescaped); + CPLFree(pszUnescaped); + } + } + } + } + + add_assoc_string(return_value, "value", iovalue); + add_assoc_double(return_value, "iPixel", iPixel); + add_assoc_double(return_value, "iLine", iLine); + add_assoc_double(return_value, "lonX", lonX); + add_assoc_double(return_value, "latY", latY); + //add_assoc_string(return_value, "raster", ZSTR_VAL(pszSrcFilename)); + //add_assoc_string(return_value, "spatialRef", OSRGetName(GDALGetSpatialRef(hSrcDS))); + char *projectionRef = GDALGetProjectionRef(hSrcDS); + if(*projectionRef) + add_assoc_string(return_value, "projectionRef", projectionRef); + add_assoc_double(return_value, "xSize", rasterXSize); + add_assoc_double(return_value, "ySize", rasterYSize); + add_assoc_double(return_value, "bandCount", GDALGetRasterCount(hSrcDS)); +} + /********************************************************************** * OGR functions **********************************************************************/ @@ -985,9 +1147,9 @@ PHP_FUNCTION(ogr_g_exporttowkb) "OGRGeometryH", le_Geometry, le_GeometryRef); } if (hGeometry) { - isize = OGR_G_WkbSize(hGeometry); - strwkb = emalloc(isize); - eErr = OGR_G_ExportToWkb(hGeometry, ibyteorder, strwkb); + isize = OGR_G_WkbSize(hGeometry); + strwkb = emalloc(isize); + eErr = OGR_G_ExportToWkb(hGeometry, ibyteorder, strwkb); } if (eErr != OGRERR_NONE){ @@ -2980,15 +3142,15 @@ PHP_FUNCTION(ogr_f_getfieldasintegerlist) RETURN_NULL(); } - array_init(return_value); + array_init(return_value); _ZVAL_PTR_DTOR(refncount); ZVAL_LONG(refncount, ncount); - while (numelements < ncount) { + while (numelements < ncount) { add_next_index_long(return_value, panList[numelements]); numelements++; - } + } } /* }}} */ @@ -3025,15 +3187,15 @@ PHP_FUNCTION(ogr_f_getfieldasdoublelist) RETURN_NULL(); } - array_init(return_value); + array_init(return_value); _ZVAL_PTR_DTOR(refncount); ZVAL_DOUBLE(refncount, ncount); - while (numelements < ncount) { + while (numelements < ncount) { add_next_index_double(return_value, padfList[numelements]); numelements++; - } + } } @@ -3077,13 +3239,13 @@ PHP_FUNCTION(ogr_f_getfieldasstringlist) RETURN_NULL(); } - array_init(return_value); + array_init(return_value); - while (numelements < ncount) { + while (numelements < ncount) { _ADD_NEXT_INDEX_STRING(return_value, (char *) CSLGetField(papszStrList, numelements)); numelements++; - } + } } /* }}} */ @@ -3200,7 +3362,7 @@ PHP_FUNCTION(ogr_f_setfielddatetime) } if(hFeat){ OGR_F_SetFieldDateTime(hFeat, ifield, iYear, iMonth, iDay, - iHour, iMinute, iSecond, iTZFlag); + iHour, iMinute, iSecond, iTZFlag); } } @@ -3231,13 +3393,13 @@ PHP_FUNCTION(ogr_f_setfieldintegerlist) "OGRFeature", le_Feature); } - numelements = zend_hash_num_elements(Z_ARRVAL_P(refanvalues)); + numelements = zend_hash_num_elements(Z_ARRVAL_P(refanvalues)); if ((numelements != ncount) || (numelements <= 0)){ php_report_ogr_error(E_WARNING); RETURN_FALSE; } - alTmp = (int *) CPLMalloc(sizeof(int)*ncount); + alTmp = (int *) CPLMalloc(sizeof(int)*ncount); numelements = 0; @@ -3281,14 +3443,14 @@ PHP_FUNCTION(ogr_f_setfielddoublelist) "OGRFeature", le_Feature); } - numelements = zend_hash_num_elements(Z_ARRVAL_P(refadfvalues)); + numelements = zend_hash_num_elements(Z_ARRVAL_P(refadfvalues)); if ((numelements != ncount) || (numelements <= 0)){ php_report_ogr_error(E_WARNING); RETURN_FALSE; } - adfTmp = (double *) CPLMalloc(sizeof(double)*ncount); + adfTmp = (double *) CPLMalloc(sizeof(double)*ncount); numelements = 0; @@ -4738,22 +4900,22 @@ PHP_FUNCTION(osr_newspatialreference) */ PHP_FUNCTION(osr_destroyspatialreference) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - /* Actual destroy occurs when PHP doesn't hold any more references to SRS - * - see ogr_free_SpatialReference() - */ + /* Actual destroy occurs when PHP doesn't hold any more references to SRS + * - see ogr_free_SpatialReference() + */ _ZEND_FREE_RESOURCE(hsrs); } } @@ -4761,80 +4923,80 @@ PHP_FUNCTION(osr_destroyspatialreference) /* }}} */ /* {{{ proto int osr_reference(reference hsrs) - */ + */ PHP_FUNCTION(osr_reference) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) - RETURN_LONG(OSRReference(hSpatialReference)); + RETURN_LONG(OSRReference(hSpatialReference)); } /* }}} */ /* {{{ proto int osr_dereference(reference hsrs) - */ + */ PHP_FUNCTION(osr_dereference) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) - RETURN_LONG(OSRDereference(hSpatialReference)); + RETURN_LONG(OSRDereference(hSpatialReference)); } /* }}} */ /* {{{ proto void osr_release(reference hsrs) - */ + */ PHP_FUNCTION(osr_release) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; - int refs; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - /* We don't want the reference to be deleted in the background as this - * leads to a segfault in PHP as it tries to delete it, so we simulate - * the behaviour of release, namely dereference the SRS and then destroy - * it if zero - * - * Actual destroy occurs when PHP doesn't hold any more references to SRS - * - see ogr_free_SpatialReference() - */ - refs = OSRDereference(hSpatialReference); - if (refs == 0) { // provoke the destroy - _ZEND_FREE_RESOURCE(hsrs); - } - } + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; + int refs; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + /* We don't want the reference to be deleted in the background as this + * leads to a segfault in PHP as it tries to delete it, so we simulate + * the behaviour of release, namely dereference the SRS and then destroy + * it if zero + * + * Actual destroy occurs when PHP doesn't hold any more references to SRS + * - see ogr_free_SpatialReference() + */ + refs = OSRDereference(hSpatialReference); + if (refs == 0) { // provoke the destroy + _ZEND_FREE_RESOURCE(hsrs); + } + } } /* }}} */ @@ -4843,21 +5005,21 @@ PHP_FUNCTION(osr_release) */ PHP_FUNCTION(osr_validate) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRValidate(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRValidate(hSpatialReference)); + } } /* }}} */ @@ -4868,21 +5030,21 @@ PHP_FUNCTION(osr_validate) */ PHP_FUNCTION(osr_fixupordering) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRFixupOrdering(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRFixupOrdering(hSpatialReference)); + } } /* }}} */ @@ -4891,21 +5053,21 @@ PHP_FUNCTION(osr_fixupordering) */ PHP_FUNCTION(osr_fixup) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRFixup(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRFixup(hSpatialReference)); + } } /* }}} */ @@ -4914,21 +5076,21 @@ PHP_FUNCTION(osr_fixup) */ PHP_FUNCTION(osr_stripctparms) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRStripCTParms(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRStripCTParms(hSpatialReference)); + } } /* }}} */ @@ -4941,19 +5103,19 @@ PHP_FUNCTION(osr_importfromepsg) { int argc = ZEND_NUM_ARGS(); zend_long nCode = -1; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!l", &hsrs, &nCode) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRImportFromEPSG(hSpatialReference, nCode)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRImportFromEPSG(hSpatialReference, nCode)); + } } /* }}} */ @@ -4964,19 +5126,19 @@ PHP_FUNCTION(osr_importfromepsga) { int argc = ZEND_NUM_ARGS(); zend_long nCode = -1; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!l", &hsrs, &nCode) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRImportFromEPSGA(hSpatialReference, nCode)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRImportFromEPSGA(hSpatialReference, nCode)); + } } /* }}} */ @@ -4988,21 +5150,21 @@ PHP_FUNCTION(osr_importfromwkt) int argc = ZEND_NUM_ARGS(); char *refstrdata = NULL; strsize_t refstrdata_len; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!s", &hsrs, &refstrdata, - &refstrdata_len) == FAILURE) + &refstrdata_len) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference && refstrdata){ - RETURN_LONG(OSRImportFromWkt(hSpatialReference, &refstrdata)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference && refstrdata){ + RETURN_LONG(OSRImportFromWkt(hSpatialReference, &refstrdata)); + } } /* }}} */ @@ -5014,21 +5176,21 @@ PHP_FUNCTION(osr_importfromproj4) int argc = ZEND_NUM_ARGS(); char *refstrdata = NULL; strsize_t refstrdata_len; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!s", &hsrs, &refstrdata, - &refstrdata_len) == FAILURE) + &refstrdata_len) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference && refstrdata){ - RETURN_LONG(OSRImportFromProj4(hSpatialReference, refstrdata)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference && refstrdata){ + RETURN_LONG(OSRImportFromProj4(hSpatialReference, refstrdata)); + } } /* }}} */ @@ -5038,8 +5200,8 @@ PHP_FUNCTION(osr_importfromproj4) PHP_FUNCTION(osr_importfromesri) { int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *prjdata = NULL; + int hsrs_id = -1; + zval *prjdata = NULL; zval *hsrs = NULL; char **prjstrings = NULL; int i = 0; @@ -5049,20 +5211,20 @@ PHP_FUNCTION(osr_importfromesri) if (zend_parse_parameters(argc TSRMLS_CC, "r!a!", &hsrs, &prjdata) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - /* OSRImportFromESRI requires a null-terminated list of strings as input */ - if (prjdata) { - prjstrings = php_array2string(prjstrings, prjdata); - } - if (hSpatialReference && prjstrings){ - res = OSRImportFromESRI(hSpatialReference, prjstrings); - } - /* tidy up newly allocated string list */ - CSLDestroy(prjstrings); - RETURN_LONG(res); + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + /* OSRImportFromESRI requires a null-terminated list of strings as input */ + if (prjdata) { + prjstrings = php_array2string(prjstrings, prjdata); + } + if (hSpatialReference && prjstrings){ + res = OSRImportFromESRI(hSpatialReference, prjstrings); + } + /* tidy up newly allocated string list */ + CSLDestroy(prjstrings); + RETURN_LONG(res); } /* }}} */ @@ -5071,23 +5233,23 @@ PHP_FUNCTION(osr_importfromesri) */ PHP_FUNCTION(osr_exporttowkt) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - char *refres = NULL; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + char *refres = NULL; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; OGRErr eErr = OGRERR_FAILURE; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - eErr = OSRExportToWkt(hSpatialReference, &refres); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + eErr = OSRExportToWkt(hSpatialReference, &refres); + } if (refres) { RETURN_CPL_STRING(refres); @@ -5101,24 +5263,24 @@ PHP_FUNCTION(osr_exporttowkt) */ PHP_FUNCTION(osr_exporttoprettywkt) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zend_bool simplify = 0; - char *refres = NULL; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zend_bool simplify = 0; + char *refres = NULL; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; OGRErr eErr = OGRERR_FAILURE; - if (zend_parse_parameters(argc TSRMLS_CC, "r!b", &hsrs, &simplify) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!b", &hsrs, &simplify) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - eErr = OSRExportToPrettyWkt(hSpatialReference, &refres, (int) simplify); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + eErr = OSRExportToPrettyWkt(hSpatialReference, &refres, (int) simplify); + } if (refres) { RETURN_CPL_STRING(refres); @@ -5132,23 +5294,23 @@ PHP_FUNCTION(osr_exporttoprettywkt) */ PHP_FUNCTION(osr_exporttoproj4) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - char *refres = NULL; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + char *refres = NULL; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; OGRErr eErr = OGRERR_FAILURE; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - eErr = OSRExportToProj4(hSpatialReference, &refres); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + eErr = OSRExportToProj4(hSpatialReference, &refres); + } if (refres) { RETURN_CPL_STRING(refres); @@ -5162,21 +5324,21 @@ PHP_FUNCTION(osr_exporttoproj4) */ PHP_FUNCTION(osr_morphtoesri) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRMorphToESRI(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRMorphToESRI(hSpatialReference)); + } } /* }}} */ @@ -5185,143 +5347,143 @@ PHP_FUNCTION(osr_morphtoesri) */ PHP_FUNCTION(osr_morphfromesri) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRMorphFromESRI(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRMorphFromESRI(hSpatialReference)); + } } /* }}} */ /* {{{ proto string osr_getattrvalue(reference hsrs, string refNodeName, int iAttr) - */ + */ PHP_FUNCTION(osr_getattrvalue) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; char *refNodeName = NULL; const char *res = NULL; strsize_t refNodeName_len; zend_long iAttr = 0; - OGRSpatialReferenceH hSpatialReference = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!s|l", &hsrs, &refNodeName, - &refNodeName_len, &iAttr) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!s|l", &hsrs, &refNodeName, + &refNodeName_len, &iAttr) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) - res = OSRGetAttrValue(hSpatialReference, refNodeName, iAttr); + res = OSRGetAttrValue(hSpatialReference, refNodeName, iAttr); if (res) { - _RETURN_DUPLICATED_STRING(res); + _RETURN_DUPLICATED_STRING(res); } } /* }}} */ /* {{{ proto array osr_getangularunits(reference hsrs) - */ + */ PHP_FUNCTION(osr_getangularunits) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; char *refName = NULL; double units = 0.0; - OGRSpatialReferenceH hSpatialReference = NULL; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + OGRSpatialReferenceH hSpatialReference = NULL; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - units = OSRGetAngularUnits(hSpatialReference, &refName); - array_init(return_value); - add_assoc_double(return_value, "multiplier", units); - if (refName) { - _ADD_ASSOC_STRING(return_value, "name", refName); - } else { - add_assoc_null(return_value, "name"); - } + units = OSRGetAngularUnits(hSpatialReference, &refName); + array_init(return_value); + add_assoc_double(return_value, "multiplier", units); + if (refName) { + _ADD_ASSOC_STRING(return_value, "name", refName); + } else { + add_assoc_null(return_value, "name"); + } } } /* }}} */ /* {{{ proto array osr_getlinearunits(reference hsrs) - */ + */ PHP_FUNCTION(osr_getlinearunits) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; char *refName = NULL; double units = 0.0; - OGRSpatialReferenceH hSpatialReference = NULL; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + OGRSpatialReferenceH hSpatialReference = NULL; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - units = OSRGetLinearUnits(hSpatialReference, &refName); - array_init(return_value); - add_assoc_double(return_value, "multiplier", units); - if (refName) { - _ADD_ASSOC_STRING(return_value, "name", refName); - } else { - add_assoc_null(return_value, "name"); - } + units = OSRGetLinearUnits(hSpatialReference, &refName); + array_init(return_value); + add_assoc_double(return_value, "multiplier", units); + if (refName) { + _ADD_ASSOC_STRING(return_value, "name", refName); + } else { + add_assoc_null(return_value, "name"); + } } } /* }}} */ /* {{{ proto array osr_getprimemeridian(reference hsrs) - */ + */ PHP_FUNCTION(osr_getprimemeridian) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; char *refName = NULL; double units = 0.0; - OGRSpatialReferenceH hSpatialReference = NULL; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + OGRSpatialReferenceH hSpatialReference = NULL; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - units = OSRGetPrimeMeridian(hSpatialReference, &refName); - array_init(return_value); - add_assoc_double(return_value, "offset", units); - if (refName) { - _ADD_ASSOC_STRING(return_value, "name", refName); - } else { - add_assoc_null(return_value, "name"); - } + units = OSRGetPrimeMeridian(hSpatialReference, &refName); + array_init(return_value); + add_assoc_double(return_value, "offset", units); + if (refName) { + _ADD_ASSOC_STRING(return_value, "name", refName); + } else { + add_assoc_null(return_value, "name"); + } } } @@ -5331,21 +5493,21 @@ PHP_FUNCTION(osr_getprimemeridian) */ PHP_FUNCTION(osr_isgeographic) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_BOOL(OSRIsGeographic(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_BOOL(OSRIsGeographic(hSpatialReference)); + } } /* }}} */ @@ -5354,21 +5516,21 @@ PHP_FUNCTION(osr_isgeographic) */ PHP_FUNCTION(osr_islocal) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_BOOL(OSRIsLocal(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_BOOL(OSRIsLocal(hSpatialReference)); + } } /* }}} */ @@ -5377,21 +5539,21 @@ PHP_FUNCTION(osr_islocal) */ PHP_FUNCTION(osr_isprojected) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_BOOL(OSRIsProjected(hSpatialReference)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_BOOL(OSRIsProjected(hSpatialReference)); + } } /* }}} */ @@ -5402,21 +5564,21 @@ PHP_FUNCTION(osr_isprojected) PHP_FUNCTION(osr_isgeocentric) { #if GDAL_VERSION_NUM >= 1900 - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_BOOL(OSRIsGeocentric(hSpatialReference)); - } + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_BOOL(OSRIsGeocentric(hSpatialReference)); + } #endif } @@ -5428,21 +5590,21 @@ PHP_FUNCTION(osr_isgeocentric) PHP_FUNCTION(osr_isvertical) { #if GDAL_VERSION_NUM >= 1730 - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_BOOL(OSRIsVertical(hSpatialReference)); - } + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_BOOL(OSRIsVertical(hSpatialReference)); + } #endif } @@ -5452,26 +5614,26 @@ PHP_FUNCTION(osr_isvertical) */ PHP_FUNCTION(osr_issamegeogcs) { - int argc = ZEND_NUM_ARGS(); - int hsrs1_id = -1; - int hsrs2_id = -1; - zval *hsrs1 = NULL; - zval *hsrs2 = NULL; - OGRSpatialReferenceH hSpatialReference1 = NULL; - OGRSpatialReferenceH hSpatialReference2 = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs1_id = -1; + int hsrs2_id = -1; + zval *hsrs1 = NULL; + zval *hsrs2 = NULL; + OGRSpatialReferenceH hSpatialReference1 = NULL; + OGRSpatialReferenceH hSpatialReference2 = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!r!", &hsrs1, &hsrs2) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!r!", &hsrs1, &hsrs2) == FAILURE) + return; - if (hsrs1 && hsrs2) { - _ZEND_FETCH_RESOURCE2(hSpatialReference1, OGRSpatialReferenceH, hsrs1, hsrs1_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - _ZEND_FETCH_RESOURCE2(hSpatialReference2, OGRSpatialReferenceH, hsrs2, hsrs2_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference1 && hSpatialReference2){ - RETURN_BOOL(OSRIsSameGeogCS(hSpatialReference1, hSpatialReference2)); - } + if (hsrs1 && hsrs2) { + _ZEND_FETCH_RESOURCE2(hSpatialReference1, OGRSpatialReferenceH, hsrs1, hsrs1_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + _ZEND_FETCH_RESOURCE2(hSpatialReference2, OGRSpatialReferenceH, hsrs2, hsrs2_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference1 && hSpatialReference2){ + RETURN_BOOL(OSRIsSameGeogCS(hSpatialReference1, hSpatialReference2)); + } } /* }}} */ @@ -5480,26 +5642,26 @@ PHP_FUNCTION(osr_issamegeogcs) */ PHP_FUNCTION(osr_issame) { - int argc = ZEND_NUM_ARGS(); - int hsrs1_id = -1; - int hsrs2_id = -1; - zval *hsrs1 = NULL; - zval *hsrs2 = NULL; - OGRSpatialReferenceH hSpatialReference1 = NULL; - OGRSpatialReferenceH hSpatialReference2 = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs1_id = -1; + int hsrs2_id = -1; + zval *hsrs1 = NULL; + zval *hsrs2 = NULL; + OGRSpatialReferenceH hSpatialReference1 = NULL; + OGRSpatialReferenceH hSpatialReference2 = NULL; - if (zend_parse_parameters(argc TSRMLS_CC, "r!r!", &hsrs1, &hsrs2) == FAILURE) - return; + if (zend_parse_parameters(argc TSRMLS_CC, "r!r!", &hsrs1, &hsrs2) == FAILURE) + return; - if (hsrs1 && hsrs2) { - _ZEND_FETCH_RESOURCE2(hSpatialReference1, OGRSpatialReferenceH, hsrs1, hsrs1_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - _ZEND_FETCH_RESOURCE2(hSpatialReference2, OGRSpatialReferenceH, hsrs2, hsrs2_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference1 && hSpatialReference2){ - RETURN_BOOL(OSRIsSame(hSpatialReference1, hSpatialReference2)); - } + if (hsrs1 && hsrs2) { + _ZEND_FETCH_RESOURCE2(hSpatialReference1, OGRSpatialReferenceH, hsrs1, hsrs1_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + _ZEND_FETCH_RESOURCE2(hSpatialReference2, OGRSpatialReferenceH, hsrs2, hsrs2_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference1 && hSpatialReference2){ + RETURN_BOOL(OSRIsSame(hSpatialReference1, hSpatialReference2)); + } } /* }}} */ @@ -5511,71 +5673,71 @@ PHP_FUNCTION(osr_setfromuserinput) int argc = ZEND_NUM_ARGS(); char *refstrdata = NULL; strsize_t refstrdata_len; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!s", &hsrs, &refstrdata, - &refstrdata_len) == FAILURE) + &refstrdata_len) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference){ - RETURN_LONG(OSRSetFromUserInput(hSpatialReference, refstrdata)); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference){ + RETURN_LONG(OSRSetFromUserInput(hSpatialReference, refstrdata)); + } } /* }}} */ /* {{{ proto mixed osr_gettowgs84(reference hsrs) - */ + */ PHP_FUNCTION(osr_gettowgs84) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; double towgs[] = {NAN, NAN, NAN, NAN, NAN, NAN, NAN}; - OGRSpatialReferenceH hSpatialReference = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; OGRErr eErr = OGRERR_FAILURE; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - eErr = OSRGetTOWGS84(hSpatialReference, &towgs[0], 7); - if (eErr == OGRERR_NONE) { - array_init(return_value); - add_assoc_double(return_value, "Dx_BF", towgs[0]); - add_assoc_double(return_value, "Dy_BF", towgs[1]); - add_assoc_double(return_value, "Dz_BF", towgs[2]); - // TOWGS84 has between 3 and 7 parameters... - if (!isnan(towgs[3])) { - add_assoc_double(return_value, "Rx_BF", towgs[3]); - } else { - add_assoc_null(return_value, "Rx_BF"); - } - if (!isnan(towgs[4])) { - add_assoc_double(return_value, "Ry_BF", towgs[4]); - } else { - add_assoc_null(return_value, "Ry_BF"); - } - if (!isnan(towgs[5])) { - add_assoc_double(return_value, "Rz_BF", towgs[5]); - } else { - add_assoc_null(return_value, "Rz_BF"); - } - if (!isnan(towgs[6])) { - add_assoc_double(return_value, "M_BF", towgs[6]); - } else { - add_assoc_null(return_value, "M_BF"); - } - } + eErr = OSRGetTOWGS84(hSpatialReference, &towgs[0], 7); + if (eErr == OGRERR_NONE) { + array_init(return_value); + add_assoc_double(return_value, "Dx_BF", towgs[0]); + add_assoc_double(return_value, "Dy_BF", towgs[1]); + add_assoc_double(return_value, "Dz_BF", towgs[2]); + // TOWGS84 has between 3 and 7 parameters... + if (!isnan(towgs[3])) { + add_assoc_double(return_value, "Rx_BF", towgs[3]); + } else { + add_assoc_null(return_value, "Rx_BF"); + } + if (!isnan(towgs[4])) { + add_assoc_double(return_value, "Ry_BF", towgs[4]); + } else { + add_assoc_null(return_value, "Ry_BF"); + } + if (!isnan(towgs[5])) { + add_assoc_double(return_value, "Rz_BF", towgs[5]); + } else { + add_assoc_null(return_value, "Rz_BF"); + } + if (!isnan(towgs[6])) { + add_assoc_double(return_value, "M_BF", towgs[6]); + } else { + add_assoc_null(return_value, "M_BF"); + } + } } } @@ -5585,24 +5747,24 @@ PHP_FUNCTION(osr_gettowgs84) */ PHP_FUNCTION(osr_getsemimajor) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - double semiMajor; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + double semiMajor; + OGRSpatialReferenceH hSpatialReference = NULL; OGRErr eErr = OGRERR_NONE; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - semiMajor = OSRGetSemiMajor(hSpatialReference, &eErr); - if (eErr != OGRERR_FAILURE) { - RETURN_DOUBLE(semiMajor); - } + semiMajor = OSRGetSemiMajor(hSpatialReference, &eErr); + if (eErr != OGRERR_FAILURE) { + RETURN_DOUBLE(semiMajor); + } } } @@ -5612,24 +5774,24 @@ PHP_FUNCTION(osr_getsemimajor) */ PHP_FUNCTION(osr_getsemiminor) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - double semiMinor; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + double semiMinor; + OGRSpatialReferenceH hSpatialReference = NULL; OGRErr eErr = OGRERR_NONE; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - semiMinor = OSRGetSemiMinor(hSpatialReference, &eErr); - if (eErr != OGRERR_FAILURE) { - RETURN_DOUBLE(semiMinor); - } + semiMinor = OSRGetSemiMinor(hSpatialReference, &eErr); + if (eErr != OGRERR_FAILURE) { + RETURN_DOUBLE(semiMinor); + } } } @@ -5639,24 +5801,24 @@ PHP_FUNCTION(osr_getsemiminor) */ PHP_FUNCTION(osr_getinvflattening) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - double invFlattening; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + double invFlattening; + OGRSpatialReferenceH hSpatialReference = NULL; OGRErr eErr = OGRERR_NONE; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - invFlattening = OSRGetInvFlattening(hSpatialReference, &eErr); - if (eErr != OGRERR_FAILURE) { - RETURN_DOUBLE(invFlattening); - } + invFlattening = OSRGetInvFlattening(hSpatialReference, &eErr); + if (eErr != OGRERR_FAILURE) { + RETURN_DOUBLE(invFlattening); + } } } @@ -5669,25 +5831,25 @@ PHP_FUNCTION(osr_getauthoritycode) int argc = ZEND_NUM_ARGS(); char *refTargetKey = NULL; strsize_t refTargetKey_len = 0; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; const char *res = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!s!", &hsrs, &refTargetKey, - &refTargetKey_len) == FAILURE) + &refTargetKey_len) == FAILURE) return; if (refTargetKey_len == 0) refTargetKey = NULL; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference) { - res = OSRGetAuthorityCode(hSpatialReference, refTargetKey); - if (res) _RETURN_DUPLICATED_STRING(res); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference) { + res = OSRGetAuthorityCode(hSpatialReference, refTargetKey); + if (res) _RETURN_DUPLICATED_STRING(res); + } } /* }}} */ @@ -5699,25 +5861,25 @@ PHP_FUNCTION(osr_getauthorityname) int argc = ZEND_NUM_ARGS(); char *refTargetKey = NULL; strsize_t refTargetKey_len = 0; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; const char *res = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!s!", &hsrs, &refTargetKey, - &refTargetKey_len) == FAILURE) + &refTargetKey_len) == FAILURE) return; if (refTargetKey_len == 0) refTargetKey = NULL; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference) { - res = OSRGetAuthorityName(hSpatialReference, refTargetKey); - if (res) _RETURN_DUPLICATED_STRING(res); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference) { + res = OSRGetAuthorityName(hSpatialReference, refTargetKey); + if (res) _RETURN_DUPLICATED_STRING(res); + } } /* }}} */ @@ -5729,29 +5891,29 @@ PHP_FUNCTION(osr_getprojparm) int argc = ZEND_NUM_ARGS(); char *refName = NULL; strsize_t refName_len; - int hsrs_id = -1; - double defaultValue = 0.0; + int hsrs_id = -1; + double defaultValue = 0.0; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; double res; OGRErr eErr = OGRERR_NONE; if (zend_parse_parameters(argc TSRMLS_CC, "r!s|d", &hsrs, &refName, - &refName_len, &defaultValue) == FAILURE) + &refName_len, &defaultValue) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference) { - res = OSRGetProjParm(hSpatialReference, refName, defaultValue, &eErr); - if (eErr == OGRERR_NONE) { - RETURN_DOUBLE(res); - } else if (argc > 2) { - RETURN_DOUBLE(defaultValue); - } - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference) { + res = OSRGetProjParm(hSpatialReference, refName, defaultValue, &eErr); + if (eErr == OGRERR_NONE) { + RETURN_DOUBLE(res); + } else if (argc > 2) { + RETURN_DOUBLE(defaultValue); + } + } } /* }}} */ @@ -5763,29 +5925,29 @@ PHP_FUNCTION(osr_getnormprojparm) int argc = ZEND_NUM_ARGS(); char *refName = NULL; strsize_t refName_len; - int hsrs_id = -1; - double defaultValue = 0.0; + int hsrs_id = -1; + double defaultValue = 0.0; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; double res; OGRErr eErr = OGRERR_NONE; if (zend_parse_parameters(argc TSRMLS_CC, "r!s|d", &hsrs, &refName, - &refName_len, &defaultValue) == FAILURE) + &refName_len, &defaultValue) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - if (hSpatialReference) { - res = OSRGetNormProjParm(hSpatialReference, refName, defaultValue, &eErr); - if (eErr == OGRERR_NONE) { - RETURN_DOUBLE(res); - } else if (argc > 2) { - RETURN_DOUBLE(defaultValue); - } - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + if (hSpatialReference) { + res = OSRGetNormProjParm(hSpatialReference, refName, defaultValue, &eErr); + if (eErr == OGRERR_NONE) { + RETURN_DOUBLE(res); + } else if (argc > 2) { + RETURN_DOUBLE(defaultValue); + } + } } /* }}} */ @@ -5794,28 +5956,28 @@ PHP_FUNCTION(osr_getnormprojparm) */ PHP_FUNCTION(osr_getutmzone) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - int utmZone; - OGRSpatialReferenceH hSpatialReference = NULL; + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + int utmZone; + OGRSpatialReferenceH hSpatialReference = NULL; int north = 0; - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - utmZone = OSRGetUTMZone(hSpatialReference, &north); - if (utmZone && !north) { - /* SWIG bindings return negative in southern - * hemisphere instead of using flag so lets do the same - */ - utmZone = -utmZone; - } - if (utmZone) RETURN_LONG(utmZone); + utmZone = OSRGetUTMZone(hSpatialReference, &north); + if (utmZone && !north) { + /* SWIG bindings return negative in southern + * hemisphere instead of using flag so lets do the same + */ + utmZone = -utmZone; + } + if (utmZone) RETURN_LONG(utmZone); } } @@ -5825,21 +5987,21 @@ PHP_FUNCTION(osr_getutmzone) */ PHP_FUNCTION(osr_autoidentifyepsg) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRErr res; - OGRSpatialReferenceH hSpatialReference = NULL; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRErr res; + OGRSpatialReferenceH hSpatialReference = NULL; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - res = OSRAutoIdentifyEPSG(hSpatialReference); - RETURN_LONG(res); + res = OSRAutoIdentifyEPSG(hSpatialReference); + RETURN_LONG(res); } } @@ -5849,19 +6011,19 @@ PHP_FUNCTION(osr_autoidentifyepsg) */ PHP_FUNCTION(osr_epsgtreatsaslatlong) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - OGRSpatialReferenceH hSpatialReference = NULL; - - if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) - return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + OGRSpatialReferenceH hSpatialReference = NULL; + + if (zend_parse_parameters(argc TSRMLS_CC, "r!", &hsrs) == FAILURE) + return; + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { - RETURN_BOOL(OSREPSGTreatsAsLatLong(hSpatialReference)); + RETURN_BOOL(OSREPSGTreatsAsLatLong(hSpatialReference)); } } @@ -5875,7 +6037,7 @@ PHP_FUNCTION(osr_getaxis) char *refTargetKey = NULL; strsize_t refTargetKey_len; zend_long iAxis = 0; - int hsrs_id = -1; + int hsrs_id = -1; zval *hsrs = NULL; OGRSpatialReferenceH hSpatialReference = NULL; char *res = NULL; @@ -5883,15 +6045,15 @@ PHP_FUNCTION(osr_getaxis) char *res_orient = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "r!sl", &hsrs, &refTargetKey, - &refTargetKey_len, &iAxis) == FAILURE) + &refTargetKey_len, &iAxis) == FAILURE) return; - if (hsrs) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } + if (hsrs) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } if (hSpatialReference) { res = (char *) OSRGetAxis(hSpatialReference, refTargetKey, iAxis, &orientation); - if (res) { + if (res) { array_init(return_value); _ADD_ASSOC_STRING(return_value, "name", res); if (orientation) { @@ -5902,7 +6064,7 @@ PHP_FUNCTION(osr_getaxis) } else { add_assoc_null(return_value, "orientation"); } - } + } } } @@ -5912,22 +6074,22 @@ PHP_FUNCTION(osr_getaxis) */ PHP_FUNCTION(is_osr) { - int argc = ZEND_NUM_ARGS(); - int hsrs_id = -1; - zval *hsrs = NULL; - int zvalType = -1; - OGRSpatialReferenceH hSpatialReference = NULL; - zend_parse_parameters(argc TSRMLS_CC, "z!", &hsrs); - - if (hsrs) { - if (Z_TYPE_P(hsrs) == IS_RESOURCE) { - _ZEND_LIST_FINDD(hsrs, zvalType); - if ((zvalType == le_SpatialReference) || (zvalType == le_SpatialReferenceRef)) { - _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, - "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); - } - } - } + int argc = ZEND_NUM_ARGS(); + int hsrs_id = -1; + zval *hsrs = NULL; + int zvalType = -1; + OGRSpatialReferenceH hSpatialReference = NULL; + zend_parse_parameters(argc TSRMLS_CC, "z!", &hsrs); + + if (hsrs) { + if (Z_TYPE_P(hsrs) == IS_RESOURCE) { + _ZEND_LIST_FINDD(hsrs, zvalType); + if ((zvalType == le_SpatialReference) || (zvalType == le_SpatialReferenceRef)) { + _ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs, hsrs_id, + "OGRSpatialReferenceH", le_SpatialReference, le_SpatialReferenceRef); + } + } + } RETURN_BOOL(hSpatialReference && 1); } diff --git a/php_ogr_api.h b/php_ogr_api.h index 92f26df..d828ea6 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -35,6 +35,9 @@ PHP_FUNCTION(cplerrorreset); PHP_FUNCTION(cplgetlasterrorno); PHP_FUNCTION(cplgetlasterrortype); PHP_FUNCTION(cplgetlasterrormsg); +PHP_FUNCTION(gdal_open_dataset); +PHP_FUNCTION(gdal_locationinfo); +PHP_FUNCTION(gdal_free); PHP_FUNCTION(ogr_ds_destroy); PHP_FUNCTION(ogropen); PHP_FUNCTION(ogr_g_createfromwkb); @@ -1101,11 +1104,27 @@ ZEND_END_ARG_INFO() /* * Every user visible function must have an entry in ogr_functions[] */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_gdal_free, 0, 0, 0) +ZEND_END_ARG_INFO() + +_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_open_dataset, 0, 1, IS_RESOURCE, NULL, 0) + _ZEND_ARG_TYPE_INFO(0, pszSrcFilename, IS_STRING, 0) +ZEND_END_ARG_INFO() + +_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_locationinfo, 0, 3, IS_ARRAY, NULL, 1) + _ZEND_ARG_TYPE_INFO(0, zgdal, IS_RESOURCE, 1) + _ZEND_ARG_TYPE_INFO(0, lonX, IS_DOUBLE, 0) + _ZEND_ARG_TYPE_INFO(0, latY, IS_DOUBLE, 0) +ZEND_END_ARG_INFO() + static const zend_function_entry ogr_functions[] = { PHP_FE(cplerrorreset, arginfo_cplerrorreset) PHP_FE(cplgetlasterrorno, arginfo_cplgetlasterrorno) PHP_FE(cplgetlasterrortype, arginfo_cplgetlasterrortype) PHP_FE(cplgetlasterrormsg, arginfo_cplgetlasterrormsg) + PHP_FE(gdal_open_dataset, arginfo_gdal_open_dataset) + PHP_FE(gdal_locationinfo, arginfo_gdal_locationinfo) + PHP_FE(gdal_free, arginfo_gdal_free) PHP_FE(ogr_g_createfromwkb, arginfo_ogr_g_createfromwkb) PHP_FE(ogr_g_createfromwkt, arginfo_ogr_g_createfromwkt) PHP_FE(ogr_g_destroygeometry, arginfo_ogr_g_destroygeometry) From daf32d1384229b642f4c1ae0ba1b82a1b9606e32 Mon Sep 17 00:00:00 2001 From: nono303 Date: Fri, 3 Feb 2023 23:17:59 +0100 Subject: [PATCH 04/20] gdal_locationinfo: add optional parameter epsgIn for OGRCoordinateTransformation --- ogr.c | 51 +++++++++++++++++++++++++++++++++++++-------------- php_ogr_api.h | 1 + 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/ogr.c b/ogr.c index 27a3c9b..8c8203d 100755 --- a/ogr.c +++ b/ogr.c @@ -748,7 +748,7 @@ PHP_FUNCTION(gdal_open_dataset) { hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, NULL, NULL, NULL); if (hSrcDS == NULL){ - char error[128]; + char error[128]; sprintf(error, "Can not open dataset '%s'", ZSTR_VAL(pszSrcFilename)); zend_throw_exception(NULL, error,0); } @@ -763,18 +763,22 @@ PHP_FUNCTION(gdal_open_dataset) { * @return array with dataset infos & required band value */ PHP_FUNCTION(gdal_locationinfo) { - double lonX, latY = 0; + double lonX, latY; + zend_long epsgIn; zend_string *pszSrcFilename; GDALDatasetH *hSrcDS; GDALRasterBandH hBand; zval *zgdal; - - ZEND_PARSE_PARAMETERS_START(3, 3) + + ZEND_PARSE_PARAMETERS_START(3, 4) Z_PARAM_RESOURCE(zgdal) Z_PARAM_DOUBLE(lonX) Z_PARAM_DOUBLE(latY) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(epsgIn) ZEND_PARSE_PARAMETERS_END(); + array_init(return_value); hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); if (hSrcDS == NULL) zend_throw_exception(NULL, "Gdal is null",0); @@ -790,6 +794,28 @@ PHP_FUNCTION(gdal_locationinfo) { zend_throw_exception(NULL, "Cannot invert geotransform",0); int rasterXSize = GDALGetRasterXSize(hSrcDS); int rasterYSize = GDALGetRasterYSize(hSrcDS); + + // !! must be casted + if((int)epsgIn > 1){ + OGRSpatialReferenceH hTrgSRS = OSRNewSpatialReference(GDALGetProjectionRef(hSrcDS)); + add_assoc_string(return_value, "rasterSRS", OSRGetName(hTrgSRS)); + // !! must be up of if + OGRSpatialReferenceH hSrcSRS = OSRNewSpatialReference(NULL); + OSRImportFromEPSG(hSrcSRS, (int)epsgIn); + OSRSetAxisMappingStrategy(hSrcSRS, OAMS_TRADITIONAL_GIS_ORDER); + OGRCoordinateTransformationH hCT = OCTNewCoordinateTransformation(hSrcSRS,hTrgSRS); + double inX = lonX; + double inY = latY; + add_assoc_double(return_value, "inEpsg", (int)epsgIn); + add_assoc_double(return_value, "inX", inX); + add_assoc_double(return_value, "inY", inY); + OCTTransform(hCT,1,&lonX,&latY,NULL); + add_assoc_string(return_value, "inSRS", OSRGetName(hSrcSRS)); + OCTDestroyCoordinateTransformation(hCT); + OSRDestroySpatialReference(hSrcSRS); + OSRDestroySpatialReference(hTrgSRS); + } + int iPixel = floor(adfInvGeoTransform[0] + adfInvGeoTransform[1] * lonX + adfInvGeoTransform[2] * latY); @@ -813,8 +839,6 @@ PHP_FUNCTION(gdal_locationinfo) { } else { zend_throw_exception(NULL, "Cannot get GDALRasterIO",0); } - array_init(return_value); - // LocationInfo for vrt char osItem[32]; sprintf(osItem,"Pixel_%d_%d", iPixel, iLine); @@ -839,18 +863,17 @@ PHP_FUNCTION(gdal_locationinfo) { } add_assoc_string(return_value, "value", iovalue); - add_assoc_double(return_value, "iPixel", iPixel); - add_assoc_double(return_value, "iLine", iLine); add_assoc_double(return_value, "lonX", lonX); add_assoc_double(return_value, "latY", latY); - //add_assoc_string(return_value, "raster", ZSTR_VAL(pszSrcFilename)); - //add_assoc_string(return_value, "spatialRef", OSRGetName(GDALGetSpatialRef(hSrcDS))); + add_assoc_double(return_value, "rasterPixel", iPixel); + add_assoc_double(return_value, "rasterLine", iLine); + char *projectionRef = GDALGetProjectionRef(hSrcDS); if(*projectionRef) - add_assoc_string(return_value, "projectionRef", projectionRef); - add_assoc_double(return_value, "xSize", rasterXSize); - add_assoc_double(return_value, "ySize", rasterYSize); - add_assoc_double(return_value, "bandCount", GDALGetRasterCount(hSrcDS)); + add_assoc_string(return_value, "rasterProjectionRef", projectionRef); + add_assoc_double(return_value, "rasterXSize", rasterXSize); + add_assoc_double(return_value, "rasterYSize", rasterYSize); + add_assoc_double(return_value, "rasterBandCount", GDALGetRasterCount(hSrcDS)); } /********************************************************************** diff --git a/php_ogr_api.h b/php_ogr_api.h index d828ea6..d9d628f 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -1115,6 +1115,7 @@ _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_locationinfo, 0, 3, IS_ARR _ZEND_ARG_TYPE_INFO(0, zgdal, IS_RESOURCE, 1) _ZEND_ARG_TYPE_INFO(0, lonX, IS_DOUBLE, 0) _ZEND_ARG_TYPE_INFO(0, latY, IS_DOUBLE, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, epsgIn, IS_LONG, 0, 0) ZEND_END_ARG_INFO() static const zend_function_entry ogr_functions[] = { From ecf1538e14e6b7a787997fac6b8f0ecab6c61a68 Mon Sep 17 00:00:00 2001 From: nono303 Date: Sat, 4 Feb 2023 09:31:40 +0100 Subject: [PATCH 05/20] register CPLErrorHandler > zend_throw_exception add get_ogr_error_string() fix null pointer exceptions fix optional epsg default parameter issue fix OGRSpatialReference definition --- ogr.c | 110 ++++++++++++++++++++++++++++++++------------------ php_ogr_api.h | 2 +- 2 files changed, 72 insertions(+), 40 deletions(-) diff --git a/ogr.c b/ogr.c index 8c8203d..ea06093 100755 --- a/ogr.c +++ b/ogr.c @@ -133,8 +133,8 @@ static void ogr_free_Datasource(zend_resource_t *rsrc TSRMLS_DC) { OGRDataSourceH hds = (OGRDataSourceH)rsrc->ptr; - - OGR_DS_Destroy( hds ); + if(hds != NULL) + OGR_DS_Destroy( hds ); } @@ -301,6 +301,13 @@ ogr_free_Feature(zend_resource_t *rsrc TSRMLS_DC) } +// https://github.com/shuhblam/simple-tiles/blob/master/src/error.c +static void ogr_error_handler(CPLErr eclass, int err_no, const char *msg) +{ + // (void)eclass, (void)err_no, (void)msg; + zend_throw_exception(NULL, msg,err_no); + return; +} /* }}} */ @@ -310,6 +317,8 @@ PHP_MINIT_FUNCTION(ogr) { // register GDAL Drivers GDALAllRegister(); + // Install custom error handler + CPLSetErrorHandler(ogr_error_handler); /* If you have INI entries, uncomment these lines ZEND_INIT_MODULE_GLOBALS(ogr, php_ogr_init_globals, NULL); REGISTER_INI_ENTRIES(); @@ -652,6 +661,30 @@ static char ** php_array2string(char **papszStrList, zval *refastrvalues) return papszStrList; } +static char* get_ogr_error_string(int ogrerrid) +{ + switch ( ogrerrid ){ + case OGRERR_NONE: + return "Success"; + case OGRERR_NOT_ENOUGH_DATA: + return "Not enough data to deserialize"; + case OGRERR_NOT_ENOUGH_MEMORY: + return "Not enough memory"; + case OGRERR_UNSUPPORTED_GEOMETRY_TYPE: + return "Unsupported geometry type"; + case OGRERR_UNSUPPORTED_OPERATION: + return "Unsupported operation"; + case OGRERR_CORRUPT_DATA: + return "Corrupt data"; + case OGRERR_FAILURE: + return "Failure"; + case OGRERR_UNSUPPORTED_SRS: + return "Unsupported SRS"; + case OGRERR_INVALID_HANDLE: + return "Invalid handle"; + } +} + /********************************************************************** * Error handling functions **********************************************************************/ @@ -745,14 +778,15 @@ PHP_FUNCTION(gdal_open_dataset) { ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(pszSrcFilename) ZEND_PARSE_PARAMETERS_END(); - - hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, NULL, NULL, NULL); - if (hSrcDS == NULL){ - char error[128]; + // | GDAL_OF_VERBOSE_ERROR + hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER, NULL, NULL, NULL); + if (hSrcDS != NULL){ + RETURN_RES(zend_register_resource(hSrcDS, le_Dataset)); + } else { + char error[128]; sprintf(error, "Can not open dataset '%s'", ZSTR_VAL(pszSrcFilename)); zend_throw_exception(NULL, error,0); } - RETURN_RES(zend_register_resource(hSrcDS, le_Dataset)); } /** @@ -763,11 +797,11 @@ PHP_FUNCTION(gdal_open_dataset) { * @return array with dataset infos & required band value */ PHP_FUNCTION(gdal_locationinfo) { - double lonX, latY; - zend_long epsgIn; + double lonX, latY = 0; + int epsg = 0; zend_string *pszSrcFilename; GDALDatasetH *hSrcDS; - GDALRasterBandH hBand; + GDALRasterBandH *hBand; zval *zgdal; ZEND_PARSE_PARAMETERS_START(3, 4) @@ -775,7 +809,7 @@ PHP_FUNCTION(gdal_locationinfo) { Z_PARAM_DOUBLE(lonX) Z_PARAM_DOUBLE(latY) Z_PARAM_OPTIONAL - Z_PARAM_LONG(epsgIn) + Z_PARAM_LONG(epsg) ZEND_PARSE_PARAMETERS_END(); array_init(return_value); @@ -794,27 +828,37 @@ PHP_FUNCTION(gdal_locationinfo) { zend_throw_exception(NULL, "Cannot invert geotransform",0); int rasterXSize = GDALGetRasterXSize(hSrcDS); int rasterYSize = GDALGetRasterYSize(hSrcDS); - - // !! must be casted - if((int)epsgIn > 1){ - OGRSpatialReferenceH hTrgSRS = OSRNewSpatialReference(GDALGetProjectionRef(hSrcDS)); - add_assoc_string(return_value, "rasterSRS", OSRGetName(hTrgSRS)); - // !! must be up of if - OGRSpatialReferenceH hSrcSRS = OSRNewSpatialReference(NULL); - OSRImportFromEPSG(hSrcSRS, (int)epsgIn); + OGRSpatialReferenceH *hTrgSRS = OSRNewSpatialReference(GDALGetProjectionRef(hSrcDS)); + char *hTrgSRSName = OSRGetName(hTrgSRS); + if(hTrgSRSName != NULL) + add_assoc_string(return_value, "rasterSRS", hTrgSRS); + // printf("epsgIn: %d\n",epsg); + if(epsg != 0){ + OGRSpatialReferenceH *hSrcSRS = OSRNewSpatialReference(NULL); + int *reterr = OSRImportFromEPSG(hSrcSRS, epsg); + if(reterr != OGRERR_NONE){ + char error[64]; + sprintf(error, "%s: Unknown espg '%u'", get_ogr_error_string(reterr),epsg); + zend_throw_exception(NULL, error,reterr); + return; + } OSRSetAxisMappingStrategy(hSrcSRS, OAMS_TRADITIONAL_GIS_ORDER); - OGRCoordinateTransformationH hCT = OCTNewCoordinateTransformation(hSrcSRS,hTrgSRS); + OGRCoordinateTransformationH *hCT = OCTNewCoordinateTransformation(hSrcSRS,hTrgSRS); double inX = lonX; double inY = latY; - add_assoc_double(return_value, "inEpsg", (int)epsgIn); + add_assoc_double(return_value, "inEpsg", epsg); add_assoc_double(return_value, "inX", inX); add_assoc_double(return_value, "inY", inY); OCTTransform(hCT,1,&lonX,&latY,NULL); - add_assoc_string(return_value, "inSRS", OSRGetName(hSrcSRS)); + char *hSrcSRSName = OSRGetName(hSrcSRS); + if(hSrcSRSName != NULL) + add_assoc_string(return_value, "inSRS", hSrcSRSName); OCTDestroyCoordinateTransformation(hCT); OSRDestroySpatialReference(hSrcSRS); - OSRDestroySpatialReference(hTrgSRS); } + OSRDestroySpatialReference(hTrgSRS); + add_assoc_double(return_value, "lonX", lonX); + add_assoc_double(return_value, "latY", latY); int iPixel = floor(adfInvGeoTransform[0] + adfInvGeoTransform[1] * lonX + @@ -822,11 +866,6 @@ PHP_FUNCTION(gdal_locationinfo) { int iLine = floor(adfInvGeoTransform[3] + adfInvGeoTransform[4] * lonX + adfInvGeoTransform[5] * latY); - if (iPixel < 0 || iLine < 0 || iPixel >= rasterXSize || iLine >= rasterYSize){ - char error[64]; - sprintf(error, "Pixel [%u,%u] is off this file [%ux%u]", iPixel, iLine, rasterXSize, rasterYSize); - zend_throw_exception(NULL, error,0); - } double adfPixel[2]; char iovalue[30]; if (GDALRasterIO(hBand, GF_Read, iPixel, iLine, 1, 1, adfPixel, 1, 1, GDT_CFloat64, 0, 0) == CE_None){ @@ -835,14 +874,11 @@ PHP_FUNCTION(gdal_locationinfo) { } else { sprintf(iovalue, "%.15g", adfPixel[0]); } - - } else { - zend_throw_exception(NULL, "Cannot get GDALRasterIO",0); } // LocationInfo for vrt char osItem[32]; sprintf(osItem,"Pixel_%d_%d", iPixel, iLine); - const char *pszLI = GDALGetMetadataItem(hBand, osItem, "LocationInfo"); + char *pszLI = GDALGetMetadataItem(hBand, osItem, "LocationInfo"); if(pszLI != NULL){ CPLXMLNode *psRoot = CPLParseXMLString(pszLI); if (psRoot != NULL && psRoot->psChild != NULL && @@ -861,19 +897,15 @@ PHP_FUNCTION(gdal_locationinfo) { } } } - add_assoc_string(return_value, "value", iovalue); - add_assoc_double(return_value, "lonX", lonX); - add_assoc_double(return_value, "latY", latY); add_assoc_double(return_value, "rasterPixel", iPixel); add_assoc_double(return_value, "rasterLine", iLine); - - char *projectionRef = GDALGetProjectionRef(hSrcDS); - if(*projectionRef) - add_assoc_string(return_value, "rasterProjectionRef", projectionRef); add_assoc_double(return_value, "rasterXSize", rasterXSize); add_assoc_double(return_value, "rasterYSize", rasterYSize); add_assoc_double(return_value, "rasterBandCount", GDALGetRasterCount(hSrcDS)); + char *projectionRef = GDALGetProjectionRef(hSrcDS); + if(*projectionRef) + add_assoc_string(return_value, "rasterProjectionRef", projectionRef); } /********************************************************************** diff --git a/php_ogr_api.h b/php_ogr_api.h index d9d628f..5e82c5f 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -1115,7 +1115,7 @@ _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_locationinfo, 0, 3, IS_ARR _ZEND_ARG_TYPE_INFO(0, zgdal, IS_RESOURCE, 1) _ZEND_ARG_TYPE_INFO(0, lonX, IS_DOUBLE, 0) _ZEND_ARG_TYPE_INFO(0, latY, IS_DOUBLE, 0) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, epsgIn, IS_LONG, 0, 0) + ZEND_ARG_TYPE_INFO(0, epsgIn, IS_LONG, 0) ZEND_END_ARG_INFO() static const zend_function_entry ogr_functions[] = { From 3d923046fb82891da64bd76e7fe652dea60c37af Mon Sep 17 00:00:00 2001 From: nono303 Date: Sat, 4 Feb 2023 17:44:01 +0100 Subject: [PATCH 06/20] fix gdal_free_Dataset & gdal_open_dataset pointer issue (had segfaults...) remove gdal_free (useless as gdal_free_Dataset now do the job) fix rasterSRS value --- ogr.c | 30 +++++++++--------------------- php_ogr_api.h | 7 +------ 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/ogr.c b/ogr.c index ea06093..03e9464 100755 --- a/ogr.c +++ b/ogr.c @@ -120,7 +120,7 @@ static int le_Feature; static void gdal_free_Dataset(zend_resource *resource) { - GDALDatasetH *hSrcDS = (GDALDatasetH*) resource; + GDALDatasetH hSrcDS = (GDALDatasetH)resource->ptr; if (hSrcDS != NULL) GDALClose(hSrcDS); } @@ -749,35 +749,23 @@ PHP_FUNCTION(cplgetlasterrormsg) * GDAL functions **********************************************************************/ - /** - * @param resource Proj - * @return void - */ -PHP_FUNCTION(gdal_free) { - GDALDatasetH *hSrcDS; - zval *zgdal; - - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_RESOURCE(zgdal) - ZEND_PARSE_PARAMETERS_END(); - - hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); - if (hSrcDS != NULL) - GDALClose(hSrcDS); -} - /** * * @param string gdal dataset filename * @return resource */ PHP_FUNCTION(gdal_open_dataset) { - GDALDatasetH *hSrcDS; + GDALDatasetH hSrcDS = NULL; zend_string *pszSrcFilename; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(pszSrcFilename) ZEND_PARSE_PARAMETERS_END(); + + if (GDALGetDriverCount() == 0) { + zend_throw_exception(NULL, "GDAL drivers not registered",0); + RETURN_NULL(); + } // | GDAL_OF_VERBOSE_ERROR hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER, NULL, NULL, NULL); if (hSrcDS != NULL){ @@ -831,7 +819,7 @@ PHP_FUNCTION(gdal_locationinfo) { OGRSpatialReferenceH *hTrgSRS = OSRNewSpatialReference(GDALGetProjectionRef(hSrcDS)); char *hTrgSRSName = OSRGetName(hTrgSRS); if(hTrgSRSName != NULL) - add_assoc_string(return_value, "rasterSRS", hTrgSRS); + add_assoc_string(return_value, "rasterSRS", hTrgSRSName); // printf("epsgIn: %d\n",epsg); if(epsg != 0){ OGRSpatialReferenceH *hSrcSRS = OSRNewSpatialReference(NULL); @@ -891,7 +879,7 @@ PHP_FUNCTION(gdal_locationinfo) { psNode->psChild != NULL) { char *pszUnescaped = CPLUnescapeString(psNode->psChild->pszValue, NULL, CPLES_XML); - add_assoc_string(return_value, "locationInfo", pszUnescaped); + add_assoc_string(return_value, "fileLocation", pszUnescaped); CPLFree(pszUnescaped); } } diff --git a/php_ogr_api.h b/php_ogr_api.h index 5e82c5f..8e97079 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -37,7 +37,6 @@ PHP_FUNCTION(cplgetlasterrortype); PHP_FUNCTION(cplgetlasterrormsg); PHP_FUNCTION(gdal_open_dataset); PHP_FUNCTION(gdal_locationinfo); -PHP_FUNCTION(gdal_free); PHP_FUNCTION(ogr_ds_destroy); PHP_FUNCTION(ogropen); PHP_FUNCTION(ogr_g_createfromwkb); @@ -1104,9 +1103,6 @@ ZEND_END_ARG_INFO() /* * Every user visible function must have an entry in ogr_functions[] */ -ZEND_BEGIN_ARG_INFO_EX(arginfo_gdal_free, 0, 0, 0) -ZEND_END_ARG_INFO() - _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_open_dataset, 0, 1, IS_RESOURCE, NULL, 0) _ZEND_ARG_TYPE_INFO(0, pszSrcFilename, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -1115,7 +1111,7 @@ _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_locationinfo, 0, 3, IS_ARR _ZEND_ARG_TYPE_INFO(0, zgdal, IS_RESOURCE, 1) _ZEND_ARG_TYPE_INFO(0, lonX, IS_DOUBLE, 0) _ZEND_ARG_TYPE_INFO(0, latY, IS_DOUBLE, 0) - ZEND_ARG_TYPE_INFO(0, epsgIn, IS_LONG, 0) + _ZEND_ARG_TYPE_INFO(0, epsgIn, IS_LONG, 0) ZEND_END_ARG_INFO() static const zend_function_entry ogr_functions[] = { @@ -1125,7 +1121,6 @@ static const zend_function_entry ogr_functions[] = { PHP_FE(cplgetlasterrormsg, arginfo_cplgetlasterrormsg) PHP_FE(gdal_open_dataset, arginfo_gdal_open_dataset) PHP_FE(gdal_locationinfo, arginfo_gdal_locationinfo) - PHP_FE(gdal_free, arginfo_gdal_free) PHP_FE(ogr_g_createfromwkb, arginfo_ogr_g_createfromwkb) PHP_FE(ogr_g_createfromwkt, arginfo_ogr_g_createfromwkt) PHP_FE(ogr_g_destroygeometry, arginfo_ogr_g_destroygeometry) From 9aa26d5c9791735f19b711ee4b870d64efe38489 Mon Sep 17 00:00:00 2001 From: nono303 Date: Sat, 4 Feb 2023 18:06:24 +0100 Subject: [PATCH 07/20] move GDALAllRegister() from PHP_MINIT_FUNCTION to PHP_FUNCTION(gdalregisterall) --- ogr.c | 20 +++++++++++++++----- php_ogr_api.h | 11 ++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/ogr.c b/ogr.c index 03e9464..77ec4fb 100755 --- a/ogr.c +++ b/ogr.c @@ -315,8 +315,6 @@ static void ogr_error_handler(CPLErr eclass, int err_no, const char *msg) */ PHP_MINIT_FUNCTION(ogr) { - // register GDAL Drivers - GDALAllRegister(); // Install custom error handler CPLSetErrorHandler(ogr_error_handler); /* If you have INI entries, uncomment these lines @@ -749,6 +747,18 @@ PHP_FUNCTION(cplgetlasterrormsg) * GDAL functions **********************************************************************/ + /* }}} */ + +/* {{{ proto void gdalregisterall() + */ +PHP_FUNCTION(gdalregisterall) +{ + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + GDALAllRegister(); +} + /** * * @param string gdal dataset filename @@ -763,8 +773,8 @@ PHP_FUNCTION(gdal_open_dataset) { ZEND_PARSE_PARAMETERS_END(); if (GDALGetDriverCount() == 0) { - zend_throw_exception(NULL, "GDAL drivers not registered",0); - RETURN_NULL(); + zend_throw_exception(NULL, "GDAL drivers not registered, make sure you call gdalregisterall() before gdalopen()",0); + RETURN_NULL(); } // | GDAL_OF_VERBOSE_ERROR hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER, NULL, NULL, NULL); @@ -4795,7 +4805,7 @@ PHP_FUNCTION(ogropen) if (OGRGetDriverCount() == 0) { php_error(E_WARNING, "OGR drivers not registered, make sure you call \ - OGRRegisterAll() before OGROpen()"); + ogrregisterall() before ogropen()"); RETURN_NULL(); } diff --git a/php_ogr_api.h b/php_ogr_api.h index 8e97079..d6c9943 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -35,6 +35,7 @@ PHP_FUNCTION(cplerrorreset); PHP_FUNCTION(cplgetlasterrorno); PHP_FUNCTION(cplgetlasterrortype); PHP_FUNCTION(cplgetlasterrormsg); +PHP_FUNCTION(gdalregisterall); PHP_FUNCTION(gdal_open_dataset); PHP_FUNCTION(gdal_locationinfo); PHP_FUNCTION(ogr_ds_destroy); @@ -1100,9 +1101,9 @@ _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_is_osr, 0, 1, _IS_BOOL, NULL, 0 ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() -/* - * Every user visible function must have an entry in ogr_functions[] - */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_gdalregisterall, 0, 0, 0) +ZEND_END_ARG_INFO() + _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_open_dataset, 0, 1, IS_RESOURCE, NULL, 0) _ZEND_ARG_TYPE_INFO(0, pszSrcFilename, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -1114,11 +1115,15 @@ _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_locationinfo, 0, 3, IS_ARR _ZEND_ARG_TYPE_INFO(0, epsgIn, IS_LONG, 0) ZEND_END_ARG_INFO() +/* + * Every user visible function must have an entry in ogr_functions[] + */ static const zend_function_entry ogr_functions[] = { PHP_FE(cplerrorreset, arginfo_cplerrorreset) PHP_FE(cplgetlasterrorno, arginfo_cplgetlasterrorno) PHP_FE(cplgetlasterrortype, arginfo_cplgetlasterrortype) PHP_FE(cplgetlasterrormsg, arginfo_cplgetlasterrormsg) + PHP_FE(gdalregisterall, arginfo_gdalregisterall) PHP_FE(gdal_open_dataset, arginfo_gdal_open_dataset) PHP_FE(gdal_locationinfo, arginfo_gdal_locationinfo) PHP_FE(ogr_g_createfromwkb, arginfo_ogr_g_createfromwkb) From c73df1c4635cd05fd33d57d409a15fd668bf1d19 Mon Sep 17 00:00:00 2001 From: nono303 Date: Sat, 4 Feb 2023 18:55:10 +0100 Subject: [PATCH 08/20] renaming gdal_open_dataset > gdalopen (according to ogropen) --- ogr.c | 2 +- php_ogr_api.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ogr.c b/ogr.c index 77ec4fb..9da22e3 100755 --- a/ogr.c +++ b/ogr.c @@ -764,7 +764,7 @@ PHP_FUNCTION(gdalregisterall) * @param string gdal dataset filename * @return resource */ -PHP_FUNCTION(gdal_open_dataset) { +PHP_FUNCTION(gdalopen) { GDALDatasetH hSrcDS = NULL; zend_string *pszSrcFilename; diff --git a/php_ogr_api.h b/php_ogr_api.h index d6c9943..b875cb5 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -36,7 +36,7 @@ PHP_FUNCTION(cplgetlasterrorno); PHP_FUNCTION(cplgetlasterrortype); PHP_FUNCTION(cplgetlasterrormsg); PHP_FUNCTION(gdalregisterall); -PHP_FUNCTION(gdal_open_dataset); +PHP_FUNCTION(gdalopen); PHP_FUNCTION(gdal_locationinfo); PHP_FUNCTION(ogr_ds_destroy); PHP_FUNCTION(ogropen); @@ -1104,7 +1104,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_gdalregisterall, 0, 0, 0) ZEND_END_ARG_INFO() -_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_open_dataset, 0, 1, IS_RESOURCE, NULL, 0) +_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdalopen, 0, 1, IS_RESOURCE, NULL, 0) _ZEND_ARG_TYPE_INFO(0, pszSrcFilename, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -1124,7 +1124,7 @@ static const zend_function_entry ogr_functions[] = { PHP_FE(cplgetlasterrortype, arginfo_cplgetlasterrortype) PHP_FE(cplgetlasterrormsg, arginfo_cplgetlasterrormsg) PHP_FE(gdalregisterall, arginfo_gdalregisterall) - PHP_FE(gdal_open_dataset, arginfo_gdal_open_dataset) + PHP_FE(gdalopen, arginfo_gdalopen) PHP_FE(gdal_locationinfo, arginfo_gdal_locationinfo) PHP_FE(ogr_g_createfromwkb, arginfo_ogr_g_createfromwkb) PHP_FE(ogr_g_createfromwkt, arginfo_ogr_g_createfromwkt) From b0a8d00f9cd0772c9de1a8e64331d55141d92a7e Mon Sep 17 00:00:00 2001 From: nono303 Date: Mon, 6 Feb 2023 09:57:33 +0100 Subject: [PATCH 09/20] improve return_value for 'in' & 'raster': - properties into specific array - add epsg code raster properties - add all (not just first) 'raster' file(s) with value into specific array - comment projectionRef --- ogr.c | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/ogr.c b/ogr.c index 9da22e3..9492b3c 100755 --- a/ogr.c +++ b/ogr.c @@ -772,7 +772,7 @@ PHP_FUNCTION(gdalopen) { Z_PARAM_STR(pszSrcFilename) ZEND_PARSE_PARAMETERS_END(); - if (GDALGetDriverCount() == 0) { + if (GDALGetDriverCount() == 0) { zend_throw_exception(NULL, "GDAL drivers not registered, make sure you call gdalregisterall() before gdalopen()",0); RETURN_NULL(); } @@ -811,6 +811,10 @@ PHP_FUNCTION(gdal_locationinfo) { ZEND_PARSE_PARAMETERS_END(); array_init(return_value); + zval return_in; + array_init(&return_in); + zval return_raster; + array_init(&return_raster); hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); if (hSrcDS == NULL) zend_throw_exception(NULL, "Gdal is null",0); @@ -827,9 +831,12 @@ PHP_FUNCTION(gdal_locationinfo) { int rasterXSize = GDALGetRasterXSize(hSrcDS); int rasterYSize = GDALGetRasterYSize(hSrcDS); OGRSpatialReferenceH *hTrgSRS = OSRNewSpatialReference(GDALGetProjectionRef(hSrcDS)); - char *hTrgSRSName = OSRGetName(hTrgSRS); + char *hTrgSRSName = OSRGetName(hTrgSRS); if(hTrgSRSName != NULL) - add_assoc_string(return_value, "rasterSRS", hTrgSRSName); + add_assoc_string(&return_raster, "srs", hTrgSRSName); + char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,"COMPD_CS"); + if(hTrgSRSCode != NULL) + add_assoc_string(&return_raster, "epsg", hTrgSRSCode); // printf("epsgIn: %d\n",epsg); if(epsg != 0){ OGRSpatialReferenceH *hSrcSRS = OSRNewSpatialReference(NULL); @@ -844,19 +851,19 @@ PHP_FUNCTION(gdal_locationinfo) { OGRCoordinateTransformationH *hCT = OCTNewCoordinateTransformation(hSrcSRS,hTrgSRS); double inX = lonX; double inY = latY; - add_assoc_double(return_value, "inEpsg", epsg); - add_assoc_double(return_value, "inX", inX); - add_assoc_double(return_value, "inY", inY); - OCTTransform(hCT,1,&lonX,&latY,NULL); char *hSrcSRSName = OSRGetName(hSrcSRS); if(hSrcSRSName != NULL) - add_assoc_string(return_value, "inSRS", hSrcSRSName); + add_assoc_string(&return_in, "srs", hSrcSRSName); + add_assoc_double(&return_in, "epsg", epsg); + add_assoc_double(&return_in, "x", inX); + add_assoc_double(&return_in, "y", inY); + OCTTransform(hCT,1,&lonX,&latY,NULL); OCTDestroyCoordinateTransformation(hCT); OSRDestroySpatialReference(hSrcSRS); } - OSRDestroySpatialReference(hTrgSRS); - add_assoc_double(return_value, "lonX", lonX); - add_assoc_double(return_value, "latY", latY); + OSRDestroySpatialReference(hTrgSRS); + add_assoc_double(&return_raster, "x", lonX); + add_assoc_double(&return_raster, "y", latY); int iPixel = floor(adfInvGeoTransform[0] + adfInvGeoTransform[1] * lonX + @@ -882,6 +889,8 @@ PHP_FUNCTION(gdal_locationinfo) { if (psRoot != NULL && psRoot->psChild != NULL && psRoot->eType == CXT_Element && EQUAL(psRoot->pszValue, "LocationInfo")) { + zval fileLocation; + array_init(&fileLocation); for (CPLXMLNode *psNode = psRoot->psChild; psNode != NULL; psNode = psNode->psNext) { if (psNode->eType == CXT_Element && @@ -889,21 +898,26 @@ PHP_FUNCTION(gdal_locationinfo) { psNode->psChild != NULL) { char *pszUnescaped = CPLUnescapeString(psNode->psChild->pszValue, NULL, CPLES_XML); - add_assoc_string(return_value, "fileLocation", pszUnescaped); + add_next_index_string(&fileLocation, pszUnescaped); CPLFree(pszUnescaped); } } + add_assoc_zval(&return_raster, "files", &fileLocation); } } - add_assoc_string(return_value, "value", iovalue); - add_assoc_double(return_value, "rasterPixel", iPixel); - add_assoc_double(return_value, "rasterLine", iLine); - add_assoc_double(return_value, "rasterXSize", rasterXSize); - add_assoc_double(return_value, "rasterYSize", rasterYSize); - add_assoc_double(return_value, "rasterBandCount", GDALGetRasterCount(hSrcDS)); - char *projectionRef = GDALGetProjectionRef(hSrcDS); + add_assoc_double(&return_raster, "pixel", iPixel); + add_assoc_double(&return_raster, "line", iLine); + add_assoc_double(&return_raster, "xSize", rasterXSize); + add_assoc_double(&return_raster, "ySize", rasterYSize); + add_assoc_double(&return_raster, "bandCount", GDALGetRasterCount(hSrcDS)); + /* + char *projectionRef = GDALGetProjectionRef(hSrcDS); if(*projectionRef) - add_assoc_string(return_value, "rasterProjectionRef", projectionRef); + add_assoc_string(&return_raster, "projectionRef", projectionRef); + */ + add_assoc_string(return_value, "value", iovalue); + add_assoc_zval(return_value, "in", &return_in); + add_assoc_zval(return_value, "raster", &return_raster); } /********************************************************************** From 96acf5bd577c579be25bef2563b414543e7ba30d Mon Sep 17 00:00:00 2001 From: nono303 Date: Mon, 6 Feb 2023 09:58:44 +0100 Subject: [PATCH 10/20] fix https://github.com/OSGeo/gdal/issues/7183 --- ogr.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ogr.c b/ogr.c index 9492b3c..b82731e 100755 --- a/ogr.c +++ b/ogr.c @@ -871,10 +871,14 @@ PHP_FUNCTION(gdal_locationinfo) { int iLine = floor(adfInvGeoTransform[3] + adfInvGeoTransform[4] * lonX + adfInvGeoTransform[5] * latY); - double adfPixel[2]; + double adfPixel[2] = {0, 0}; + const bool bIsComplex = GDALDataTypeIsComplex(GDALGetRasterDataType(hBand)); char iovalue[30]; - if (GDALRasterIO(hBand, GF_Read, iPixel, iLine, 1, 1, adfPixel, 1, 1, GDT_CFloat64, 0, 0) == CE_None){ - if (GDALDataTypeIsComplex(GDALGetRasterDataType(hBand))){ + if (GDALRasterIO(hBand, GF_Read, iPixel, iLine, 1, 1, + adfPixel, 1, 1, + bIsComplex ? GDT_CFloat64 : GDT_Float64, 0, + 0) == CE_None) { + if (bIsComplex){ sprintf(iovalue, "%.15g+%.15gi", adfPixel[0], adfPixel[1]); } else { sprintf(iovalue, "%.15g", adfPixel[0]); From 7d678ca55bb03a7281b02eab0b42b8e317ce507c Mon Sep 17 00:00:00 2001 From: nono303 Date: Mon, 6 Feb 2023 11:03:23 +0100 Subject: [PATCH 11/20] add scale/offset test to compute value --- ogr.c | 61 +++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/ogr.c b/ogr.c index b82731e..449a520 100755 --- a/ogr.c +++ b/ogr.c @@ -811,10 +811,10 @@ PHP_FUNCTION(gdal_locationinfo) { ZEND_PARSE_PARAMETERS_END(); array_init(return_value); - zval return_in; - array_init(&return_in); - zval return_raster; - array_init(&return_raster); + zval return_in; + array_init(&return_in); + zval return_raster; + array_init(&return_raster); hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); if (hSrcDS == NULL) zend_throw_exception(NULL, "Gdal is null",0); @@ -831,12 +831,12 @@ PHP_FUNCTION(gdal_locationinfo) { int rasterXSize = GDALGetRasterXSize(hSrcDS); int rasterYSize = GDALGetRasterYSize(hSrcDS); OGRSpatialReferenceH *hTrgSRS = OSRNewSpatialReference(GDALGetProjectionRef(hSrcDS)); - char *hTrgSRSName = OSRGetName(hTrgSRS); + char *hTrgSRSName = OSRGetName(hTrgSRS); if(hTrgSRSName != NULL) add_assoc_string(&return_raster, "srs", hTrgSRSName); - char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,"COMPD_CS"); + char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,"COMPD_CS"); if(hTrgSRSCode != NULL) - add_assoc_string(&return_raster, "epsg", hTrgSRSCode); + add_assoc_string(&return_raster, "epsg", hTrgSRSCode); // printf("epsgIn: %d\n",epsg); if(epsg != 0){ OGRSpatialReferenceH *hSrcSRS = OSRNewSpatialReference(NULL); @@ -861,7 +861,7 @@ PHP_FUNCTION(gdal_locationinfo) { OCTDestroyCoordinateTransformation(hCT); OSRDestroySpatialReference(hSrcSRS); } - OSRDestroySpatialReference(hTrgSRS); + OSRDestroySpatialReference(hTrgSRS); add_assoc_double(&return_raster, "x", lonX); add_assoc_double(&return_raster, "y", latY); @@ -873,15 +873,30 @@ PHP_FUNCTION(gdal_locationinfo) { adfInvGeoTransform[5] * latY); double adfPixel[2] = {0, 0}; const bool bIsComplex = GDALDataTypeIsComplex(GDALGetRasterDataType(hBand)); - char iovalue[30]; - if (GDALRasterIO(hBand, GF_Read, iPixel, iLine, 1, 1, - adfPixel, 1, 1, - bIsComplex ? GDT_CFloat64 : GDT_Float64, 0, - 0) == CE_None) { + char osValue[30]; + if (GDALRasterIO( + hBand, GF_Read, iPixel, iLine, 1, 1, + adfPixel, 1, 1, + bIsComplex ? GDT_CFloat64 : GDT_Float64, 0, 0 + ) == CE_None + ) { if (bIsComplex){ - sprintf(iovalue, "%.15g+%.15gi", adfPixel[0], adfPixel[1]); + sprintf(osValue, "%.15g+%.15gi", adfPixel[0], adfPixel[1]); } else { - sprintf(iovalue, "%.15g", adfPixel[0]); + sprintf(osValue, "%.15g", adfPixel[0]); + } + } + // have we scale/offset values. + int bSuccess; + double dfOffset = GDALGetRasterOffset(hBand, &bSuccess); + double dfScale = GDALGetRasterScale(hBand, &bSuccess); + if (dfOffset != 0.0 || dfScale != 1.0) { + adfPixel[0] = adfPixel[0] * dfScale + dfOffset; + if (bIsComplex) { + adfPixel[1] = adfPixel[1] * dfScale + dfOffset; + sprintf(osValue, "%.15g+%.15gi", adfPixel[0], adfPixel[1]); + } else { + sprintf(osValue, "%.15g", adfPixel[0]); } } // LocationInfo for vrt @@ -893,8 +908,8 @@ PHP_FUNCTION(gdal_locationinfo) { if (psRoot != NULL && psRoot->psChild != NULL && psRoot->eType == CXT_Element && EQUAL(psRoot->pszValue, "LocationInfo")) { - zval fileLocation; - array_init(&fileLocation); + zval fileLocation; + array_init(&fileLocation); for (CPLXMLNode *psNode = psRoot->psChild; psNode != NULL; psNode = psNode->psNext) { if (psNode->eType == CXT_Element && @@ -906,7 +921,7 @@ PHP_FUNCTION(gdal_locationinfo) { CPLFree(pszUnescaped); } } - add_assoc_zval(&return_raster, "files", &fileLocation); + add_assoc_zval(&return_raster, "files", &fileLocation); } } add_assoc_double(&return_raster, "pixel", iPixel); @@ -915,13 +930,13 @@ PHP_FUNCTION(gdal_locationinfo) { add_assoc_double(&return_raster, "ySize", rasterYSize); add_assoc_double(&return_raster, "bandCount", GDALGetRasterCount(hSrcDS)); /* - char *projectionRef = GDALGetProjectionRef(hSrcDS); + char *projectionRef = GDALGetProjectionRef(hSrcDS); if(*projectionRef) add_assoc_string(&return_raster, "projectionRef", projectionRef); - */ - add_assoc_string(return_value, "value", iovalue); - add_assoc_zval(return_value, "in", &return_in); - add_assoc_zval(return_value, "raster", &return_raster); + */ + add_assoc_string(return_value, "value", osValue); + add_assoc_zval(return_value, "in", &return_in); + add_assoc_zval(return_value, "raster", &return_raster); } /********************************************************************** From c1a06e4d816a3c3bf9c41666c1a4343c92b95b79 Mon Sep 17 00:00:00 2001 From: nono303 Date: Mon, 6 Feb 2023 12:09:07 +0100 Subject: [PATCH 12/20] move Error handling functions before PHP_MINIT_FUNCTION (required) --- ogr.c | 128 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/ogr.c b/ogr.c index 449a520..a6c4892 100755 --- a/ogr.c +++ b/ogr.c @@ -301,22 +301,82 @@ ogr_free_Feature(zend_resource_t *rsrc TSRMLS_DC) } -// https://github.com/shuhblam/simple-tiles/blob/master/src/error.c -static void ogr_error_handler(CPLErr eclass, int err_no, const char *msg) +/* }}} */ + +/********************************************************************** + * Error handling functions + **********************************************************************/ + +/* }}} */ +/* The previous line is meant for vim and emacs, so it can correctly fold and + unfold functions in source code. See the corresponding marks just before + function definition, where the functions purpose is also documented. Please + follow this convention for the convenience of others editing your code. +*/ + +// from https://github.com/shuhblam/simple-tiles/blob/master/src/error.c +static void handle_error_to_zend_exception(CPLErr eclass, int err_no, const char *msg) { // (void)eclass, (void)err_no, (void)msg; - zend_throw_exception(NULL, msg,err_no); + zend_throw_exception(NULL, msg, err_no); return; } +/* {{{ proto void cplerrorreset() + */ +PHP_FUNCTION(cplerrorreset) +{ + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + + CPLErrorReset(); +} + +/* }}} */ +/* {{{ proto int cplgetlasterrorno() + */ +PHP_FUNCTION(cplgetlasterrorno) +{ + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + + RETURN_LONG(CPLGetLastErrorNo()); +} + +/* }}} */ +/* {{{ proto CPLErr cplgetlasterrortype() + */ +PHP_FUNCTION(cplgetlasterrortype) +{ + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + RETURN_LONG(CPLGetLastErrorType()); +} + /* }}} */ +/* {{{ proto int cplgetlasterrormsg() + */ +PHP_FUNCTION(cplgetlasterrormsg) +{ + const char *pszMsg; + + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + + if ((pszMsg = CPLGetLastErrorMsg()) != NULL) + _RETURN_DUPLICATED_STRING((char *)pszMsg); +} /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(ogr) { // Install custom error handler - CPLSetErrorHandler(ogr_error_handler); + CPLSetErrorHandler(handle_error_to_zend_exception); /* If you have INI entries, uncomment these lines ZEND_INIT_MODULE_GLOBALS(ogr, php_ogr_init_globals, NULL); REGISTER_INI_ENTRIES(); @@ -683,66 +743,6 @@ static char* get_ogr_error_string(int ogrerrid) } } -/********************************************************************** - * Error handling functions - **********************************************************************/ - -/* }}} */ -/* The previous line is meant for vim and emacs, so it can correctly fold and - unfold functions in source code. See the corresponding marks just before - function definition, where the functions purpose is also documented. Please - follow this convention for the convenience of others editing your code. -*/ - -/* {{{ proto void cplerrorreset() - */ -PHP_FUNCTION(cplerrorreset) -{ - if (ZEND_NUM_ARGS() != 0) { - WRONG_PARAM_COUNT; - } - - CPLErrorReset(); -} - -/* }}} */ -/* {{{ proto int cplgetlasterrorno() - */ -PHP_FUNCTION(cplgetlasterrorno) -{ - if (ZEND_NUM_ARGS() != 0) { - WRONG_PARAM_COUNT; - } - - RETURN_LONG(CPLGetLastErrorNo()); -} - -/* }}} */ -/* {{{ proto CPLErr cplgetlasterrortype() - */ -PHP_FUNCTION(cplgetlasterrortype) -{ - if (ZEND_NUM_ARGS() != 0) { - WRONG_PARAM_COUNT; - } - RETURN_LONG(CPLGetLastErrorType()); -} - -/* }}} */ -/* {{{ proto int cplgetlasterrormsg() - */ -PHP_FUNCTION(cplgetlasterrormsg) -{ - const char *pszMsg; - - if (ZEND_NUM_ARGS() != 0) { - WRONG_PARAM_COUNT; - } - - if ((pszMsg = CPLGetLastErrorMsg()) != NULL) - _RETURN_DUPLICATED_STRING((char *)pszMsg); -} - /********************************************************************** * GDAL functions **********************************************************************/ From f5cbe2e8e1f1b7789bc61221e4bc0690b14fb665 Mon Sep 17 00:00:00 2001 From: nono303 Date: Tue, 7 Feb 2023 14:11:01 +0100 Subject: [PATCH 13/20] Improve performance by extraction coordinates transformation out of locationinfo: - add gdal_tr_create (return CoordinateTransformation resource) & gdal_ds_getsrscode (get dataset SRS code) - gdalopen: add optional passed by reference trgSRS parameter to retrieve dataset SRS code at Dataset creation - gdal_locationinfo: change int epsg additional parameter from to zhct CoordinateTransformation fix: - OSRGetAuthorityCode: COMPD_CS > NULL (first in root) - avoid useless resource creation: OSRNewSpatialReference(GDALGetProjectionRef(GDALDatasetH)) > GDALGetSpatialRef(GDALDatasetH) - add RETURN_NULL() after zend_throw_exception add correct proto to gdal* functions --- ogr.c | 208 ++++++++++++++++++++++++++++++++++---------------- php_ogr_api.h | 23 +++++- 2 files changed, 163 insertions(+), 68 deletions(-) diff --git a/ogr.c b/ogr.c index a6c4892..b2ae467 100755 --- a/ogr.c +++ b/ogr.c @@ -167,7 +167,7 @@ static void ogr_free_SpatialReference(zend_resource_t *rsrc TSRMLS_DC) { OGRSpatialReferenceH hSRS = (OGRSpatialReferenceH)rsrc->ptr; - /* Release rather than Destroy in case OGR still references the object */ + /* Release rather than Destroy (OSRDestroySpatialReference)in case OGR still references the object */ OSRRelease( hSRS ); } @@ -684,9 +684,9 @@ PHP_MINFO_FUNCTION(ogr) { php_info_print_table_start(); php_info_print_table_header(2, "ogr support", "enabled"); - php_info_print_table_row(2, "PHP_OGR Version", PHP_OGR_VERSION); - php_info_print_table_row(2, "GDAL/OGR Version", GDAL_RELEASE_NAME); - php_info_print_table_row(2, "GDAL Build Info", GDALVersionInfo("BUILD_INFO")); + php_info_print_table_row(2, "PHP_OGR Version", PHP_OGR_VERSION); + php_info_print_table_row(2, "GDAL/OGR Version", GDAL_RELEASE_NAME); + php_info_print_table_row(2, "GDAL Build Info", GDALVersionInfo("BUILD_INFO")); php_info_print_table_end(); /* Remove comments if you have entries in php.ini @@ -759,17 +759,19 @@ PHP_FUNCTION(gdalregisterall) GDALAllRegister(); } -/** - * - * @param string gdal dataset filename - * @return resource - */ -PHP_FUNCTION(gdalopen) { - GDALDatasetH hSrcDS = NULL; +/* }}} */ + +/* {{{ proto ressource gdalopen(string pszSrcFilename, int trgSRS) + */ +PHP_FUNCTION(gdalopen) +{ zend_string *pszSrcFilename; + zval *trgSRS = NULL; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_STR(pszSrcFilename) + Z_PARAM_OPTIONAL + Z_PARAM_ZVAL(trgSRS) ZEND_PARSE_PARAMETERS_END(); if (GDALGetDriverCount() == 0) { @@ -777,37 +779,110 @@ PHP_FUNCTION(gdalopen) { RETURN_NULL(); } // | GDAL_OF_VERBOSE_ERROR - hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER, NULL, NULL, NULL); + GDALDatasetH hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER, NULL, NULL, NULL); if (hSrcDS != NULL){ + if(ZEND_NUM_ARGS() == 2){ + OGRSpatialReferenceH *hTrgSRS = GDALGetSpatialRef(hSrcDS); + char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,NULL); + if(hTrgSRSCode != NULL){ + ZEND_TRY_ASSIGN_REF_STRING(trgSRS, hTrgSRSCode); + } else { + php_error_docref(NULL, E_WARNING, "'%s' don't have any SRS Authority Code", ZSTR_VAL(pszSrcFilename)); + } + } RETURN_RES(zend_register_resource(hSrcDS, le_Dataset)); } else { char error[128]; - sprintf(error, "Can not open dataset '%s'", ZSTR_VAL(pszSrcFilename)); + sprintf(error, "Can not open GDALDataset '%s'", ZSTR_VAL(pszSrcFilename)); zend_throw_exception(NULL, error,0); + RETURN_NULL(); } } -/** - * https://gis.stackexchange.com/a/393550 - * @param float lonX - * @param float latY - * @param string file - * @return array with dataset infos & required band value - */ -PHP_FUNCTION(gdal_locationinfo) { +/* }}} */ + +/* {{{ proto string gdal_ds_getsrscode(resource zgdal) + */ +PHP_FUNCTION(gdal_ds_getsrscode) +{ + zval *zgdal = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_RESOURCE(zgdal) + ZEND_PARSE_PARAMETERS_END(); + + GDALDatasetH *hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); + if (hSrcDS == NULL) { + zend_throw_exception(NULL, "Param zgdal is null",0); + RETURN_NULL(); + } + OGRSpatialReferenceH *hTrgSRS = GDALGetSpatialRef(hSrcDS); + char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,NULL); + if(hTrgSRSCode != NULL){ + _RETURN_DUPLICATED_STRING((char *) hTrgSRSCode); + } else { + zend_throw_exception(NULL, "GDALDataset have no SRS Code",0); + RETURN_NULL(); + } +} + +/* }}} */ + +/* {{{ proto ressource gdal_ds_getsrscode(int srcSRS, int trgSRS) + */ +PHP_FUNCTION(gdal_tr_create) +{ + int srcSRS, trgSRS = -1; + + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_LONG(srcSRS) + Z_PARAM_LONG(trgSRS) + ZEND_PARSE_PARAMETERS_END(); + + OGRSpatialReferenceH *hSrcSRS = OSRNewSpatialReference(NULL); + int *srcReterr = OSRImportFromEPSG(hSrcSRS, srcSRS); + if(srcReterr != OGRERR_NONE){ + OSRDestroySpatialReference(hSrcSRS); + char error[64]; + sprintf(error, "%s: Unknown srcSRS '%u'", get_ogr_error_string(srcReterr),srcSRS); + zend_throw_exception(NULL, error,srcReterr); + RETURN_NULL(); + } + OSRSetAxisMappingStrategy(hSrcSRS, OAMS_TRADITIONAL_GIS_ORDER); + // TODO necessary ? + zend_register_resource(hSrcSRS, le_SpatialReference); + + OGRSpatialReferenceH *hTrgSRS = OSRNewSpatialReference(NULL); + int *trgReterr = OSRImportFromEPSG(hTrgSRS, trgSRS); + if(trgReterr != OGRERR_NONE){ + OSRDestroySpatialReference(hTrgSRS); + char error[64]; + sprintf(error, "%s: Unknown trgSRS '%u'", get_ogr_error_string(trgReterr),trgSRS); + zend_throw_exception(NULL, error,trgReterr); + RETURN_NULL(); + } + // TODO necessary ? + zend_register_resource(hTrgSRS, le_SpatialReference); + + RETURN_RES(zend_register_resource(OCTNewCoordinateTransformation(hSrcSRS,hTrgSRS), le_CoordinateTransformation)); +} + +/* }}} */ + +/* {{{ proto array gdal_locationinfo(resource zgdal, double lonX, + double latY, resource zhct) + */ +PHP_FUNCTION(gdal_locationinfo) +{ double lonX, latY = 0; - int epsg = 0; - zend_string *pszSrcFilename; - GDALDatasetH *hSrcDS; - GDALRasterBandH *hBand; - zval *zgdal; + zval *zgdal, *zhct = NULL; ZEND_PARSE_PARAMETERS_START(3, 4) Z_PARAM_RESOURCE(zgdal) Z_PARAM_DOUBLE(lonX) Z_PARAM_DOUBLE(latY) Z_PARAM_OPTIONAL - Z_PARAM_LONG(epsg) + Z_PARAM_RESOURCE(zhct) ZEND_PARSE_PARAMETERS_END(); array_init(return_value); @@ -815,56 +890,60 @@ PHP_FUNCTION(gdal_locationinfo) { array_init(&return_in); zval return_raster; array_init(&return_raster); - hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); - if (hSrcDS == NULL) - zend_throw_exception(NULL, "Gdal is null",0); - // Assume there is only one band in the raster source and use that - hBand = GDALGetRasterBand(hSrcDS, 1); - if (hBand == NULL) - zend_throw_exception(NULL, "Gdal has no band",0); + GDALDatasetH *hSrcDS = (GDALDatasetH*) zend_fetch_resource_ex(zgdal, "GDALDataset", le_Dataset); + if (hSrcDS == NULL) { + zend_throw_exception(NULL, "Param zgdal is null",0); + RETURN_NULL(); + } + // TODO Assume there is only one band in the raster source and use that + GDALRasterBandH *hBand = GDALGetRasterBand(hSrcDS, 1); + if (hBand == NULL){ + zend_throw_exception(NULL, "GDALDataset have no band",0); + RETURN_NULL(); + } double adfGeoTransform[6]; - if (GDALGetGeoTransform(hSrcDS, adfGeoTransform) != CE_None) + if (GDALGetGeoTransform(hSrcDS, adfGeoTransform) != CE_None) { zend_throw_exception(NULL, "Cannot get geotransform",0); - double adfInvGeoTransform[6]; - if (!GDALInvGeoTransform(adfGeoTransform, adfInvGeoTransform)) + RETURN_NULL(); + } + double adfInvGeoTransform[6]; + if (!GDALInvGeoTransform(adfGeoTransform, adfInvGeoTransform)) { zend_throw_exception(NULL, "Cannot invert geotransform",0); - int rasterXSize = GDALGetRasterXSize(hSrcDS); - int rasterYSize = GDALGetRasterYSize(hSrcDS); - OGRSpatialReferenceH *hTrgSRS = OSRNewSpatialReference(GDALGetProjectionRef(hSrcDS)); + RETURN_NULL(); + } + double rasterXSize = GDALGetRasterXSize(hSrcDS); + double rasterYSize = GDALGetRasterYSize(hSrcDS); + + OGRSpatialReferenceH *hTrgSRS = GDALGetSpatialRef(hSrcDS); char *hTrgSRSName = OSRGetName(hTrgSRS); if(hTrgSRSName != NULL) add_assoc_string(&return_raster, "srs", hTrgSRSName); - char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,"COMPD_CS"); + char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,NULL); if(hTrgSRSCode != NULL) - add_assoc_string(&return_raster, "epsg", hTrgSRSCode); - // printf("epsgIn: %d\n",epsg); - if(epsg != 0){ - OGRSpatialReferenceH *hSrcSRS = OSRNewSpatialReference(NULL); - int *reterr = OSRImportFromEPSG(hSrcSRS, epsg); - if(reterr != OGRERR_NONE){ - char error[64]; - sprintf(error, "%s: Unknown espg '%u'", get_ogr_error_string(reterr),epsg); - zend_throw_exception(NULL, error,reterr); - return; + add_assoc_string(&return_raster, "epsg", hTrgSRSCode); + double inX = lonX; + double inY = latY; + if (zhct) { + int hct_id = -1; + OGRCoordinateTransformationH *hCT = (OGRCoordinateTransformationH*) zend_fetch_resource_ex(zhct, "OGRCoordinateTransformation", le_CoordinateTransformation); + if(hCT == NULL){ + zend_throw_exception(NULL, "Param zhct is null",0); + RETURN_NULL(); } - OSRSetAxisMappingStrategy(hSrcSRS, OAMS_TRADITIONAL_GIS_ORDER); - OGRCoordinateTransformationH *hCT = OCTNewCoordinateTransformation(hSrcSRS,hTrgSRS); - double inX = lonX; - double inY = latY; + + OGRSpatialReferenceH *hSrcSRS = OCTGetSourceCS(hCT); char *hSrcSRSName = OSRGetName(hSrcSRS); if(hSrcSRSName != NULL) add_assoc_string(&return_in, "srs", hSrcSRSName); - add_assoc_double(&return_in, "epsg", epsg); - add_assoc_double(&return_in, "x", inX); - add_assoc_double(&return_in, "y", inY); + char *hSrcSRSCode = OSRGetAuthorityCode(hSrcSRS,NULL); + if(hSrcSRSCode != NULL) + add_assoc_string(&return_in, "epsg", hSrcSRSCode); OCTTransform(hCT,1,&lonX,&latY,NULL); - OCTDestroyCoordinateTransformation(hCT); - OSRDestroySpatialReference(hSrcSRS); } - OSRDestroySpatialReference(hTrgSRS); + add_assoc_double(&return_in, "x", inX); + add_assoc_double(&return_in, "y", inY); add_assoc_double(&return_raster, "x", lonX); add_assoc_double(&return_raster, "y", latY); - int iPixel = floor(adfInvGeoTransform[0] + adfInvGeoTransform[1] * lonX + adfInvGeoTransform[2] * latY); @@ -939,11 +1018,12 @@ PHP_FUNCTION(gdal_locationinfo) { add_assoc_zval(return_value, "raster", &return_raster); } +/* }}} */ + /********************************************************************** * OGR functions **********************************************************************/ -/* }}} */ /* {{{ proto int ogr_g_createfromwkb(string strbydata, resource hsrs, resource refhnewgeom) */ diff --git a/php_ogr_api.h b/php_ogr_api.h index b875cb5..6f9154b 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -38,6 +38,8 @@ PHP_FUNCTION(cplgetlasterrormsg); PHP_FUNCTION(gdalregisterall); PHP_FUNCTION(gdalopen); PHP_FUNCTION(gdal_locationinfo); +PHP_FUNCTION(gdal_ds_getsrscode); +PHP_FUNCTION(gdal_tr_create); PHP_FUNCTION(ogr_ds_destroy); PHP_FUNCTION(ogropen); PHP_FUNCTION(ogr_g_createfromwkb); @@ -1101,18 +1103,29 @@ _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_is_osr, 0, 1, _IS_BOOL, NULL, 0 ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() +// ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, class_name, allow_null) ZEND_BEGIN_ARG_INFO_EX(arginfo_gdalregisterall, 0, 0, 0) ZEND_END_ARG_INFO() -_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdalopen, 0, 1, IS_RESOURCE, NULL, 0) +_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdalopen, 0, 1, IS_RESOURCE, NULL, 1) _ZEND_ARG_TYPE_INFO(0, pszSrcFilename, IS_STRING, 0) + _ZEND_ARG_TYPE_INFO(1, trgSRS, IS_RESOURCE, 1) ZEND_END_ARG_INFO() _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_locationinfo, 0, 3, IS_ARRAY, NULL, 1) _ZEND_ARG_TYPE_INFO(0, zgdal, IS_RESOURCE, 1) - _ZEND_ARG_TYPE_INFO(0, lonX, IS_DOUBLE, 0) - _ZEND_ARG_TYPE_INFO(0, latY, IS_DOUBLE, 0) - _ZEND_ARG_TYPE_INFO(0, epsgIn, IS_LONG, 0) + _ZEND_ARG_TYPE_INFO(0, lonX, IS_DOUBLE, 0) + _ZEND_ARG_TYPE_INFO(0, latY, IS_DOUBLE, 0) + _ZEND_ARG_TYPE_INFO(0, zhct, IS_RESOURCE, 1) +ZEND_END_ARG_INFO() + +_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_ds_getsrscode, 0, 1, IS_STRING, NULL, 1) + _ZEND_ARG_TYPE_INFO(0, zgdal, IS_RESOURCE, 1) +ZEND_END_ARG_INFO() + +_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_tr_create, 0, 2, IS_RESOURCE, NULL, 1) + _ZEND_ARG_TYPE_INFO(0, srcSRS, IS_LONG, 0) + _ZEND_ARG_TYPE_INFO(0, trgSRS, IS_LONG, 0) ZEND_END_ARG_INFO() /* @@ -1126,6 +1139,8 @@ static const zend_function_entry ogr_functions[] = { PHP_FE(gdalregisterall, arginfo_gdalregisterall) PHP_FE(gdalopen, arginfo_gdalopen) PHP_FE(gdal_locationinfo, arginfo_gdal_locationinfo) + PHP_FE(gdal_ds_getsrscode, arginfo_gdal_ds_getsrscode) + PHP_FE(gdal_tr_create, arginfo_gdal_tr_create) PHP_FE(ogr_g_createfromwkb, arginfo_ogr_g_createfromwkb) PHP_FE(ogr_g_createfromwkt, arginfo_ogr_g_createfromwkt) PHP_FE(ogr_g_destroygeometry, arginfo_ogr_g_destroygeometry) From c23f87a67899ca32e63cdeb3ac3e6f2bd233a51c Mon Sep 17 00:00:00 2001 From: nono303 Date: Sat, 18 Feb 2023 17:45:39 +0100 Subject: [PATCH 14/20] typo --- ogr.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ogr.c b/ogr.c index b2ae467..f751b96 100755 --- a/ogr.c +++ b/ogr.c @@ -747,15 +747,12 @@ static char* get_ogr_error_string(int ogrerrid) * GDAL functions **********************************************************************/ - /* }}} */ - /* {{{ proto void gdalregisterall() */ PHP_FUNCTION(gdalregisterall) { - if (ZEND_NUM_ARGS() != 0) { + if (ZEND_NUM_ARGS() != 0) WRONG_PARAM_COUNT; - } GDALAllRegister(); } From 82045baffe759825d975a05fb610c37a4cc34265 Mon Sep 17 00:00:00 2001 From: nono303 Date: Wed, 2 Aug 2023 06:57:12 +0200 Subject: [PATCH 15/20] fix proto name --- ogr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ogr.c b/ogr.c index f751b96..f566412 100755 --- a/ogr.c +++ b/ogr.c @@ -825,7 +825,7 @@ PHP_FUNCTION(gdal_ds_getsrscode) /* }}} */ -/* {{{ proto ressource gdal_ds_getsrscode(int srcSRS, int trgSRS) +/* {{{ proto ressource gdal_tr_create(int srcSRS, int trgSRS) */ PHP_FUNCTION(gdal_tr_create) { @@ -4425,7 +4425,7 @@ PHP_FUNCTION(ogr_l_rollbacktransaction) /* }}} */ -/* {{{ proto void OGR_DS_Destroy(resource hDS) +/* {{{ proto void ogr_ds_destroy(resource hDS) */ PHP_FUNCTION(ogr_ds_destroy) { @@ -4886,7 +4886,7 @@ PHP_FUNCTION(ogr_dr_createdatasource) /* }}} */ -/* {{{ proto resource OGROpen(string strName, boolean bUpdate, +/* {{{ proto resource ogropen(string strName, boolean bUpdate, resource refhSFDriver) */ PHP_FUNCTION(ogropen) @@ -5801,7 +5801,7 @@ PHP_FUNCTION(osr_issamegeogcs) /* }}} */ -/* {{{ proto boolean osr_issamegeogcs(reference hsrs1, reference hsrs2) +/* {{{ proto boolean osr_issame(reference hsrs1, reference hsrs2) */ PHP_FUNCTION(osr_issame) { @@ -6233,7 +6233,7 @@ PHP_FUNCTION(osr_getaxis) /* }}} */ -/* {{{ proto boolean osr_epsgtreatsaslatlong(reference hsrs) +/* {{{ proto boolean is_osr(reference hsrs) */ PHP_FUNCTION(is_osr) { From 99e249bb4689c11e7e17369284d5ace53f25446f Mon Sep 17 00:00:00 2001 From: nono303 Date: Wed, 2 Aug 2023 07:04:02 +0200 Subject: [PATCH 16/20] typo: tab2sapce --- ogr.c | 16 ++++++++-------- php_ogr.h | 10 +++++----- php_ogr_api.h | 6 +++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ogr.c b/ogr.c index f566412..44a1870 100755 --- a/ogr.c +++ b/ogr.c @@ -779,14 +779,14 @@ PHP_FUNCTION(gdalopen) GDALDatasetH hSrcDS = GDALOpenEx(ZSTR_VAL(pszSrcFilename), GDAL_OF_RASTER, NULL, NULL, NULL); if (hSrcDS != NULL){ if(ZEND_NUM_ARGS() == 2){ - OGRSpatialReferenceH *hTrgSRS = GDALGetSpatialRef(hSrcDS); - char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,NULL); - if(hTrgSRSCode != NULL){ - ZEND_TRY_ASSIGN_REF_STRING(trgSRS, hTrgSRSCode); - } else { - php_error_docref(NULL, E_WARNING, "'%s' don't have any SRS Authority Code", ZSTR_VAL(pszSrcFilename)); - } - } + OGRSpatialReferenceH *hTrgSRS = GDALGetSpatialRef(hSrcDS); + char *hTrgSRSCode = OSRGetAuthorityCode(hTrgSRS,NULL); + if(hTrgSRSCode != NULL){ + ZEND_TRY_ASSIGN_REF_STRING(trgSRS, hTrgSRSCode); + } else { + php_error_docref(NULL, E_WARNING, "'%s' don't have any SRS Authority Code", ZSTR_VAL(pszSrcFilename)); + } + } RETURN_RES(zend_register_resource(hSrcDS, le_Dataset)); } else { char error[128]; diff --git a/php_ogr.h b/php_ogr.h index 5f48cae..a38cf8a 100755 --- a/php_ogr.h +++ b/php_ogr.h @@ -55,12 +55,12 @@ PHP_RSHUTDOWN_FUNCTION(ogr); PHP_MINFO_FUNCTION(ogr); /* - Declare any global variables you may need between the BEGIN - and END macros here: + Declare any global variables you may need between the BEGIN + and END macros here: ZEND_BEGIN_MODULE_GLOBALS(ogr) - int global_value; - char *global_string; + int global_value; + char *global_string; ZEND_END_MODULE_GLOBALS(ogr) */ @@ -80,7 +80,7 @@ ZEND_END_MODULE_GLOBALS(ogr) #define OGR_G(v) (ogr_globals.v) #endif -#endif /* PHP_OGR_H */ +#endif /* PHP_OGR_H */ /* diff --git a/php_ogr_api.h b/php_ogr_api.h index 6f9154b..b805dff 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -1109,7 +1109,7 @@ ZEND_END_ARG_INFO() _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdalopen, 0, 1, IS_RESOURCE, NULL, 1) _ZEND_ARG_TYPE_INFO(0, pszSrcFilename, IS_STRING, 0) - _ZEND_ARG_TYPE_INFO(1, trgSRS, IS_RESOURCE, 1) + _ZEND_ARG_TYPE_INFO(1, trgSRS, IS_RESOURCE, 1) ZEND_END_ARG_INFO() _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gdal_locationinfo, 0, 3, IS_ARRAY, NULL, 1) @@ -1139,8 +1139,8 @@ static const zend_function_entry ogr_functions[] = { PHP_FE(gdalregisterall, arginfo_gdalregisterall) PHP_FE(gdalopen, arginfo_gdalopen) PHP_FE(gdal_locationinfo, arginfo_gdal_locationinfo) - PHP_FE(gdal_ds_getsrscode, arginfo_gdal_ds_getsrscode) - PHP_FE(gdal_tr_create, arginfo_gdal_tr_create) + PHP_FE(gdal_ds_getsrscode, arginfo_gdal_ds_getsrscode) + PHP_FE(gdal_tr_create, arginfo_gdal_tr_create) PHP_FE(ogr_g_createfromwkb, arginfo_ogr_g_createfromwkb) PHP_FE(ogr_g_createfromwkt, arginfo_ogr_g_createfromwkt) PHP_FE(ogr_g_destroygeometry, arginfo_ogr_g_destroygeometry) From 46546c4e40b0555e8efa0bd69d9b6cfd82e05da0 Mon Sep 17 00:00:00 2001 From: nono303 Date: Wed, 2 Aug 2023 07:42:06 +0200 Subject: [PATCH 17/20] typo --- config.w32 | 4 +--- ogr.c | 8 ++++---- php_ogr_api.h | 1 - 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/config.w32 b/config.w32 index 691e050..f1b0fd3 100644 --- a/config.w32 +++ b/config.w32 @@ -1,5 +1,3 @@ -// vim:ft=javascript - ARG_WITH("ogr", "OGR support", "no"); if (PHP_OGR != "no") { if( @@ -17,4 +15,4 @@ if (PHP_OGR != "no") { } else { WARNING("ogr support can't be enabled: libraries or headers are missing") } -} \ No newline at end of file +} diff --git a/ogr.c b/ogr.c index 44a1870..7fc9317 100755 --- a/ogr.c +++ b/ogr.c @@ -167,7 +167,7 @@ static void ogr_free_SpatialReference(zend_resource_t *rsrc TSRMLS_DC) { OGRSpatialReferenceH hSRS = (OGRSpatialReferenceH)rsrc->ptr; - /* Release rather than Destroy (OSRDestroySpatialReference)in case OGR still references the object */ + /* Release rather than Destroy in case OGR still references the object */ OSRRelease( hSRS ); } @@ -684,9 +684,9 @@ PHP_MINFO_FUNCTION(ogr) { php_info_print_table_start(); php_info_print_table_header(2, "ogr support", "enabled"); - php_info_print_table_row(2, "PHP_OGR Version", PHP_OGR_VERSION); - php_info_print_table_row(2, "GDAL/OGR Version", GDAL_RELEASE_NAME); - php_info_print_table_row(2, "GDAL Build Info", GDALVersionInfo("BUILD_INFO")); + php_info_print_table_row(2, "PHP_OGR Version", PHP_OGR_VERSION); + php_info_print_table_row(2, "GDAL/OGR Version", GDAL_RELEASE_NAME); + php_info_print_table_row(2, "GDAL Build Info", GDALVersionInfo("BUILD_INFO")); php_info_print_table_end(); /* Remove comments if you have entries in php.ini diff --git a/php_ogr_api.h b/php_ogr_api.h index b805dff..38df521 100755 --- a/php_ogr_api.h +++ b/php_ogr_api.h @@ -1103,7 +1103,6 @@ _ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_is_osr, 0, 1, _IS_BOOL, NULL, 0 ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() -// ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, class_name, allow_null) ZEND_BEGIN_ARG_INFO_EX(arginfo_gdalregisterall, 0, 0, 0) ZEND_END_ARG_INFO() From 8241950bd71f617aacfcba47685717836c0aa1de Mon Sep 17 00:00:00 2001 From: nono303 Date: Wed, 2 Aug 2023 07:42:16 +0200 Subject: [PATCH 18/20] 1.7.0 --- php_ogr.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php_ogr.h b/php_ogr.h index a38cf8a..919a48c 100755 --- a/php_ogr.h +++ b/php_ogr.h @@ -30,9 +30,9 @@ #ifndef PHP_OGR_H -#define PHP_OGR_H 1 +#define PHP_OGR_H -#define PHP_OGR_VERSION "1.6.1" +#define PHP_OGR_VERSION "1.7.0" #define PHP_OGR_EXTNAME "ogr" extern zend_module_entry ogr_module_entry; From 91dd6ccd8b7274689ab18de3be690af2da9092c1 Mon Sep 17 00:00:00 2001 From: nono303 Date: Tue, 5 Dec 2023 10:30:04 +0100 Subject: [PATCH 19/20] comment with committed changes on https://github.com/OSGeo/gdal/blob/master/apps/gdallocationinfo.cpp --- ogr.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ogr.c b/ogr.c index 7fc9317..e362cbd 100755 --- a/ogr.c +++ b/ogr.c @@ -871,6 +871,10 @@ PHP_FUNCTION(gdal_tr_create) */ PHP_FUNCTION(gdal_locationinfo) { + /* + 2023-09-24 + https://github.com/OSGeo/gdal/commit/bb49e4140d83d15bba95f4265b4df58411adab96#diff-c1802686f4c5e9a80680f5c5ec2576ef19e1c223319bd69a48a7e10d95efeac2 + */ double lonX, latY = 0; zval *zgdal, *zhct = NULL; @@ -947,6 +951,11 @@ PHP_FUNCTION(gdal_locationinfo) int iLine = floor(adfInvGeoTransform[3] + adfInvGeoTransform[4] * lonX + adfInvGeoTransform[5] * latY); + /* + 2023-02-05 + https://github.com/OSGeo/gdal/commit/d2af066f908b782dd2a8437482da2b395241d3b9 + https://github.com/OSGeo/gdal/issues/7183 + */ double adfPixel[2] = {0, 0}; const bool bIsComplex = GDALDataTypeIsComplex(GDALGetRasterDataType(hBand)); char osValue[30]; From 89b3ebaa0ac8ce399756d850512d424b2d9d6efd Mon Sep 17 00:00:00 2001 From: nono303 Date: Mon, 5 May 2025 17:38:50 +0200 Subject: [PATCH 20/20] +libgdal.lib --- config.w32 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.w32 b/config.w32 index f1b0fd3..758f095 100644 --- a/config.w32 +++ b/config.w32 @@ -1,7 +1,7 @@ ARG_WITH("ogr", "OGR support", "no"); if (PHP_OGR != "no") { if( - CHECK_LIB("gdal.lib;gdal_i.lib", "ogr", PHP_OGR) && + CHECK_LIB("gdal.lib;gdal_i.lib;libgdal.lib", "ogr", PHP_OGR) && CHECK_HEADER_ADD_INCLUDE("ogr_srs_api.h", "CFLAGS_OGR", PHP_OGR + "\\include") && CHECK_HEADER_ADD_INCLUDE("gdal.h", "CFLAGS_OGR", PHP_OGR + "\\include") ) {