Skip to content

Instantly share code, notes, and snippets.

@johnny13
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save johnny13/b052cd9745457fd4372a to your computer and use it in GitHub Desktop.

Select an option

Save johnny13/b052cd9745457fd4372a to your computer and use it in GitHub Desktop.
Lithium Li3 PHP PostgreSQL PostGIS Searching.
<?php
/* EXAMPLE OF HOW TO SETUP ALL YOUR LI3 FILES TO DO LOCATION BASED SEARCHING */
/* CONFIG/BOOTSTRAP/CONNECTIONS */
Connections::add('postpins', array(
'type' => 'database',
'adapter' => 'PostgreSql',
'host' => 'localhost',
'login' => 'postgres_user',
'password' => '',
'database' => 'databasename',
'encoding' => 'UTF-8'
));
/* MyPoints MODEL */
namespace app\models;
class MyPoints extends \lithium\data\Model{
protected $_meta = array(
'connection' => 'postpins'
);
}
/* CONTROLLER */
class ApigeoController extends \lithium\action\Controller {
...
public function index(){
$geospot = "-102.7896242,46.8791756"; //Dickinson North Dakota
$order = 'geom <-> st_setsrid(st_makepoint('.$geospot.'), 4326)';
$results = MyPoints::find('all', array('order' => $order, 'limit' => $limit));
foreach($results as $item){
//do stuff w/ $item
}
.....
}
}
?>
/* ADDITIONAL NOTES */
Li3 PHP Needs your Model to have a Column Named ID and that Autoincrementing ID. On PostGRESQL if you import a shape file,
or some other data source, you will need to add that.
Something like this for a value will autoincrement on ID.
nextval('tablename_gid_seq'::regclass)
You should also notice the search query has hardcoded the number 4326
That is really your EPSG Number. Each Dataset that has a .prj file will give you that.
Here is an online tool for helping you figure out that number: http://prj2epsg.org/search
You would need to figure that out for each table and somehow store and retreive that if you are quering data sets across multiple locations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment