punkish

macrostrat

Jul 17th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.67 KB | None | 0 0
  1. package macrostrat;
  2. use Dancer ':syntax';
  3. use Dancer::Plugin::REST;
  4. use DBI;
  5.  
  6. prepare_serializer_for_format;
  7.  
  8. our $VERSION = '0.1';
  9.  
  10. my $db = config->{'database'};
  11. my $hn = config->{'hostname'} . ':mysql_socket=/var/mysql/mysql.sock';
  12. my $un = config->{'username'};
  13. my $pw = config->{'password'};
  14. my $pn = 3306;
  15.  
  16. my $dbh = DBI->connect("DBI:mysql:database=$db;host=$hn;port=$pn","$un","$pw");
  17.  
  18. get '/' => sub {
  19.     template 'index';
  20. };
  21.  
  22. #curl http://localhost:3000/poly.json?BBOX=-87%2033,%20-85%2033,%20-85%2035,%20-87%2035,%20-87%2033
  23. get '/points.:format' => sub {
  24.     my $bbox = params->{'BBOX'};
  25.     my $callback = params->{'callback'};
  26.     my $res = $callback . '(' . to_json(points_within_bbox($bbox)) . ')';
  27.     return $res;
  28. };
  29.  
  30. get '/polys.:format' => sub {
  31.     my $bbox = params->{'BBOX'};
  32.     my $callback = params->{'callback'};
  33.     my $res = $callback . '(' . to_json(polys_within_bbox($bbox)) . ')';
  34.     return $res;
  35. };
  36.  
  37. true;
  38.  
  39. sub points_within_bbox {
  40.     my ($bbox) = @_;
  41.    
  42.     my $sql = qq{
  43.         SELECT id, col_group_id, lat, lng, col_name  
  44.         FROM cols
  45.         WHERE status_code='active' AND MBRContains(GeomFromText('POLYGON(($bbox))'), coordinate)
  46.     };
  47.    
  48.     my $sth = $dbh->prepare($sql, { async => 1 });
  49.     $sth->execute();
  50.     my $res = $sth->fetchall_arrayref({});
  51.     return $res;
  52. }
  53.  
  54. sub polys_within_bbox {
  55.     my ($bbox) = @_;
  56.    
  57.     my $sql = qq{
  58.         SELECT col_group_id, col_name, gmap
  59.         FROM cols JOIN col_areas ON cols.id = col_areas.col_id
  60.         WHERE status_code='active' AND MBRContains(GeomFromText('POLYGON(($bbox))'), coordinate)
  61.     };
  62.    
  63.     my $sth = $dbh->prepare($sql, { async => 1 });
  64.     $sth->execute();
  65.     my $res = $sth->fetchall_arrayref({});
  66.     return $res;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment