简体   繁体   English

运行示例转换时出现 PHPCoord TypeError

[英]PHPCoord TypeError when running example conversions

I'm battling on trying to use PHPCoord to convert different types of coordinates.我正在努力尝试使用PHPCoord来转换不同类型的坐标。 I am new to composer and PHP libraries so could well be making basic mistakes.我是 composer 和 PHP 库的新手,所以很可能会犯基本错误。 I had issues related to Autoloading which I've now resolved but I'm now facing errors of a different kind which could well be basic and due to my inexperience in this area.我遇到了与自动加载相关的问题,我现在已经解决了,但我现在面临着另一种错误,这很可能是基本的,并且由于我在这方面缺乏经验。

I'm using the following example code taken direct from thePHPCoord docs :我正在使用直接取自PHPCoord 文档的以下示例代码:

use PHPCoord\CoordinateReferenceSystem\Geographic2D;
use PHPCoord\CoordinateReferenceSystem\Projected;
use PHPCoord\GeographicPoint;
use PHPCoord\UnitOfMeasure\Angle\Degree;

// Converting from NAD83 to Florida State Plane
$from = GeographicPoint::create(
    Geographic2D::fromSRID(Geographic2D::EPSG_NAD83),
    new Degree(28.46),
    new Degree(-80.53),
    null
);
$toCRS = Projected::fromSRID(Projected::EPSG_NAD83_FLORIDA_EAST);
$to = $from->convert($toCRS); // $to instanceof ProjectedPoint

Above this I'm autoloading with:在此之上,我正在自动加载:

$theme_root = dirname(__DIR__, 1);
require_once $theme_root . '/vendor/autoload.php';

I'm getting the following errors:我收到以下错误:

Fatal error: Uncaught TypeError: Argument 1 passed to PHPCoord\GeographicPoint::create() must be an instance of PHPCoord\UnitOfMeasure\Angle\Angle, instance of PHPCoord\CoordinateReferenceSystem\Geographic2D given...致命错误:未捕获的类型错误:传递给 PHPCoord\GeographicPoint::create() 的参数 1 必须是 PHPCoord\UnitOfMeasure\Angle\Angle 的实例,给定的 PHPCoord\CoordinateReferenceSystem\Geographic2D 实例...

As the example code relates to North American geography I also added the optional add-on datapack: datapack-northamerica , though this had no effect.由于示例代码与北美地理相关,我还添加了可选的附加数据包: datapack-northamerica ,尽管这没有效果。

Any help would be much appreciated.任何帮助将非常感激。 Thank you!谢谢!

Answering this myself in the hope that anyone else struggling with PHPCoord might find guidance.我自己回答这个问题,希望其他任何在PHPCoord上苦苦挣扎的人都能找到指导。

As kindly pointed out by @Foobar I was using an older version of the library despite installing it recently using composer .正如@Foobar 所指出的那样,尽管最近使用composer安装了它,但我使用的是旧版本的库。 This was because composer had detected my version of PHP (7.4.12) and decided it didn't meet the criteria for the latest version of PHPCoord.这是因为 composer 检测到我的 PHP (7.4.12) 版本并确定它不符合最新版本的 PHPCoord 的标准。 I assumed I had the latest version and was trying to use code examples suitable only for PHPCoord version 5 and beyond but I was running version 4.x.我假设我有最新版本,并尝试使用仅适用于 PHPCoord 版本 5 及更高版本的代码示例,但我运行的是版本 4.x。

It's all slightly confusing as the documentation states:正如文档所述,这一切都有些令人困惑:

PHPCoord v4 is compatible with PHP 7.3+ PHPCoord v4 兼容 PHP 7.3+

However, the code examples are for PHPCoord v5 which needs PHP 8.0+.但是,代码示例适用于需要 PHP 8.0+ 的 PHPCoord v5。

My solution was to upgrade my PHP version to 8.x.我的解决方案是将我的 PHP 版本升级到 8.x。

This removed the error outlined above, however I then got a new insufficient memory error which was rectified by upping my MAMP memory limit from 128M to 256M.这消除了上面概述的错误,但是我随后收到了一个新的memory 不足错误,该错误已通过将我的 MAMP memory 限制从 128M 提高到 256M 得到纠正。

So to re-cap, my first issue was not including require_once 'vendor/autoload.php';回顾一下,我的第一个问题不包括require_once 'vendor/autoload.php';

I then had minimum requirement issues due to running PHP 7.4.x and not version 8.x and was unknowingly running an outdated version of the library.然后,由于运行 PHP 7.4.x 而不是 8.x 版本,我遇到了最低要求问题,并且在不知不觉中运行了库的过时版本。

I then had insufficient memory allocation issues which I resolved by editing php.ini changing memory_limit = 128M to memory_limit = 256M .然后我没有足够的 memory 分配问题,我通过编辑php.inimemory_limit = 128M更改为memory_limit = 256M解决了这些问题。

Thanks for all the comments and help.感谢所有评论和帮助。 I can now move on to converting OSGB references to Lat/Lon but will need to ensure the production host is running PHP version 8 and has a sufficient memory limit.我现在可以继续将 OSGB 引用转换为纬度/经度,但需要确保生产主机运行 PHP 版本 8 并且具有足够的 memory 限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM