phayes / geophp
GeoPHP是一个开源的本地PHP库,用于执行几何操作。它完全用PHP编写,因此可以在共享主机上运行。它可以读取和写入多种格式:WKT(包括EWKT)、WKB(包括EWKB)、GeoJSON、KML、GPX、GeoRSS)。它与所有Simple-Feature地理数据(点、线字符串、多边形、几何集合等)兼容。
Requires (Dev)
- phpunit/phpunit: 4.1.*
This package is not auto-updated.
Last update: 2024-09-15 00:25:29 UTC
README
GeoPHP是一个开源的本地PHP库,用于执行几何操作。它完全用PHP编写,因此可以在共享主机上运行。它可以读取和写入多种格式:WKT(包括EWKT)、WKB(包括EWKB)、GeoJSON、KML、GPX和GeoRSS。它与所有Simple-Feature几何形状(点、线字符串、多边形、几何集合等)兼容,并可用来获取质心、边界框、面积和其他大量有用的信息。
geoPHP还方便地包装了GEOS php扩展,使得当服务器上安装了GEOS时,应用程序可以透明地提高性能。当安装了GEOS时,geoPHP也完全符合OpenGIS®地理信息实施标准。有了GEOS,您可以在PHP中获得完整的OpenGIS函数集,如并集、IsWithin、Touch等。这意味着应用程序获得了一组有用的“核心”几何操作,这些操作在所有环境中都有效,并为已安装GEOS的环境提供了一组“扩展”操作。
请参阅下面的“入门”部分,了解geoPHP能做什么的所有参考和示例。
此项目目前正在寻找共同维护者。如果您认为您可以帮助,请给我发消息。分支也欢迎,请发起拉取请求,我会将其合并到主分支。
入门
- 最新稳定版本始终可以在以下位置下载: https://phayes.github.io/bin/current/geoPHP/geoPHP.tar.gz
- 阅读API参考: https://geophp.net/api.html
- 示例
- 使用geoPHP作为GIS格式转换器: http://github.com/phayes/geoPHP/wiki/Example-format-converter
- 其他有趣链接
- 了解GEOS集成: https://geophp.net/geos.html
示例用法
<?php include_once('geoPHP.inc'); // Polygon WKT example $polygon = geoPHP::load('POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))','wkt'); $area = $polygon->getArea(); $centroid = $polygon->getCentroid(); $centX = $centroid->getX(); $centY = $centroid->getY(); print "This polygon has an area of ".$area." and a centroid with X=".$centX." and Y=".$centY; // MultiPoint json example print "<br/>"; $json = '{ "type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }'; $multipoint = geoPHP::load($json, 'json'); $multipoint_points = $multipoint->getComponents(); $first_wkt = $multipoint_points[0]->out('wkt'); print "This multipoint has ".$multipoint->numGeometries()." points. The first point has a wkt representation of ".$first_wkt;
=======
更多示例
Well Known Text (WKT) 和 Well Known Binary (WKB) 的支持非常适合与MySQL或PostGIS的空间功能集成。一旦您使用 'AsText('geo_field')'
或 'AsBinary('geo_field')'
选择了数据,您就可以将其直接放入geoPHP中(可以是wkt或wkb,但必须与您从数据库中提取的方式相同)
$geom = geoPHP::load($dbRow,'wkt');
您可以将多个几何形状收集到一个中(注意您必须使用wkt来做这件事)
$geom = geoPHP::load("GEOMETRYCOLLECTION(".$dbString1.",".$dbString2.")",'wkt');
调用get components返回几何形状内的子几何形状数组。
$geom2 = geoPHP::load("GEOMETRYCOLLECTION(LINESTRING(1 1,5 1,5 5,1 5,1 1),LINESTRING(2 2,2 3,3 3,3 2,2 2))");
$geomComponents = $geom2->getComponents(); //an array of the two linestring geometries
$linestring1 = $geomComponents[0]->getComponents(); //an array of the first linestring's point geometries
$linestring2 = $geomComponents[1]->getComponents();
echo $linestring1[0]->x() . ", " . $linestring1[0]->y(); //outputs '1, 1'
另一种选择是使用 asArray()
方法。使用上面的由两个线字符串组成的几何集合,
$geometryArray = $geom2->asArray();
echo $geometryArray[0][0][0] . ", " . $geometryArray[0][0][1]; //outputs '1, 1'
显然,可以进行更复杂的分析。
echo $geom2->envelope()->area();
与PostGIS一起工作
geoPHP通过其EWKB适配器与PostGIS有良好的集成。以下是一个读取和写入PostGIS几何形状的示例
<?php include_once('geoPHP.inc'); $host = 'localhost'; $database = 'phayes'; $table = 'test'; $column = 'geom'; $user = 'phayes'; $pass = 'supersecret'; $connection = pg_connect("host=$host dbname=$database user=$user password=$pass"); // Working with PostGIS and Extended-WKB // ---------------------------- // Using asBinary and GeomFromWKB in PostGIS $result = pg_fetch_all(pg_query($connection, "SELECT asBinary($column) as geom FROM $table")); foreach ($result as $item) { $wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob $geom = geoPHP::load($wkb, 'ewkb'); // We now a full geoPHP Geometry object // Let's insert it back into the database $insert_string = pg_escape_bytea($geom->out('ewkb')); pg_query($connection, "INSERT INTO $table ($column) values (GeomFromWKB('$insert_string'))"); } // Using a direct SELECT and INSERTs in PostGIS without using wrapping functions $result = pg_fetch_all(pg_query($connection, "SELECT $column as geom FROM $table")); foreach ($result as $item) { $wkb = pack('H*',$item['geom']); // Unpacking the hex blob $geom = geoPHP::load($wkb, 'ewkb'); // We now have a geoPHP Geometry // To insert directly into postGIS we need to unpack the WKB $unpacked = unpack('H*', $geom->out('ewkb')); $insert_string = $unpacked[1]; pg_query($connection, "INSERT INTO $table ($column) values ('$insert_string')"); }
致谢
维护者:Patrick Hayes
其他贡献者
- GeoMemes 研究 (http://www.geomemes.com)
- HighWire Press (http://www.highwire.org) 和 GeoScienceWorld (http://www.geoscienceworld.org)
- Arnaud Renevier (gisconverter.php) https://github.com/arenevier/gisconverter.php
- Dave Tarc https://github.com/dtarc
- Elliott Hunston (文档) https://github.com/ejh
该库是开源的,并且双重许可,同时遵循修改后的BSD许可证和GPLv2。您可以根据自己的选择使用任一许可证。