Author Topic: Turning a map color into a value- more fun with PHP...  (Read 3095 times)

0 Members and 1 Guest are viewing this topic.

Offline SLOweather

  • Global Moderator
  • Forecaster
  • *****
  • Posts: 3456
    • Weatherelement Moline IL
Turning a map color into a value- more fun with PHP...
« on: August 01, 2007, 06:13:39 PM »
I'm throwing this script out not as a finished product (although it does it's specific job well so far), but more as food for other ideas for similar scripts. It's certainly not a polished as other scripts, (and there are probably better ways to do some things) but I think there are enough comments in it for anyone with a smackerel of PHP to follow along and modify it as desired.

It includes some commented out testing code. Post any other ideas you come up with for such a script.

I've been working on some fire weather page for a client, and I haven't been able to find a text source of the Haines Index values, which is "used to indicate the potential for wildfire growth by measuring the stability and dryness of the air over a fire."

It's also pretty hard to find the right data easily to calculate it locally.

But that link above has a link to this map:



I did a little research and found that PHP has an imagecolorat command that will output the decimal color value of a pixel in an image.

So I wrote this script to get the color value and then turn it back into a Haines number and adjective. It's not perfect. I hard-coded the color values, so I'm screwed if they change them at all. I did find out that the colors on the map today don't exactly match the ones in the legend. I'll have to test it for a while to see if the colors change slightly from day to day.

The pixel this version checks is just about at the middle of this part of the image:




To see the simple output, it's currently available to run at http://fire.cambriacsd.org/fire/haines.php unless I'm playing with it or broke it.

<?php
// Second alpha release 070801
// This SLOweatherscript loads a color coded map, such as the one at
// http://www.fs.fed.us/land/wfas/haines.gif, reads the color at a
// specified pixel, and converts that color value back into a text value.

// The base routine will work on jpgs and png with no problem. However, GIFs
// apparently need to be upsampled before checking the color value.

// To modify this for use other than the Haines Index GIF, load the desired
// image into an editing program and determine the X and Y coordinates of the
// pixel who's color you want the value of. Then, run this script on other known
// color locations on the image to determine the values of different colors.

// Jpgs may take a little more work, due to the jpeg color artifacts present
// around and in colored areas causing the integer color value to shift.

// There are lots of other ways to used the recovered color info, but this
// version only compares it to 5 known colors.

// Create a blank true color image the same size as the GIF.
$dimg=imagecreatetruecolor(782,580);
// Get the image.
$simg = imagecreatefromgif("http://www.fs.fed.us/land/wfas/haines.gif");

// Upsample the GIF to the true color blank.
imagecopy($dimg,$simg,0,0,0,0,782,580);

// The script will fail if either the X or Y value for the location is
// less than 0, or greater than the corresponding size value of the image
// in the line above. Enter the pixel location X and Y  below.

$rgb = imagecolorat($dimg, 48, 300);

// Get the decimal values of each component color
// $r = ($rgb >> 16) & 0xFF;
// $g = ($rgb >> Cool & 0xFF;
// $b = $rgb & 0xFF;

// Destroy the images
imagedestroy($simg);
imagedestroy($dimg);

// This is for testing and to determine the color
// values.
// echo $rgb;


//echo $r;


//echo $g;


//echo $b

// Define the Haines GIF colors

$black= "0";
$blue ="65535";
$dkgreen ="3912561";
$green = "65280";
$yellow = "16776960";
$orange = "16766720";
$red = "16737094";

//Sort out the Haines Index

if ($rgb == $dkgreen) {
$hainestext = "Very Low";
$hainesvalue= "2";

} elseif ($rgb == $green) {
$hainestext = "Very Low";
$hainesvalue= "3";

} elseif ($rgb == $yellow) {
$hainestext = "Low";
$hainesvalue= "4";

} elseif ($rgb == $orange) {
$hainestext = "Moderate";
$hainesvalue= "5";

} elseif ($rgb == $red) {
$hainestext = "High";
$hainesvalue= "6";

} elseif ($rgb == $blue) {
$hainestext = "Water";
$hainesvalue= "-";

} elseif ($rgb == $black) {
$hainestext = "Text or Border";
$hainesvalue= "-";

} else

{
$hainestext = "Unknown";
$hainesvalue= "-";
}

// Output the value and text

echo $hainesvalue, " ";

echo "$hainestext";

?>

Offline Anole

  • Forecaster
  • *****
  • Posts: 585
    • http://pineislandweather.com
Re: Turning a map color into a value- more fun with PHP...
« Reply #1 on: August 01, 2007, 07:24:44 PM »
Now that could be really useful! I"ve always wanted to play around with imagecolorat but haven't taken the time.

Offline SLOweather

  • Global Moderator
  • Forecaster
  • *****
  • Posts: 3456
    • Weatherelement Moline IL
Re: Turning a map color into a value- more fun with PHP...
« Reply #2 on: August 01, 2007, 09:01:58 PM »
Quote from: "Anole"
Now that could be really useful! I"ve always wanted to play around with imagecolorat but haven't taken the time.


Wow! High praise indeed from the keeper of the Weather Graphics script! Thanks!

I'll be interested to hear what you come up with. It'll be a lot more useful when I or someone else figures out an algorithm that determines a color is "close enough" rather than exact. Then it'll open up the ability to examine things like webcams or satellite or radar images.

Offline carterlake

  • Senior Contributor
  • ****
  • Posts: 243
    • CarterLake.org
Re: Turning a map color into a value- more fun with PHP...
« Reply #3 on: August 26, 2007, 02:33:23 PM »
Very clever idea. 8)

Worth a bump.

Davis VP2 6153; Weather Display (LIVE w/ Ajax); Quickcam for Notebooks Pro; Boltek w/ Nexstorm; GRLevel3; live NOAA Radio

 

anything