Archive

Posts Tagged ‘GIS’

Calculating Point in Polygon in PHP

September 2nd, 2009 simon No comments

Having recently developed a GIS application which reads data in and identifies the area within which it resides, we struggled to find an existing PHP solution, so developed our own. For those of you who need to calculate whether a location exists within a polygon, here it is:

The polygons are to be defined within the ‘areas’ table:

CREATE TABLE `areas` (
`id` int(8) NOT NULL auto_increment,
`area_code` varchar(10) NOT NULL,
`country` varchar(250) NOT NULL,
`longitude` varchar(25500) NOT NULL,
`latitude` varchar(25500) NOT NULL,
`polyset_id` int(8) NOT NULL default ‘0′,
PRIMARY KEY  (`id`)
)

The class:

<?php
class PointInPolygon
{

/* Point in Polygon
* Copyright Blue Lobster IT 2009
* www.bluelobster.co.uk
*/

//Queries the area table to distinguish which area the point is in.
static function CheckPoint($Longitude, $Latitude)
{
//searches areas table for all areas
$sqlPip = “SELECT * FROM areas”;
$qryPip = mysql_query($sqlPip) or die(mysql_error());
$numPip = mysql_num_rows($qryPip);

//begin process

if ($numPip > 0) {

$return = 0;
for ($i = 0; $i < $numPip; $i++) {
$rsPip = mysql_fetch_array($qryPip);
$arrLon = explode(’,',$rsPip['longitude']);
$arrLat = explode(’,',$rsPip['latitude']);
if (count($arrLon) == count($arrLat)) {
if (PointInPolygon::IsPointInPolygon(count($arrLon), $arrLon, $arrLat, $Longitude, $Latitude) == true) {
$return = $rsPip['id'];
break;
}
}else{
echo(”error: lon/lat irregularities\n\n”);
print_r($arrLon);
print_r($arrLat);
echo(”\n\n”);
}
}
}else{
echo(”fixme: error”);
}
return $return;
}

//$nvert            = Number of vertices in the polygon. Whether to repeat the first vertex at the end is discussed below.
//$vertx, $verty    = Arrays containing the x- and y-coordinates of the polygon’s vertices.
//$testx, $testy    = X- and y-coordinate of the test point.

private static function IsPointInPolygon($nvert, $vertx, $verty, $testx, $testy)
{
$i;
$j;
$c = false;
for ($i = 0, $j = $nvert-1; $i < $nvert; $j = $i++) {
if (
(($verty[$i]>$testy) != ($verty[$j]>$testy)) &&
($testx < ($vertx[$j]-$vertx[$i]) * ($testy-$verty[$i]) / ($verty[$j]-$verty[$i]) + $vertx[$i])
){
$c = !$c;
}
}
return $c;
}
}

?>

Categories: September Tags: , , ,

Where’s the internet going?

August 15th, 2009 simon No comments

From time to time I get invited to do a short presentation to business startups on websites, SEO and the internet in general. I’ve got the hang of this presentation now and I can normally predict the questions that I’ll get asked at the end. Yesterday, however, was different. I was asked a very straight forward question that I should have had an instant answer for but I didn’t: “We’ve been asked to forecast where our businesses will go over the next few years, where do you think the internet is going?”. What a good question! On the drive back from Ipswich to Lowestoft, where I’m based, I had about an hour to think about this.

There is often talk of Web 1.0, 2.0, 3.0, 4+ and people try to categorise stages in the development of the internet into these ‘boxes’. The problem with this is that the internet just doesn’t seem to work that way. Web 2.0, as it’s been called, seemed to miss out the obvious: that significantly wider availability of broadband and accessibility to the internet for all through libraries, educational establishments, airports, cafe’s etc. allowed the internet to become far more media rich. YouTube, BBC iPlayer, Skype style video conferencing, and so on, all became far more accessible and therefore allowed far greater collaboration of information.

So what’s happening right now? Search Engine’s have become soooo much more sophisticated and useful than in the days of Alta Vista which would return absolute rubbish. Cloud Computing is taking it’s early steps, you can now manage email, calendars, to do lists, write documents, spreadsheets, code and so much more without anything more than just a browser – for free. This is definitely a step in the right direction, lighter client computers (netbooks) and more powerful servers and networks. Mobile phone technology is catching up and so are the networks allowing people to conduct their days activities, social media, photos and videos, sharing all while on the move. I was on a boat the other day without my mobile for the first time in about a year and it felt really strange, something was missing.

Social media IS changing the world RIGHT NOW. LinkedIn, Facebook, Twitter, YouTube, MySpace,Ecademy, Bebo, Second Life and many more are shaping the way we communicate with each other. Students leave university with over a 1000 contacts on Facebook, all of whom will get jobs and communicate with each other and collaborate for years to come.

I’ve been working on a large data ‘mashup’ project on the internet for the last few months which is now set to adopt a phenomenal amount of data, processing it for use in real time: in charts, data exports to other platforms, GIS (Geographical Information System) export to view geospacial information in Google Earth and other GIS browsers. There are a number of similar projects being conducted throught the world because the infrastructure and development technologies now exist to develop and deploy these systems in a relatively short amount of time.

So what is next for the internet? Hopefully more bandwidth to allow us to do more of the above in far more creative ways! What do you think is next?

Categories: August 2009 Tags: , ,