Giter Club home page Giter Club logo

geophp's Introduction

Build Status

geophp.net

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

Example usage

<?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
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:

This library is open-source and dual-licensed under both the Modified BSD License and GPLv2. Either license may be used at your option.

geophp's People

Contributors

andrewkett avatar andrewtch avatar danoh avatar dasjo avatar dbrkv avatar drupol avatar dstelljes avatar dtarc avatar ejh avatar fanquake avatar gregseb avatar lolandese avatar marcjansen avatar mishal avatar mprins avatar phayes avatar ramunasd avatar theodoreb avatar tillz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

geophp's Issues

union() not working even for example

Trying to use the union() to combine two polygons. First by following the example:

$poly1 = geoPHP::load('POLYGON((30 10,10 20,20 40,40 40,30 10))','wkt');
$poly2 = geoPHP::load('POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30, 35 35, 30 20, 20 30))','wkt');
$combined_poly = $poly1->union($poly2);

Then the $combined_poly is null after the last line is executed. Please note I take the dollar sign away from geoPHP::load() because the it complained "Class name must be a valid object or a string".

Multiple geohashes decode to same lat/lon

hi,

i'm having a problem with decoding geohashes:

  • 'u26r' and 'u26q' both decode to lat 48, lon 15.

i can "reproduce" this here
http://geohash.org/u26r
http://geohash.org/u26q

on the other hand, these sites visualize those geohashes as different squares

u26r
latitude :47.724609
longitude:14.589844

u26q
latitude :47.548828
longitude:14.589844

http://geohash.2ch.to/u26r
http://geohash.2ch.to/u26q
http://openlocation.org/geohash/geohash-js/
http://geohash.gofreerange.com/

i'm confused, thanks for any clarification :)

Google Geocode without file_get_contents callback

From a comment on a Drupal Geofield issue (http://drupal.org/node/1234496)

The module uses $this->result = json_decode(@file_get_contents($url)); (line 49 of GoogleGeocode.class.php) to actually send the request to Google and get the response. However, the file_get_contents operation will only work if 'allow_url_fopen' is on in the PHP configuration. If 'allow_url_fopen' is not allowed, then the 'file_get_contents' will not be allowed to execute, but the '@' sign suppresses error reporting. A lot of hosting providers rightly consider allow_url_fopen to be a security risk, so it's not really ideal. But this is in the geoPHP library, so I guess an issue should be filed with that project rather than here on Drupal.org.

In any case, in the Apache configuration, I added this line, which made the thing work: php_admin_flag allow_url_fopen on

Drupal uses 'drupal_http_request()' these days for those sorts of operations. It would be good if the geoPHP library could use something similar instead of 'allow_url_fopen'.

I'm not sure how to address this without either duplicating code or forcing some weird dependencies. It might be worth just saying that you need to have file_get_contents enabled on your server.

How to add new Adapter?

I write a new Adapter class and I want to use it without edit the original geoPHP files (include and getAdapterMap).
Is it possible?

Thanks.

Authorize the use of domNode directly in geomFromText methods

I use geoPhp and domDocument to parse (or write) gpx or kml files.
First, thank you for this great API !

It would be great to allow direct use of domNode in geomFromText methods (for xml formats).
Currently, my classes transform a domNode into a string to be able to use geoPhp geomFromText but this method transform this string into domNode again

I propose this replace in geomFromText methods :

Replace

// Change to lower-case, strip all CDATA, and de-namespace
$text = strtolower($text);
$text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);

// Load into DOMDOcument
$xmlobj = new DOMDocument();
@$xmlobj->loadXML($text);
if ($xmlobj === false) {
  throw new Exception("Invalid GeoRSS: ". $text);
}

$this->xmlobj = $xmlobj;

with

if(is_string($text)){
    // Change to lower-case and strip all CDATA
    $text = mb_strtolower($text, mb_detect_encoding($text));
    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);

    // Load into DOMDOcument
    $xmlobj = new DOMDocument();
    @$xmlobj->loadXML($text);
    if ($xmlobj === false) {
      throw new Exception("Invalid KML: ". $text);
    }

    $this->xmlobj = $xmlobj;
}
else if(is_object($text))
    $this->xmlobj = $text;
else
    return false;

append point to linstring

hi

how can I append new point at end of gpx track?

$newPoint = geoPHP::load('POINT(12.23 45.2)','wkt');

$trackFile = 'mytrack.gpx';

$track = geoPHP::load(file_get_contents($trackFile),'gpx');

...???append point???...

file_put_contents($trackFile, $out);

Error while processing json data

Hi I test your great code with some json data and for some it's work perfectly and for other it doesn't while my json is correct (i test it with a validator). Could you just have a look at this code please :

$json = '{ "type": "Feature", "properties": { "IIDTN_FRT": "test"}, "geometry": { "type": "Polygon", "coordinates":[ [[[1.6051958566706,50.785936202881],[1.6035577181033,50.786331165949],[1.6036272417271,50.786490972741],[1.6037969382322,50.786624987962],[1.6041839477005,50.786792164244],[1.6046934098152,50.78714709222],[1.6048584090552,50.787422397645],[1.6049731931262,50.787888965505],[1.6052573913483,50.788336485091],[1.6057083401075,50.788384091537],[1.6061482799832,50.788513039937],[1.6063639672226,50.788647351108],[1.6065610139567,50.788828930519],[1.6068650256711,50.789270813966],[1.607075797545,50.789828450257],[1.6070115346163,50.790671111528],[1.6069376464062,50.790999874813],[1.6066874254713,50.791643247549],[1.6064843317236,50.792351365233],[1.6064166446318,50.792845616202],[1.606223766391,50.793101929623],[1.6061364550305,50.793299215329],[1.6061411880416,50.793466435912],[1.6062409008458,50.793712665294],[1.6067011089333,50.794019979963],[1.6067487097456,50.794187068363],[1.6064929082631,50.794837103446],[1.6062037185237,50.795914911599],[1.6056571797231,50.79736187957],[1.6052387215233,50.798177982637],[1.6047862416646,50.799311791108],[1.6045125617485,50.799781835828],[1.6043323755464,50.800011466138],[1.6040304871711,50.801024110774],[1.6061178156331,50.801071056569],[1.6071163760902,50.801055994173],[1.6086430038263,50.8009390864],[1.6090752979237,50.801216164707],[1.6091018993069,50.801713634896],[1.6091556157668,50.801817438129],[1.6093368753415,50.801883926923],[1.6110921379731,50.801819002747],[1.6122515683712,50.801719775368],[1.6132479797856,50.80161245365],[1.6151480279832,50.801344726775],[1.6157166439056,50.800750318725],[1.6158133574053,50.800562131826],[1.6158254225854,50.800431766068],[1.6157173173456,50.800192635204],[1.6139127165632,50.798025713941],[1.6138062237643,50.797927576773],[1.612991809047,50.797410860171],[1.6127552586171,50.797207230138],[1.6126360762814,50.797030687965],[1.6125378593967,50.796652538209],[1.6122645865852,50.793378973862],[1.6121795334486,50.793106498798],[1.6119522644812,50.792942627496],[1.6110340373853,50.792622464612],[1.61074245503,50.792462612884],[1.6104691025385,50.792220287038],[1.6103225879518,50.791981423199],[1.6100866778656,50.790669846697],[1.6099278309427,50.790212720735],[1.6074551972525,50.787507024282],[1.6070660559365,50.78732826646],[1.6059453262665,50.787117423065],[1.6055502328683,50.786935365941],[1.6053786097774,50.786775852918],[1.6053159781859,50.786649546594],[1.6051958566706,50.785936202881]]] ]}}';

$polygon1 = geoPHP::load($json);
$area = round(($polygon1->getArea())/1000000,3);
$centroid = $polygon1->getCentroid();
$centX = $centroid->getX();
$centY = $centroid->getY();

print "This polygon2 has an area of ".$area." and a centroid with X=".$centX." and Y=".$centY;

project Method is missing

Hi,

i would like to calculate some distances in meters with the GEOS extension. In the standard WGS84 projection the distance function returns no meters, but degrees. So I learned I have to project to a different SRID.

The project method is not implemented, any reason for this? Maybe I can supply a patch.

best,

Karsten

bbox of polygons that cross the international date line

Hi,

Thank you very much for this library.
I've added a few functionalities such as Yahoo geocoding and reverse geocoding (they have greater request limits than google's) which I will submit as a pull request as soon as my project's rush period is over.

However, I'm getting strange results when using the getBBox function on a multipolygon representing countries that cross the international date line such as Russia and New Zealand.

For Russia, for example, I'm getting a very thin line that follows the international date line.
I'm guessing this is an issue with the calculation of the min and max values.

Any idea how to fix this ?

Thank you.

GeoHash revisited

We need to support geohash for polygons and other non-point geometries.

I think our goal would be to create a geohash who's "bounding box" is entirely within the geohash "bin".

My suggestion is as follows:

  1. Get the envelope for the geometry.
  2. Compute the geohash for each of the four corners of the envelope
  3. Compare the four strings, return the leading parts of the string that all match.

This also brings to mind that I think treating geohashes as points (when convering from geohash to geometry) might be a mistake. Reading a geohash should always result in a four-point polygon, not a point.

Support all geometry types

Specifically:

  • CircularString (and it's derivatives)

Also support 3D geometries

  • All existing geometries in 3D space
  • SURFACE (and it's derivatives)

Converting GPX to KML

HI,

If I try this:

<?php
include_once('../geoPHP.inc');
$value = file_get_contents('test_2.gpx');
$geometry = geoPHP::load($value, 'gpx');
echo $geometry->out('kml');

I get:

<LineString><coordinates>-72.579110031947,42.489776238799 -72.579105002806,42.489786967635 -72.579118078575,42.489776406437..........

Is it expected or bug? I would expect

<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2">.......

Composer install failing

When installing with composer and the minimum-stability isn't set to dev the install will fail. I think this is because composer checks the latest tagged release (1.1) which doesn't contain a composer file.

Collection namespace too common

Complex project have many dependancies, so the class name "Collection" might collide with other classes.
This is an example error message:
Fatal error: Cannot redeclare class Collection in /project/vendor/phayes/geophp/lib/geometry/Collection.class.php on line 293
This issue can be fixed in two ways:

  1. Using a certain namespace for the library.
  2. Prefixing the classes.

Too many recursions for preg_match_all in parseGeometryCollection

hi,

i just came across a problem, that preg_match_all in parseGeometryCollection will die when re-saving a larger WKT with bout 1000 points.

lowering the pcre.recursion_limit as for example documented in http://stackoverflow.com/questions/7620910/regexp-in-preg-match-function-returning-browser-error will avoid such behavior but strip the contents from the WKT field.

as a side-note, i have some cycling tracks from garmin which i export using google earth to KML export and try to import them into drupal 7 using geocoder + geofield. the import/geocoding part works, but when re-saving the drupal node that contains the WKT data, the problem stated above causes PHP to die.

i'm not sure what to best do about this problem? maybe the regex in parseGeometryCollection could be optimized...

Missing Z coordinate in point

Hi,

I have a KML file with the following line string coordinates;

<coordinates>5.032150000,52.03049000,0.0000000
5.030590000,52.02989000,1.0000000</coordinates>

Is there a reason the altitude data is completely ignored?

For example in the KML adapter;

  protected function parsePoint($xml) {
          $coordinates = $this->_extractCoordinates($xml);
          return new Point($coordinates[0][0],$coordinates[0][1]);
  }

The 3rd constructor argument of the Point class is never set :( however, the component data is available in $coordinates.

Error in KML.class.php

PHP Parse error: syntax error, unexpected T_STATIC in /Users/dtarc/development/workspace/wcbrr/profiles/wcbrr/libraries/geoPHP/lib/adapters/KML.class.php on line 60

There is some problem with the static function call:

        try {
            $geom = static::_geomFromXML($xmlobj);
        } catch(InvalidText $e) {
            throw new Exception("Invalid KML: ". $text);
        } catch(\Exception $e) {
            throw $e;
        }

Error using distance method

I get this error in httpd error_log

httpd: GeometryComponentFilter.cpp:35: virtual void geos::geom::GeometryComponentFilter::filter_ro(const geos::geom::Geometry*): Assertion `0' failed.

my code is as follows

$wkt_reader = new GeoJSON();
        $cord = array(array(100.0, 0.0), array(101.0, 1.0));
        $geometry = $wkt_reader->read(json_encode(array('type'=>'MultiPoint','coordinates'=>$cord),TRUE));
        $point1 = $wkt_reader->read(json_encode(array('type'=>'Point','coordinates'=>array(100.0, 0.0)),TRUE));
        $point2 = $wkt_reader->read(json_encode(array('type'=>'Point','coordinates'=>array(101.0, 1.0)),TRUE));
        $d = $geometry->distance($point1);

Please help if you can

exception in tests suite

c5f8c9e

---- Testing route.gpx
---- Testing short.geohash
---- Testing simple_point.json
---- Testing track.gpx
PHP Fatal error:  Uncaught exception 'Exception' with message 'IllegalArgumentException: Empty Points cannot be represented in WKB' in /home/ramunas/src/geoPHP/geoPHP.inc:135
Stack trace:
#0 /home/ramunas/src/geoPHP/geoPHP.inc(135): GEOSWKBWriter->writeHEX(Object(GEOSGeometry))
#1 /home/ramunas/src/geoPHP/lib/geometry/Collection.class.php(49): geoPHP::geosToGeometry(Object(GEOSGeometry))
#2 /home/ramunas/src/geoPHP/tests/test.php(43): Collection->centroid()
#3 /home/ramunas/src/geoPHP/tests/test.php(29): test_geometry(Object(LineString))
#4 /home/ramunas/src/geoPHP/tests/test.php(4): run_test()
#5 {main}
  thrown in /home/ramunas/src/geoPHP/geoPHP.inc on line 135

Fatal error: Uncaught exception 'Exception' with message 'IllegalArgumentException: Empty Points cannot be represented in WKB' in /home/ramunas/src/geoPHP/geoPHP.inc:135
Stack trace:
#0 /home/ramunas/src/geoPHP/geoPHP.inc(135): GEOSWKBWriter->writeHEX(Object(GEOSGeometry))
#1 /home/ramunas/src/geoPHP/lib/geometry/Collection.class.php(49): geoPHP::geosToGeometry(Object(GEOSGeometry))
#2 /home/ramunas/src/geoPHP/tests/test.php(43): Collection->centroid()
#3 /home/ramunas/src/geoPHP/tests/test.php(29): test_geometry(Object(LineString))
#4 /home/ramunas/src/geoPHP/tests/test.php(4): run_test()
#5 {main}
  thrown in /home/ramunas/src/geoPHP/geoPHP.inc on line 135

GPL license

It would be nice to include geoPHP with Geofield so that it's easier for site admins to set up the module without having to come here first. Granted, drush integration makes that pretty easy for those of us that are drush make savvy, but it would be nice to remove this minor stumbling block. For us to include geoPHP with Geofield, the code would have to be released under a GPL license.

Are you (and by extension, the rest of the copyright holders) willing to consider releasing the code either under GPL, or a duel-license similar to Geofield?

Error message on "make check". Failed asserting that two strings are equal.

I'm getting an error message during make check: On the end it returns:


chmod +x phpunit
PHPUnit 3.4.14 by Sebastian Bergmann.

.........................F.................................. 60 / 70
..........

Time: 1 second, Memory: 7.75Mb

There was 1 failure:

1) test::testGeometry_pointOnSurface
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-POINT (2 5)
+POINT (1 8)

/tmp/geos-svn/php/test/test.php:1060

FAILURES!
Tests: 70, Assertions: 532, Failures: 1.
FAIL: phpunit
==================
1 of 1 test failed
==================
make[4]: *** [check-TESTS] Error 1
make[4]: Leaving directory `/tmp/geos-svn/php/test'
make[3]: *** [check-am] Error 2
make[3]: Leaving directory `/tmp/geos-svn/php/test'
make[2]: *** [check-recursive] Error 1
make[2]: Leaving directory `/tmp/geos-svn/php'
make[1]: *** [check-recursive] Error 1
make[1]: Leaving directory `/tmp/geos-svn'
make: *** [check] Error 2


Polygon Centroids signs are opposite

When computing the centroid of a polygon that has negatives components (anything not in the north eastern hemisphere), the centroid is inverted (the signs are opposite).

The polygon in the example below is a very simple outline of the US. It's centroid should be

X=-98.3413124738 and Y=39.8526108305

but instead its

X=98.3413124738 and Y=-39.8526108305.

I have looked at the Polygon centroid method, but don't fully understand it yet to suggest a fix yet.

<?php

include_once('geoPHP/geoPHP.inc');

// Polygon WKT example
$polygon = geoPHP::load('POLYGON ((-123.222653196 49.1529676585, -89.4726531957 49.3823707987, -81.0351531957 44.0875828344, -71.1914031957 44.3395630636, -62.0507781957 48.4583498573, -60.2929656957 45.0890334085, -78.9257781957 37.4399716272, -82.0898406957 31.3536343332, -81.3867156957 26.4312253295, -91.9335906957 29.8406412505, -98.2617156957 26.4312253295, -107.753903196 32.2499718728, -116.894528196 33.1375486348, -122.519528196 36.0313293064, -126.035153196 42.2935619329, -123.222653196 49.1529676585))','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;

Provide 'data' property

It would be nice to have a 'data' array property ($geometry->data). When doing geometric operations, the contents of this property would be preserved.

Add geohash support

Geohash is another way of storing geographical information and can be useful in mathematical representations :

http://en.wikipedia.org/wiki/Geohash

There is an easy library available here :
https://github.com/asonge/php-geohash/blob/master/geohash.php

This feature requests comes from within the Drupal community, where geofield extensively uses the geophp library. If the geohash algorithm could be added, we have a new way of showing locations and this can then be exploited in several integrations such as solr
http://wiki.apache.org/solr/SpatialSearch

Add Equiv Function/Method to ST_TRANSFORM

I am not sure if this has been addressed before, I see mention of "Transformation" methods in the wiki but donts see a transform. I notice that this can be done in javascript now via GeoScript but still dont see a way to transform a GCS polygon to something that will produce a valid area in PHP without the aid of PostGis or Ogr2ogr. I can do al lot with GeoPHP but its not enough as my industry (In Canada at least) still uses a GPS to create a wildfire polygon, and this is done to calculate Area Burned. so the data gets uploaded into the application (Drupal) and thereby into PHP, where I can use GeoPHP to calculate the area, but not before I put the data thru PostGis ST_Transform (or reproject with Ogr2ogr)so that I can calculate a valid area. If this is already possible in some way I am Missing it, otherwise I am suggesting a feature request :)

Polygon->centroid() doesn't work for 1-dimensional polygons

hi phayes,

i just noticed this when calculating the centroid of a MultiPoint.

this is the input:

  { "geometry" : { "coordinates" : [ 15.48333, 47.95],  "type" : "Point" }, },
  { "geometry" : { "coordinates" : [ 15.46667, 47.95 ], "type" : "Point"  }, },

i would have expected the centroid for a MultiPoint of the two given points to be the center, but it returns just the first Point:

  { "geometry" : { "coordinates" : [ 15.48333, 47.95],  "type" : "Point" }, },

the problem here is that the polygon of the two points has an area() of zero and in such cases Polygon->centroid() will return just the first point.

regards dasjo

Simplifying a Polygon fails

I'm trying to simplify a WKT-polygon using:

    $oGeometry = geoPHP::load($sWkt,'wkt');    
    $reducedGeom = $oGeometry->simplify(1.5);
    $sWkt = $reducedGeom->out('wkt');

PHP throws a fatal error:

Warning: fread() [function.fread]: Length parameter must be greater than 0 in WKB.class.php on line 71


Fatal error: Uncaught exception 'Exception' with message 'Linestring with less than two points' in LineString.class.php:35 Stack trace: 
#0 /adapters/WKB.class.php(81): LineString->__construct(Array) 
#1 /adapters/WKB.class.php(91): WKB->getLinstring(Resource id #69) 
#2 /adapters/WKB.class.php(49): WKB->getPolygon(Resource id #69) 
#3 /adapters/WKB.class.php(33): WKB->getGeometry(Resource id #69) 
#4 [internal function]: WKB->read('010300000001000...', true) 
#5 geophp/geoPHP.inc(53): call_user_func_array(Array, Array) 
#6 geophp/geoPHP.inc(107): geoPHP::load('010300000001000...', 'wkb', true) 
#7 parsewfs.php(65): geoPHP::geosTo in /geometry/LineString.class.php on line 35

Any ideas how to fix this? Simplifying the geomtry by 0 works (although obviously, it has no effect).

Thanks for your help

Namespacing Classes

I'd like to integrate the GeoPHP project into our companies CMS, there is however a problem with the classnames. For example in some client-installations there's already a Point object defined. This would be solved by namespacing the Classes (e.g. geoPHP_Geometry_Point).

I absolutely realise that adding namespacing breaks backwards compatibility, but I do like to know what your opinion is regarding this subject.

If this is not the place to start this discussion, feel free to disregard and/or remove this issue!

OSM format support

it would be very interesting to have a support for format of openstreetmap *.OSM
it is still xml, I do not think that it is difficult to implement

Fix Hack alert in Geometry.class.php

Sorry I'm at work and I can't make a pull request.

in Geometry.class.php replace line 107 to 115 with :

array_unshift($args, $this);
$result = call_user_func_array(array($processor, 'write'), $args);

I'll make a proper request from home if you'd prefer.

License

The source files here have a note about being originally written by camptocamp and referring to a license file. I'd assume this license file, which is BSD. Could this and a note about the original authors be added?

Class name 'WKT' conflicts with PostGIS class 'Wkt'

When I try to load the geoPHP library, this is the error I get:

PHP Fatal error: Cannot redeclare class WKT in /var/www/sites/all/modules/geophp/geoPHP/lib/adapters/WKT.class.php on line 247

This is conflicting with:
profiles/cartaro/modules/contrib/postgis/includes/postgis.geometry.inc:class Wkt

Who's responsibility is it to ensure there are no class name conflicts?
What should I do to work around this?

Better XML support

For our XML based adapters, namespace support is pretty ad-hoc.

These are the features I would like to see:

  1. When a fully-valid XML document with name-spaces is passed in, use those namespaces
  2. When an XML snippet is passed in, by default we should support a non-name-spaced snippet, and a "default" namespace for each given adapter. (ie, for geoRSS support <georss:point>123 123</georss:point> with no configuration)
  3. When an XML snippet is passed in, allow a namespace to be passed in as a paramater.
  4. When outputting XML snippets, allow a namespace to be assed in as a parameter - default to no namespacing.

Negative area

Hi,

With a multipolygon like this one above I have a negative area, is it related to the multipolygon or the area can't be use with multipolygons? Thanks for your feeback!

{ "type": "Feature", "properties": { "IIDTN_FRT": "F00630F"}, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 3.1073143875919, 44.887990006903 ], [ 3.1073384647246, 44.888068080817 ], [ 3.1075574038406, 44.88821587608 ], [ 3.107583969472, 44.888410297556 ], [ 3.1077113063361, 44.888557690435 ], [ 3.1087819311883, 44.888763307141 ], [ 3.1090708287066, 44.888791223069 ], [ 3.1093551378882, 44.888777656244 ], [ 3.1095141422255, 44.888813531059 ], [ 3.1090502371319, 44.889283741026 ], [ 3.1087144560652, 44.889566787657 ], [ 3.1084034322231, 44.889381047491 ], [ 3.1081650179318, 44.889288590702 ], [ 3.1073145025109, 44.889139541321 ], [ 3.107177569805, 44.889154759261 ], [ 3.1068235332224, 44.889048263171 ], [ 3.1065226026304, 44.889036864865 ], [ 3.1063973327936, 44.889064654543 ], [ 3.1061424043511, 44.888979545505 ], [ 3.1059482830336, 44.889028757675 ], [ 3.1058715657338, 44.889105311774 ], [ 3.1057590491724, 44.889131743138 ], [ 3.1054041816669, 44.889084688019 ], [ 3.1052852878035, 44.889096365908 ], [ 3.1050117230492, 44.889280228524 ], [ 3.1047478578226, 44.889516745009 ], [ 3.1047123607098, 44.889719533044 ], [ 3.1047556172507, 44.889855806178 ], [ 3.1046165316473, 44.890024301111 ], [ 3.1040714554571, 44.890387696957 ], [ 3.1040805600171, 44.890640165132 ], [ 3.1036446919409, 44.890616185369 ], [ 3.103352923363, 44.89062756221 ], [ 3.1031742615921, 44.890664862379 ], [ 3.1030053543552, 44.890760272035 ], [ 3.1026898234391, 44.89107473528 ], [ 3.1024942725705, 44.891192908744 ], [ 3.1023204011887, 44.891214828988 ], [ 3.1019264457505, 44.891214875545 ], [ 3.1016117845157, 44.891079920495 ], [ 3.1003187175523, 44.890242790895 ], [ 3.1002091156681, 44.890208339662 ], [ 3.1000480551496, 44.89025436532 ], [ 3.1001143789322, 44.89034683965 ], [ 3.1000700165148, 44.890444338549 ], [ 3.0993850527596, 44.891021635866 ], [ 3.0995945628377, 44.891186574165 ], [ 3.0998928276267, 44.891561169747 ], [ 3.1000485890195, 44.891655089987 ], [ 3.1002691112195, 44.892054292059 ], [ 3.1007370633289, 44.892376140699 ], [ 3.1009669667374, 44.892473595089 ], [ 3.101694883784, 44.892690395645 ], [ 3.1019817128134, 44.892739575055 ], [ 3.1022967544782, 44.892737278263 ], [ 3.1025618651497, 44.892806025145 ], [ 3.1031862288287, 44.892716990712 ], [ 3.1036593154526, 44.892572180369 ], [ 3.1041315749509, 44.892541317511 ], [ 3.1043222548653, 44.892555005453 ], [ 3.104650770915, 44.892691669791 ], [ 3.1051004933453, 44.892753918024 ], [ 3.1054288214017, 44.892886081701 ], [ 3.1058848233546, 44.892957755313 ], [ 3.1063885243405, 44.893170726734 ], [ 3.1069514511488, 44.893217178885 ], [ 3.1071571518892, 44.893204632365 ], [ 3.1073934012564, 44.893142958291 ], [ 3.1078099972875, 44.892910764897 ], [ 3.1079747802046, 44.892898235354 ], [ 3.1085373614493, 44.893236716568 ], [ 3.1098147790196, 44.89357866482 ], [ 3.1106616341205, 44.89373450855 ], [ 3.1112991511632, 44.893659144315 ], [ 3.1117563984149, 44.893328208984 ], [ 3.1112811824682, 44.893419681102 ], [ 3.1111528529288, 44.893360450636 ], [ 3.1111243925343, 44.893308691417 ], [ 3.1111622036387, 44.893203301999 ], [ 3.1120918876493, 44.892839886331 ], [ 3.1121075553935, 44.892683073962 ], [ 3.1122539012991, 44.892554838066 ], [ 3.1126337090937, 44.892438485032 ], [ 3.113279323493, 44.892303559957 ], [ 3.1132805233193, 44.8921416213 ], [ 3.1130521089788, 44.891825811336 ], [ 3.1130748553339, 44.891690292595 ], [ 3.1129879994682, 44.891413290592 ], [ 3.1128114868463, 44.891103526342 ], [ 3.1125259981665, 44.89079693385 ], [ 3.1121009022167, 44.890036341791 ], [ 3.1118616546734, 44.889794587148 ], [ 3.1118417687694, 44.889711067762 ], [ 3.1115262816921, 44.889283332923 ], [ 3.1111051516915, 44.888852315383 ], [ 3.1109842612813, 44.888787144647 ], [ 3.1107873988442, 44.888552915848 ], [ 3.1101656256023, 44.888311476475 ], [ 3.1095379855259, 44.888135905174 ], [ 3.1092561153749, 44.888142119365 ], [ 3.1083767189252, 44.888032756288 ], [ 3.1080182369488, 44.888048228974 ], [ 3.1073143875919, 44.887990006903 ] ],[ [ 3.0573643763135, 44.917076238071 ], [ 3.0571561515804, 44.917235450636 ], [ 3.056903031026, 44.917578663336 ], [ 3.0568285678245, 44.917771576919 ], [ 3.0567777358745, 44.918078377083 ], [ 3.0568603923971, 44.91848233804 ], [ 3.0570211563374, 44.918699470968 ], [ 3.0572296418645, 44.91887176271 ], [ 3.0580821190806, 44.919391988591 ], [ 3.0584150124185, 44.919398631674 ], [ 3.0594690523443, 44.919334907907 ], [ 3.0604132861668, 44.919391303153 ], [ 3.0608894447264, 44.919387884332 ], [ 3.0614011953486, 44.919301833316 ], [ 3.0617875501148, 44.919167473908 ], [ 3.0633225003763, 44.918334392629 ], [ 3.0634971298689, 44.918201661306 ], [ 3.0635797373335, 44.918081749872 ], [ 3.0636979763623, 44.91766837081 ], [ 3.0637534439684, 44.917631828011 ], [ 3.0638980832214, 44.917616320456 ], [ 3.0641233611366, 44.917457781426 ], [ 3.0641829890543, 44.917277840302 ], [ 3.0647366656872, 44.917005152617 ], [ 3.0649064167956, 44.916886407334 ], [ 3.0651529913494, 44.916864755371 ], [ 3.0654517876207, 44.916667359678 ], [ 3.0659203390361, 44.916629291042 ], [ 3.0663322118811, 44.916683488983 ], [ 3.0667186715238, 44.916783399557 ], [ 3.0674279722394, 44.91685674863 ], [ 3.0677280462652, 44.916975167309 ], [ 3.0686687534664, 44.917436541277 ], [ 3.0692743270687, 44.917094856967 ], [ 3.069933938425, 44.916924839757 ], [ 3.0700786511317, 44.916919493554 ], [ 3.0707710872124, 44.916705840027 ], [ 3.0701723445419, 44.916406860823 ], [ 3.0690867002986, 44.916003812772 ], [ 3.0686420060687, 44.915806791879 ], [ 3.0684788666762, 44.915702500921 ], [ 3.0684075017387, 44.915475078482 ], [ 3.0684501825935, 44.915245798863 ], [ 3.0686138966888, 44.915067943383 ], [ 3.0689083881875, 44.914845781618 ], [ 3.0693668348488, 44.914599252079 ], [ 3.0694151776811, 44.914477399738 ], [ 3.0694335239413, 44.91376227263 ], [ 3.0693361735535, 44.9123397371 ], [ 3.0674669561046, 44.912376587893 ], [ 3.0673361632308, 44.912343080327 ], [ 3.0672926419533, 44.912303438534 ], [ 3.0670773594037, 44.910057701791 ], [ 3.06690535851, 44.909846647252 ], [ 3.0669523523076, 44.909718267119 ], [ 3.0666320491177, 44.909632394583 ], [ 3.066562104015, 44.909546360161 ], [ 3.0665293127056, 44.909396325099 ], [ 3.0664103604184, 44.909509909934 ], [ 3.0662479579451, 44.909539878737 ], [ 3.0660175816235, 44.909642105688 ], [ 3.0659353638726, 44.90964078049 ], [ 3.0658102638111, 44.909569245262 ], [ 3.0656475873843, 44.90933390644 ], [ 3.0656020281623, 44.90913907072 ], [ 3.0651112257758, 44.909079352671 ], [ 3.0650178125311, 44.909009877884 ], [ 3.0645439723107, 44.909099007631 ], [ 3.0644114644631, 44.909083272458 ], [ 3.0636198957525, 44.908407341929 ], [ 3.0632185672976, 44.908455706695 ], [ 3.0643205203038, 44.909319828016 ], [ 3.0648611101821, 44.910068864079 ], [ 3.0656615323405, 44.911305294157 ], [ 3.0660482951459, 44.9117659063 ], [ 3.0661199301383, 44.91208157902 ], [ 3.0660062233683, 44.912438637245 ], [ 3.0660403271668, 44.91289497185 ], [ 3.0663178660493, 44.913272088554 ], [ 3.0663456822738, 44.913550582456 ], [ 3.0662608537004, 44.91380797657 ], [ 3.0657816737201, 44.914291368219 ], [ 3.0654745012736, 44.914495420371 ], [ 3.0650653982333, 44.914641938259 ], [ 3.0640270381049, 44.914809317766 ], [ 3.0632134881911, 44.914873165497 ], [ 3.0608305310844, 44.915375883991 ], [ 3.0600179976807, 44.915765228102 ], [ 3.0595955154311, 44.915899156427 ], [ 3.0588977216506, 44.916292055533 ], [ 3.0581455244366, 44.916572882765 ], [ 3.0585835051898, 44.916924114478 ], [ 3.0587869508732, 44.917136332939 ], [ 3.0589941275912, 44.91721573364 ], [ 3.0591780584178, 44.917238133175 ], [ 3.0597982995512, 44.916705669518 ], [ 3.0599101496019, 44.916658773062 ], [ 3.0600605022187, 44.91668117364 ], [ 3.0603927284539, 44.916302283892 ], [ 3.061167917493, 44.91600504661 ], [ 3.0613825557643, 44.915983419702 ], [ 3.0615478998399, 44.915896380646 ], [ 3.0625687627057, 44.91564948922 ], [ 3.062761631176, 44.915495773503 ], [ 3.0631333545529, 44.915406566612 ], [ 3.0632975410437, 44.915401042223 ], [ 3.0634054892627, 44.915456969469 ], [ 3.063653505798, 44.915853729432 ], [ 3.063814654449, 44.915964354357 ], [ 3.0638374142968, 44.916016261788 ], [ 3.0637997690159, 44.916111056802 ], [ 3.0638521023211, 44.916272227835 ], [ 3.0637644925669, 44.916350210591 ], [ 3.0635462623639, 44.91638365163 ], [ 3.0634281368031, 44.916579314031 ], [ 3.0630112316498, 44.91692479634 ], [ 3.0627392610203, 44.917291406732 ], [ 3.0621192752355, 44.917502171812 ], [ 3.0615962500115, 44.917737514087 ], [ 3.060570946992, 44.917994035951 ], [ 3.0598115754772, 44.918260446843 ], [ 3.059559040732, 44.918699936379 ], [ 3.0590166531873, 44.919104117839 ], [ 3.0587510084504, 44.919186446269 ], [ 3.0585035231002, 44.919193209506 ], [ 3.0578586149943, 44.919055164129 ], [ 3.0576317966112, 44.918916357324 ], [ 3.0574391136635, 44.91870249686 ], [ 3.0572851601295, 44.918321541196 ], [ 3.0572069334854, 44.917728633859 ], [ 3.0572478973695, 44.917417485431 ], [ 3.0573643763135, 44.917076238071 ] ],[ [ 3.0525584586193, 44.90390648195 ], [ 3.0521495224336, 44.904051925287 ], [ 3.0524706364081, 44.904360649307 ], [ 3.0531835742526, 44.905184285574 ], [ 3.0532853646557, 44.905553807214 ], [ 3.0534788668272, 44.905844365025 ], [ 3.0536108043378, 44.906215942571 ], [ 3.0538491501124, 44.907059612109 ], [ 3.0539116808197, 44.907149367029 ], [ 3.0539715555149, 44.907174688261 ], [ 3.0547751296051, 44.906552774836 ], [ 3.0549297280563, 44.906368432813 ], [ 3.0552718225206, 44.905715881618 ], [ 3.0554704998965, 44.905579795939 ], [ 3.0562565270321, 44.904131655368 ], [ 3.0562509385962, 44.904015967542 ], [ 3.0560850249658, 44.904009448269 ], [ 3.0556896656142, 44.903873853864 ], [ 3.0554068161887, 44.903687642517 ], [ 3.0525584586193, 44.90390648195 ] ],[ [ 3.0594216878025, 44.901761314545 ], [ 3.0576435728709, 44.903049296159 ], [ 3.0588845345163, 44.904618523002 ], [ 3.0590283451162, 44.904428590974 ], [ 3.0592694841664, 44.90426193587 ], [ 3.0593500806773, 44.904147461437 ], [ 3.0597920324928, 44.903955763545 ], [ 3.0601768713262, 44.903926181181 ], [ 3.0604039632984, 44.904001699518 ], [ 3.0606887761601, 44.903973413908 ], [ 3.0610868068157, 44.903993935225 ], [ 3.0607748571184, 44.902574152456 ], [ 3.0606102417575, 44.902214990604 ], [ 3.0594216878025, 44.901761314545 ] ],[ [ 3.1187284694037, 44.888138603853 ], [ 3.1185092420036, 44.889207239356 ], [ 3.1200407469625, 44.889126033706 ], [ 3.1210876191473, 44.889125914058 ], [ 3.1215403440674, 44.889071062671 ], [ 3.1216957969709, 44.888289457355 ], [ 3.1213832690139, 44.887377318088 ], [ 3.1210273503497, 44.88757404661 ], [ 3.1203249480621, 44.887841410258 ], [ 3.1199857203372, 44.88787804453 ], [ 3.1193128345273, 44.887826301276 ], [ 3.1187284694037, 44.888138603853 ] ],[ [ 3.1075027867906, 44.887999766664 ], [ 3.1075027089531, 44.888000474283 ], [ 3.1075028379672, 44.887999770519 ], [ 3.1075027867906, 44.887999766664 ] ] ] ]}}

Result :

area : -1.179483049896E-5
centX : 3.0869226597097
centY : 44.903387974987

EWKB adaptor does not support SRID on write

Looks like the read function for EWKB adaptor supports SRID. The write function does not.

Support for SRID on write is needed in order to save records to postgis in EWKB format when the SRID is set on a geometry column.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.