Community Documentation

Code for working with geometry objects

Last updated June 15, 2012. Created by phayes on April 21, 2010.
Edited by batigolix, bmadore. Log in to edit this page.

This code may not work yet, but it should soon!

// First we load the geometry object and use the SQL back-end for processing
$geometry1 = new Geometry(array(
  'handler_type' => 'sql'
));

// Next we feed it the geometry we want to work with
$geometry1 -> setText("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))");

// Let's load another geometry, and load it with X and Y data.
$geometry2 = new Geometry(
  'handler_type' => 'sql'
  'lat' => 23,
  'lon' => 76,
));

// Test whether they intersect
if ($geometry1 -> intersect($geometry2)) {
  drupal_set_message("those geometries intersect!");
 
  // Tell the user about the center of geomtry1
  $lon = $geomtry1->centroid()->getY();
  $lat = $geomtry1->centroid()->getX();
 
  drupal_set_message("The center of this polygon is at: Lat: " . $lat . ", Lon: " . $lon);
}

Some Common Methods

setText - set the data using WKT
getText - get the data in WKT format
setBinary - get the data using WKB
getBinary - set the data using WKB
getX - get the X value (only works for points)
getY - get the Y value (only works for points)

Page status

About this page

Drupal version
Drupal 6.x
Audience
Programmers

Archive

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here