function geophp_sane() { $sane = true; if (!geoPHP::geosInstalled()) { $msg = "geoPHP needs the GEOS and its PHP extension (please download it at: http://trac.osgeo.org/geos/)"; $sane = false; } else { // Is the Digital Methods lab in Amsterdam? $point_lng = 4.893346; $point_lat = 52.369042; $sw_lng = 4.76852; $sw_lat = 52.321629; $ne_lng = 5.01727; $ne_lat = 52.425129; $sane = coordinatesInsideBoundingBox($point_lng, $point_lat, $sw_lng, $sw_lat, $ne_lng, $ne_lat); if (!$sane) { $msg = "geoPHP/GEOS library seems broken. searching on area will not work"; } } if (!$sane) { if (defined('CAPTURE')) { logit(CAPTURE . ".error.log", $msg); } else { logit("cli", $msg); } } return $sane; }
static function geosToGeometry($geos) { if (!geoPHP::geosInstalled()) { return NULL; } $wkb_writer = new GEOSWKBWriter(); $wkb = $wkb_writer->writeHEX($geos); $geometry = geoPHP::load($wkb, 'wkb', TRUE); if ($geometry) { $geometry->setGeos($geos); return $geometry; } }
/** * Serialize geometries into a WKT string. * * @param Geometry $geometry * * @return string The WKT string representation of the input geometries */ public function write(Geometry $geometry) { // If geos is installed, then we take a shortcut and let it write the WKT if (geoPHP::geosInstalled()) { $writer = new GEOSWKTWriter(); return $writer->write($geometry->geos()); } $type = strtolower($geometry->geometryType()); if (is_null($data = $this->extract($geometry))) { return null; } return strtoupper($type) . ' (' . $data . ')'; }
function testAdapters() { foreach (scandir('./input', SCANDIR_SORT_NONE) as $file) { $parts = explode('.', $file); if ($parts[0]) { $format = $parts[1]; $input = file_get_contents('./input/' . $file); echo "\nloading: " . $file . " for format: " . $format; $geometry = geoPHP::load($input, $format); // Test adapter output and input. Do a round-trip and re-test foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test $output = $geometry->out($adapter_key); $this->assertNotNull($output, "Empty output on " . $adapter_key); if ($output) { $adapter_loader = new $adapter_class(); $test_geom_1 = $adapter_loader->read($output); $test_geom_2 = $adapter_loader->read($test_geom_1->out($adapter_key)); $this->assertEquals($test_geom_1->out('wkt'), $test_geom_2->out('wkt'), "Mismatched adapter output in " . $adapter_class . ' (test file: ' . $file . ')'); } } } // Test to make sure adapter work the same wether GEOS is ON or OFF // Cannot test methods if GEOS is not intstalled if (!geoPHP::geosInstalled()) { return; } foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test // Turn GEOS on geoPHP::geosInstalled(TRUE); $output = $geometry->out($adapter_key); if ($output) { $adapter_loader = new $adapter_class(); $test_geom_1 = $adapter_loader->read($output); // Turn GEOS off geoPHP::geosInstalled(FALSE); $test_geom_2 = $adapter_loader->read($output); // Turn GEOS back On geoPHP::geosInstalled(TRUE); // Check to make sure a both are the same with geos and without $this->assertEquals($test_geom_1->out('wkt'), $test_geom_2->out('wkt'), "Mismatched adapter output between GEOS and NORM in " . $adapter_class . ' (test file: ' . $file . ')'); } } } } } }
function testGeos() { if (!geoPHP::geosInstalled()) { echo "Skipping GEOS -- not installed"; return; } foreach (scandir('./input') as $file) { $parts = explode('.', $file); if ($parts[0]) { $format = $parts[1]; $value = file_get_contents('./input/' . $file); echo "\nloading: " . $file . " for format: " . $format; $geometry = geoPHP::load($value, $format); $geosMethods = array(array('name' => 'geos'), array('name' => 'setGeos', 'argument' => $geometry->geos()), array('name' => 'PointOnSurface'), array('name' => 'equals', 'argument' => $geometry), array('name' => 'equalsExact', 'argument' => $geometry), array('name' => 'relate', 'argument' => $geometry), array('name' => 'checkValidity'), array('name' => 'isSimple'), array('name' => 'buffer', 'argument' => '10'), array('name' => 'intersection', 'argument' => $geometry), array('name' => 'convexHull'), array('name' => 'difference', 'argument' => $geometry), array('name' => 'symDifference', 'argument' => $geometry), array('name' => 'union', 'argument' => $geometry), array('name' => 'simplify', 'argument' => '0'), array('name' => 'disjoint', 'argument' => $geometry), array('name' => 'touches', 'argument' => $geometry), array('name' => 'intersects', 'argument' => $geometry), array('name' => 'crosses', 'argument' => $geometry), array('name' => 'within', 'argument' => $geometry), array('name' => 'contains', 'argument' => $geometry), array('name' => 'overlaps', 'argument' => $geometry), array('name' => 'covers', 'argument' => $geometry), array('name' => 'coveredBy', 'argument' => $geometry), array('name' => 'distance', 'argument' => $geometry), array('name' => 'hausdorffDistance', 'argument' => $geometry)); foreach ($geosMethods as $method) { $argument = NULL; $method_name = $method['name']; if (isset($method['argument'])) { $argument = $method['argument']; } switch ($method_name) { case 'isSimple': case 'equals': case 'geos': if ($geometry->geometryType() == 'Point') { $this->assertNotNull($geometry->{$method_name}($argument), 'Failed on ' . $method_name . ' (test file: ' . $file . ')'); } if ($geometry->geometryType() == 'LineString') { $this->assertNotNull($geometry->{$method_name}($argument), 'Failed on ' . $method_name . ' (test file: ' . $file . ')'); } if ($geometry->geometryType() == 'MultiLineString') { $this->assertNotNull($geometry->{$method_name}($argument), 'Failed on ' . $method_name . ' (test file: ' . $file . ')'); } break; default: if ($geometry->geometryType() == 'Point') { $this->assertNotNull($geometry->{$method_name}($argument), 'Failed on ' . $method_name . ' (test file: ' . $file . ')'); } if ($geometry->geometryType() == 'LineString') { $this->assertNotNull($geometry->{$method_name}($argument), 'Failed on ' . $method_name . ' (test file: ' . $file . ')'); } if ($geometry->geometryType() == 'MultiLineString') { $this->assertNull($geometry->{$method_name}($argument), 'Failed on ' . $method_name . ' (test file: ' . $file . ')'); } } } } } }
/** * */ public function save(Request $request) { $out = ['data' => $request->input()]; if (!$request->has('id')) { $location = new \App\Models\Location(); $geojsonLocation = new \App\Models\GeojsonLocation(); } else { $location = \App\Models\Location::find($request->input('id')); $geojsonLocation = \App\Models\GeojsonLocation::find($request->input('id')); } $location->name = $request->input('name'); $location->description = $request->input('description'); // get the center $geometry = $request->input('geometryWkt'); $polygon = \geoPHP::load($geometry); $polygon->setSRID(4326); $srid = $polygon->SRID(); $geos = \geoPHP::geosInstalled(); $valid = $polygon->checkValidity(); $centroid = $polygon->getCentroid(); $area = $polygon->getArea(); $location->center = \DB::raw("ST_SetSRID(ST_PointFromText('POINT(" . $centroid->getX() . ' ' . $centroid->getY() . ")'), 4326)"); $location->bounds = \DB::raw("ST_SetSRID(ST_PolygonFromText('" . $geometry . "'), 4326)"); $location->area = $area; $location->save(); // save it in the mongo model too... $geojsonLocation->name = $request->input('name'); $geojsonLocation->description = $request->input('description'); $geojsonLocation->area = $area; // save the center as GeoJSON $centroidWkt = \geoPHP::load('POINT(' . $centroid->getX() . ' ' . $centroid->getY() . ')', 'wkt'); $geojsonLocation->center = json_decode($centroidWkt->out('json')); // save the geometry as geoJSON $geojsonLocation->bounds = json_decode($polygon->out('json')); $geojsonLocation->save(); $out['data']['location'] = $location; $out['data']['geojsonLocation'] = $geojsonLocation; return Response::json($out); }
/** * Serialize geometries into a WKT string. * * @param Geometry $geometry * * @return string The WKT string representation of the input geometries */ public function write(Geometry $geometry) { // If geos is installed, then we take a shortcut and let it write the WKT if (geoPHP::geosInstalled()) { $writer = new GEOSWKTWriter(); $writer->setTrim(TRUE); return $writer->write($geometry->geos()); } if ($geometry->isEmpty()) { return strtoupper($geometry->geometryType()) . ' EMPTY'; } else { if ($data = $this->extractData($geometry)) { return strtoupper($geometry->geometryType()) . ' (' . $data . ')'; } } }
public function geos() { // If it's already been set, just return it if ($this->geos && geoPHP::geosInstalled()) { return $this->geos; } // It hasn't been set yet, generate it if (geoPHP::geosInstalled()) { $reader = new GEOSWKBReader(); $this->geos = $reader->readHEX($this->out('wkb', TRUE)); } else { $this->geos = FALSE; } return $this->geos; }
function _methods_tester_with_geos($geometry) { // Cannot test methods if GEOS is not intstalled if (!geoPHP::geosInstalled()) { return; } $methods = array('envelope', 'getBBox', 'x', 'y', 'startPoint', 'endPoint', 'isRing', 'isClosed', 'numPoints'); foreach ($methods as $method) { // Turn GEOS on geoPHP::geosInstalled(TRUE); $geos_result = $geometry->{$method}(); // Turn GEOS off geoPHP::geosInstalled(FALSE); $norm_result = $geometry->{$method}(); // Turn GEOS back On geoPHP::geosInstalled(TRUE); $geos_type = gettype($geos_result); $norm_type = gettype($norm_result); if ($geos_type != $norm_type) { $this->fail('Type mismatch on ' . $method); $this->dump($geos_type); $this->dump($norm_type); continue; } // Now check base on type if ($geos_type == 'object') { $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'), 'wkt')); // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance // Using Pythagorean theorem $bb = $geos_result->getBBox(); $scale = sqrt(($bb['maxy'] - $bb['miny'] ^ 2) + ($bb['maxx'] - $bb['minx'] ^ 2)); // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units if ($haus_dist / $scale > 0.5) { $this->fail('Output mismatch on ' . $method); $this->dump('GEOS : '); $this->dump($geos_result->out('wkt')); $this->dump('NORM : '); $this->dump($norm_result->out('wkt')); continue; } } if ($geos_type == 'boolean' || $geos_type == 'string') { if ($geos_result !== $norm_result) { $this->fail('Output mismatch on ' . $method); $this->dump('GEOS : '); $this->dump((string) $geos_result); $this->dump('NORM : '); $this->dump((string) $norm_result); continue; } } //@@TODO: Run tests for output of types arrays and float //@@TODO: centroid function is non-compliant for collections and strings } }