swen100/geophp
Composer 安装命令:
composer require swen100/geophp
包简介
GeoPHP is an open-source native PHP library for doing geometry operations. It is written entirely in PHP and can therefore run on shared hosts. It can read and write a wide variety of formats: (E)WKT, (E)WKB, GeoJSON, KML, GPX, GeoRSS). It works with all Simple-Feature geometries (Point, LineString,
README 文档
README
GeoPHP is an open-source native PHP library for doing geometry operations. It is written entirely in PHP and can therefore run on shared hosts. It can read and write a wide variety of formats: WKT (including EWKT), WKB (including EWKB), GeoJSON, KML, GPX, and GeoRSS. It works with all Simple-Feature geometries (Point, LineString, Polygon, GeometryCollection etc.) and can be used to get centroids, bounding-boxes, area, and a wide variety of other useful information.
This is a fork of the famous geoPHP library by Patrick Hayes.
geoPHP also helpfully wraps the GEOS php extension so that applications can get a transparent performance increase when GEOS is installed on the server. When GEOS is installed, geoPHP also becomes fully compliant with the OpenGIS® Implementation Standard for Geographic information. With GEOS you get the full-set of openGIS functions in PHP like Union, IsWithin, Touches etc. This means that applications get a useful "core-set" of geometry operations that work in all environments, and an "extended-set"of operations for environments that have GEOS installed.
See the getting started section below for references and examples of everything that geoPHP can do.
Forks and contributions are welcome. Please open issues, send pull requests and I will merge them into the main branch.
Getting Started
- Examples
- Using geoPHP as a GIS format converter: http://github.com/phayes/geoPHP/wiki/Example-format-converter
- Other Interesting Links:
- Learn about GEOS integration at: https://geophp.net/geos.html
Example usage
<?php use \geoPHP\geoPHP; // 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;
More Examples
The Well Known Text (WKT) and Well Known Binary (WKB) support is ideal for integrating with MySQL's or PostGIS's spatial capability.
Once you have SELECTed your data with 'AsText('geo_field')' or 'AsBinary('geo_field')', you can put it straight into
geoPHP (can be wkt or wkb, but must be the same as how you extracted it from your database):
$geom = geoPHP::load($dbRow,'wkt');
You can collect multiple geometries into one (note that you must use wkt for this):
$geom = geoPHP::load("GEOMETRYCOLLECTION(".$dbString1.",".$dbString2.")",'wkt');
Calling get components returns the sub-geometries within a geometry as an array.
$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'
An alternative is to use the asArray() method. Using the above geometry collection of two linestrings,
$geometryArray = $geom2->asArray();
echo $geometryArray[0][0][0] . ", " . $geometryArray[0][0][1]; //outputs '1, 1'
Clearly, more complex analysis is possible.
echo $geom2->envelope()->area();
Working with PostGIS
geoPHP, through it's EWKB adapter, has good integration with postGIS. Here's an example of reading and writing postGIS geometries
<?php use \geoPHP\geoPHP; $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'); // Now we have a full geoPHP geometry object // Let's insert it back into the database $wkbGeometryString = pg_escape_bytea($geom->out('ewkb')); pg_query($connection, "INSERT INTO $table ($column) values (GeomFromWKB('$wkbGeometryString'))"); } // Using a direct SELECT and INSERT 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'); // Now we have a geoPHP geometry // To insert it directly into postGIS we need to unpack the WKB $unpackedGeomStr = unpack('H*', $geom->out('ewkb')); $geometryString = $unpackedGeomStr[1]; pg_query($connection, "INSERT INTO $table ($column) values ('$geometryString')"); }
Documentation
In progress… You can read the doc for original phayes/geoPHP at geophp.net
Credit
- Maintainer: Péter Báthory, Swen Zanon
- Original author: Patrick Hayes
Additional Contributors:
- GeoMemes Research (http://www.geomemes.com)
- HighWire Press (http://www.highwire.org) and GeoScienceWorld (http://www.geoscienceworld.org)
- Arnaud Renevier (gisconverter.php) https://github.com/arenevier/gisconverter.php
- Dave Tarc https://github.com/dtarc
- Elliott Hunston (documentation) https://github.com/ejh
This library is open-source and dual-licensed under both the modified BSD License and GPLv2. Either license may be used at your option.
swen100/geophp 适用场景与选型建议
swen100/geophp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 93.17k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2018 年 02 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「geometry」 「converter」 「gpx」 「gis」 「geojson」 「Polygon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 swen100/geophp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 swen100/geophp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 swen100/geophp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
RSS and Atom feed generator by Kai Blankenhorn
Tools to convert between .NET and PHP date formats
It parses .gpx file to JSON with distance and timestamp
Encode integer IDs using a preset or customized symbol set
PHP Library for parsing running GPS activities and calculating metrics
Geo-related tools PHP 7.3+ library
统计信息
- 总下载量: 93.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2018-02-13