Zend Framework Demo app - feed reader

I have written a small demo app for those of you looking for a quick start with Zend Framework.

You can view, and download the demo at:
http://andries.systray.be/zf-demos/feed-reader/

The download package contains:
- Zend Framework 1.0.0
- A conventional directory structure
- A bootstrap file
- An index controller (welcome page)
- An error controller (404, and other application errors)
- A feed controller, to read RSS feeds
- A default configuration file, with 2 stages (production, staging)

Feel free to use it :)

Update:

  • Tested with Zend Framework 1.0.1
  • Improved error controller now available
  • Usage of $this->_request->isPost()
  • Added default feeds to play with
  • Improved inline documentation
  • Removed usage of setNoViewRenderer()

Comments/feedback are more than welcome!

Calculating a distance between 2 points in PHP

I feel pretty confident that Rob (Akrabat.com) will not be writing about the same topic this time. Anyway, in this post I’ll explain you how to calculate a distance between 2 points in PHP

Because of the near-spherical shape of the Earth, calculating an accurate distance between two points requires the use of spherical geometry [1], and trigonometric math functions. For many applications, an approximate distance calculation provides sufficient accuracy with much less complexity.

To calculate an approximate distance in miles, we could do:

sqrt(x * x + y * y)

where:x = 69.1 * (lat2 - lat1) and y = 53.0 * (lon2 - lon1)

We can improve the accuracy, by adding the cosine math function:

sqrt(x * x + y * y)

where: x = 69.1 * (lat2 - lat1) and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)

If you need greater accuracy, you can use the Great Circle Distance Formula [2]. This formula requires use of spherical geometry, and a high level of floating point mathematical accuracy - about 15 digits of accuracy (double-precision).

To convert latitude or longitude from decimal degrees to radians, we can divide the latitude and longitude values by 180/pi, or approximately 57.29577951. The radius of the earth is assumed to be 6,378.8 kilometers, or 3,963.0 miles.

Since we are using PHP, it’s much simpler, because the deg2rad() function does this calculation for us.

If you convert all latitude and longitude values to radians before the calculation, we can use this equation:

3963.0 * arccos[sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)]

Implementing this in PHP

Sorry, I am European so I talk kilometers instead of miles, and therefore I will also implement the example with kilometers:

function getDistance($latitudeFrom, $longitudeFrom,
    $latituteTo, $longitudeTo)
{
    // 1 degree equals 0.017453292519943 radius
    $degreeRadius = deg2rad(1);
 
    // convert longitude and latitude values
    // to radians before calculation
    $latitudeFrom  *= $degreeRadius;
    $longitudeFrom *= $degreeRadius;
    $latituteTo    *= $degreeRadius;
    $longitudeTo   *= $degreeRadius;
 
    // apply the Great Circle Distance Formula
    $d = sin($latitudeFrom) * sin($latituteTo) + cos($latitudeFrom)
       * cos($latituteTo) * cos($longitudeFrom - $longitudeTo);
 
    return (6371.0 * acos($d));
}

Resources

[1] http://en.wikipedia.org/wiki/Spherical_geometry
[2] http://en.wikipedia.org/wiki/Great-circle_distance

Update:
See comments below if you want to implement the calculation using miles instead of kilometers.

Zend Framework, just get me started, okay?

If you are looking for a quick way to get started with the Zend Framework, then you should download this file. This download has a default setup for the Zend Framework, including:

  • A conventional directory structure
  • A bootstrap file
  • A configuration file with 2 stages: production, staging
  • An error controller
  • An index controller
  • etc

The only thing you need to do (if you don’t have a shared version of the Zend Framework) is to download the framework, extract it, and put the library/Zend folder, into the same place in the downloadable file.

A quick and dirty test should tell you if it works: just open
http://localhost/<foldername>/document_root/

You should see “hooray application is running.” if everything went fine.

Update:

I have also made a modular starter package available. You can download it here.

More updates:
Naneau explains a modular setup in more details here.