forked from phayes/geoPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
260 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* EWKB (Extended Well Known Binary) Adapter | ||
*/ | ||
class EWKB extends WKB | ||
{ | ||
|
||
/** | ||
* Read WKB binary string into geometry objects | ||
* | ||
* @param string $wkb An Extended-WKB binary string | ||
* | ||
* @return Geometry | ||
*/ | ||
public function read($wkb, $is_hex_string = FALSE) { | ||
if ($is_hex_string) { | ||
$wkb = pack('H*',$wkb); | ||
} | ||
|
||
// Open the wkb up in memory so we can examine the SRID | ||
$mem = fopen('php://memory', 'r+'); | ||
fwrite($mem, $wkb); | ||
fseek($mem, 0); | ||
$base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); | ||
if ($base_info['s']) { | ||
$srid = unpack("Lsrid", fread($mem, 4)); | ||
} | ||
fclose($mem); | ||
|
||
// Run the wkb through the normal WKB reader to get the geometry | ||
$wkb_reader = new WKB(); | ||
$geom = $wkb_reader->read($wkb); | ||
|
||
// If there is an SRID, add it to the geometry | ||
if ($srid) { | ||
$geom->setSRID($srid); | ||
} | ||
|
||
return $geom; | ||
} | ||
|
||
/** | ||
* Serialize geometries into an EWKB binary string. | ||
* | ||
* @param Geometry $geometry | ||
* | ||
* @return string The Extended-WKB binary string representation of the input geometries | ||
*/ | ||
public function write(Geometry $geometry) { | ||
// We always write into NDR (little endian) | ||
$wkb = pack('c',1); | ||
|
||
switch ($geometry->getGeomType()) { | ||
case 'Point'; | ||
$wkb .= pack('L',1); | ||
$wkb .= $this->writePoint($geometry); | ||
break; | ||
case 'LineString'; | ||
$wkb .= pack('L',2); | ||
$wkb .= $this->writeLineString($geometry); | ||
break; | ||
case 'Polygon'; | ||
$wkb .= pack('L',3); | ||
$wkb .= $this->writePolygon($geometry); | ||
break; | ||
case 'MultiPoint'; | ||
$wkb .= pack('L',4); | ||
$wkb .= $this->writeMulti($geometry); | ||
break; | ||
case 'MultiLineString'; | ||
$wkb .= pack('L',5); | ||
$wkb .= $this->writeMulti($geometry); | ||
break; | ||
case 'MultiPolygon'; | ||
$wkb .= pack('L',6); | ||
$wkb .= $this->writeMulti($geometry); | ||
break; | ||
case 'GeometryCollection'; | ||
$wkb .= pack('L',7); | ||
$wkb .= $this->writeMulti($geometry); | ||
break; | ||
} | ||
|
||
if ($write_as_hex) { | ||
$unpacked = unpack('H*',$wkb); | ||
return $unpacked[1]; | ||
} | ||
else { | ||
return $wkb; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* EWKT (Extended Well Known Text) Adapter | ||
*/ | ||
class EWKT extends WKT | ||
{ | ||
|
||
/** | ||
* Serialize geometries into an EWKT string. | ||
* | ||
* @param Geometry $geometry | ||
* | ||
* @return string The Extended-WKT string representation of the input geometries | ||
*/ | ||
public function write(Geometry $geometry) { | ||
$srid = $geometry->SRID(); | ||
$wkt = ''; | ||
if ($srid) { | ||
$wkt = 'SRID=' . $srid . ';'; | ||
$wkt .= $geometry->out('wkt'); | ||
return $wkt; | ||
} | ||
else { | ||
return $geometry->out('wkt'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<? | ||
// Uncomment to test | ||
# run_test(); | ||
|
||
function run_test() { | ||
|
||
header("Content-type: text"); | ||
|
||
include_once('../geoPHP.inc'); | ||
|
||
// Your database test table should contain 3 columns: name (text), type (text), geom (geometry) | ||
$host = 'localhost'; | ||
$database = 'phayes'; | ||
$table = 'test'; | ||
$column = 'geom'; | ||
$user = 'phayes'; | ||
$pass = 'supersecret'; | ||
|
||
$connection = pg_connect("host=$host dbname=$database user=$user password=$pass"); | ||
|
||
// Truncate | ||
pg_query($connection, "DELETE FROM $table"); | ||
|
||
// Working with PostGIS and EWKB | ||
// ---------------------------- | ||
|
||
foreach (scandir('./input') as $file) { | ||
$parts = explode('.',$file); | ||
if ($parts[0]) { | ||
$name = $parts[0]; | ||
$format = $parts[1]; | ||
$value = file_get_contents('./input/'.$file); | ||
print '---- Testing '.$file."\n"; | ||
flush(); | ||
$geometry = geoPHP::load($value, $format); | ||
test_postgis($name, $format, $geometry, $connection, 'wkb'); | ||
$geometry->setSRID(4326); | ||
test_postgis($name, $format, $geometry, $connection, 'ewkb'); | ||
} | ||
} | ||
print "Testing Done!"; | ||
} | ||
|
||
function test_postgis($name, $type, $geom, $connection, $format) { | ||
global $table; | ||
|
||
// Let's insert into the database using GeomFromWKB | ||
$insert_string = pg_escape_bytea($geom->out($format)); | ||
pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', GeomFromWKB('$insert_string'))"); | ||
|
||
// SELECT using asBinary PostGIS | ||
$result = pg_fetch_all(pg_query($connection, "SELECT asBinary(geom) as geom FROM $table WHERE name='$name'")); | ||
foreach ($result as $item) { | ||
$wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob | ||
$geom = geoPHP::load($wkb, $format); // We now a full geoPHP Geometry object | ||
} | ||
|
||
// SELECT and INSERT directly, with no wrapping functions | ||
$result = pg_fetch_all(pg_query($connection, "SELECT geom as geom FROM $table WHERE name='$name'")); | ||
foreach ($result as $item) { | ||
$wkb = pack('H*',$item['geom']); // Unpacking the hex blob | ||
$geom = geoPHP::load($wkb, $format); // We now have a geoPHP Geometry | ||
|
||
// Let's re-insert directly into postGIS | ||
// We need to unpack the WKB | ||
$unpacked = unpack('H*', $geom->out($format)); | ||
$insert_string = $unpacked[1]; | ||
pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', '$insert_string')"); | ||
} | ||
|
||
// SELECT and INSERT using as EWKT (ST_GeomFromEWKT and ST_AsEWKT) | ||
$result = pg_fetch_all(pg_query($connection, "SELECT ST_AsEWKT(geom) as geom FROM $table WHERE name='$name'")); | ||
foreach ($result as $item) { | ||
$wkt = $item['geom']; // Make sure to unescape the hex blob | ||
$geom = geoPHP::load($wkt, 'ewkt'); // We now a full geoPHP Geometry object | ||
|
||
// Let's re-insert directly into postGIS | ||
$insert_string = $geom->out('ewkt'); | ||
pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', ST_GeomFromEWKT('$insert_string'))"); | ||
} | ||
} | ||
|