Archive

Posts Tagged ‘PHP’

Updated Metrostop.org

March 18th, 2006

It took much longer then I had expected, between work actually picking up, running into a few unexpected hickups with IE and learning Rails, I finally have a much improved metrostop.org.
This was almost a complete rewrite from my previous PHP.

The list of improvements include:

* Scalable Map - resizes with the browser width/height
* Back Button Support - after searching clicking the back button will return to the previous state
* Caching - I use a lot more caching now so performance should be much better

Updates: (things I forgot to mention)
* Entrance Level data
* Search Certainty raiting stars
* Nearly Validating markup all but the MS specific namespace validate

Software , , ,

Languages, Frameworks, and a problem to solve

January 30th, 2006

Some note worthy quotes:
source

And of course, who can ignore Rails? The backlash from heavyweight web frameworks has been significant. We now know that EJB 1 & 2 were based on an entirely flawed set of use cases. Because of the damage this (still slowly dawning) realization has wrought to Sun’s reputation, it’s hard to know whether EJB3, which probably should have been called something else to disassociate it with the failures of its predecessors, will succeed, despite the fact that EJB3 is like a breath of fresh air. You look at the code and it makes sense; no bizzarre and obscure interfaces and concepts to puzzle over while thinking, “I wonder why I have to do this? Well, these guys are clearly smarter than I am.” (I tried to understand EJB1, but when I first heard that entity beans didn’t actually work, my brain refused to let me invest any more time, which turned out to be the right choice). As a result of all this, someone said “hey, all I want to do is create a database and use it from the web. Why should I do all that work?” As it turns out, such activities seem to be about 90% of all we ever do in “Enterprise” programming, and EJB 1/2 were solving an entirely different problem, and making the 90% incredibly difficult in the process. Thus, the Rails approach of “just connect the database to the web.”

This one reflects my current experiences, in learning zope, using php, and now rails

My own experience in web frameworks was with Zope. In an interesting parallel with EJB3, Zope is now on version 3 which is a from-the-ground-up redesign, and everything I’ve seen of it indicates that, like EJB3, it represents a great deal of rethinking of the problem. I’ve been bumping up against the problem of “but all I want to do is connect a database to the web” in Zope2 for several years now. Oh, it’s definitely something you can do, but unfortunately it’s past the knee of the “Z-shaped learning curve,” and is only trivial if you live and breathe Zope every day. Don’t get me wrong; Zope is an excellent system and incredibly powerful, and Zope3 may be much easier, but I’m out of steam. I have realized that on my site, I really just want to do a collection of simple things, and I don’t want to relearn Zope every time I want to accomplish something. So — sorry, Jim (Fulton, not Kirk) — I’m going to find something drop-dead simple to solve my drop-dead simple problems. Probably PHP5, which actually includes most of Java and C++ syntax, amazingly enough, and I wonder if that isn’t what made IBM adopt it.

Update:
I wanted to add, I don’t really see the point in arguing for or against python/ruby. I think they are pretty comparable languages; So, it really comes down to preference and task. Right now there is rails, so I’m using ruby. What I like about ruby in this respect, is it has block structure just like Javascript, but really something like PHP is even better in this respect because it uses the same kind of block structure (e.g. {} vs begin end).

Software , , , , , ,

Metrostop Back Up

January 10th, 2006

Fixing things was actually really easy. JSON is really nice.

On the server I had to update the regular expression to extract the JSON. I’m actually thinking about reworking this further to support the back button. I’ll load the google data into an iframe. Adding a function to the parent window that the iframe calls. This way onunload I can control the unwind and onload the forward is already encoded.

For now regex:

header('Content-Type: text/javascript');
$address = strip_tags($_GET["address"]);
$q = urlencode( $address );
$query = 'http://maps.google.com/maps?q=' . $q . '&output=js';
$json = trim( file_get_contents( $query ) );
$json = preg_replace( "/.*html *.*CDATA\[/", "", $json );
$json = preg_replace( "/function onLoad\(\) {window.parent.loadVPage\(/", "", $json );
$json = preg_replace( "/,document.getElementById\(\"state\"\)\);}\/\/\]\]>.*/", "", $json );
echo $json;

and on the client:

var data = window.eval( "(" + txt + ")" );
var point =  new GPoint( data.overlays.markers[0].lng, data.overlays.markers[0].lat );

Software , ,

Hacking Google Maps Geocoding

October 26th, 2005

Tonight I was experimenting with google maps and geocoding. It turns out it’s not too difficult to extract geocoordinates from google maps. Basically, the key is to add the extra query parameter ouput=js

Once you do that google outputs the response as javascript with a nice xml document embedded within. Using a few regular expressions to extract the xml and we have some very sophisticated results from our friends google.

The following is a very simple example, in php that searches for a location near DC. What someone might want to do with this little trickier and googles nice javascript api is beyond me ;-)

header('Content-Type: text/xml');
 
$address = strip_tags($_GET["address"]);
$query = 'http://maps.google.com/maps?q=' .
urlencode( $address ) .
'&sll=38.880660,-77.114642&sspn=0.023753,0.039439&hl=en' . // near DC
'&output=js';
 
$gm = fopen( $query, 'r' );
if( !$gm ){
return;
}
$buffer = "";
while( !feof( $gm ) ){
$buffer .= fgets($gm, 4096);
}
fclose($gm);
 
// correct end tags
// "/\\\//\//g"
$buffer = preg_replace( "/<\/\\//", "</", $buffer );
 
// extract the xml, removing all html and most of the javascript
preg_match("/var vpage = '*.*';/", $buffer, $matches );
if( count($matches) == 0 )
return;
 
$buffer = $matches[0];
// strip out the remaining javascript
$buffer = preg_replace( "/var vpage = '/", "", $buffer );
$buffer = preg_replace( "/';/", "", $buffer );
 
// load the xml into a dom tree
$doc = DOMDocument::loadXML( $buffer );
 
// extract what we want from the xml
echo "<?xml version='1.0'?>";
$locs = $doc->getElementsByTagName( "locations" );
echo $doc->saveXML($locs->item(0)) . "\n";

Update

This no longer works.  Google started returning JSON.  Also, google now has GeoCoding built in to their API.

Software , , ,

Metro Stop

October 15th, 2005

Alright I got a real domain for my metro maps project and even can say it supports all the major browsers now, including IE.

The main problem I was having in IE was caused by my lack of understanding how VML works in the browser. Reading the maps FAQ really helped clear things up.

The fun stuff is in showing metro alerts in the upper right hand corner. I setup my server to check the metro RSS feed once every hour. The php for this is really straight forward RSS using PHP DOM calls.

source: metro3.tar.gz

I hope to get back to XUL coding soon, but for the time being it looks like my work is going to force me into writing cross browser code.

Software , , , ,

Updated Maps

October 8th, 2005

Made a few improvements to the metro maps.

1. Works in Opera and Safari, still no IE.
2. Added a Search for locating the nearests Metro stop, given an address.

How it works:

To go from address to longitude/latitude, I used maps.google.com.

$query = 'http://maps.google.com/maps?q=' . $q . 
              '&amp;output=js';
$gm = fopen( $query, 'r' );
if( !$gm )
  return false;
$tmp = @fread($gm,30000);
fclose($gm);
$x = preg_replace ('/.* = 2 ){
  $lat = $ret[0];
  $long = $ret[1];
}

I learned about this technique while reading about geocoding here.

The algorithm for computing distances between longitude and latitude came from this link.

The rest of how it works is pretty straight forward - loop over all points, popup the mark at the end.

Update: Did I meantion - I’m in Barcelona!

Software , , , ,

Metroized Google Maps

September 29th, 2005

D.C. Metro maps with Google Maps API for that extra touch of spice! ;-)

To build this map I first scrapped wmata using the scrap.php file to build a first pass of the stations.xml file. About 12 stations addresses did not resolve to valid addresses when back referencing maps.google.com. I had to manually figure out the lng lat for each of those.

The next thing that I had to do manually was enter each of the linked tags into the stations.xml file. This was really the only sucky part, but vi and split screen made it pass quickly.

I noticed the ajax part is busted in IE/Opera/Safari. Maybe I’ll fix that, but I suspect the DOM walking would be busted in IE anyway so I’m not sure I’ll bother, of course it would be nice if it worked everywhere ;-)

Code: http://xullicious.com/metro.tar.gz

Software , , , ,