AI prompts
base on Advanced geometry operations in PHP [![Build Status](https://travis-ci.org/phayes/geoPHP.svg?branch=master)](https://travis-ci.org/phayes/geoPHP)
[geophp.net](https://geophp.net "GeoPHP homepage")
GeoPHP is a 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.
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.
This project is currently looking for co-maintainers. If you think you can help out, please send me a
message. Forks are also welcome, please issue pull requests and I will merge them into the main branch.
Getting Started
-----------------------
* The lastest stable version can always be downloaded at: <https://phayes.github.io/bin/current/geoPHP/geoPHP.tar.gz>
* Read the API Reference at: <https://geophp.net/api.html>
* 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
<?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;
```
=======
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
<?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')");
}
```
Credit
-------------------------------------------------
Maintainer: 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.
", Assign "at most 3 tags" to the expected json: {"id":"3223","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"