Click here to Skip to main content
Licence CPOL
First Posted 21 Jun 2010
Views 24,491
Downloads 849
Bookmarked 39 times

GHeat .NET

By Corey Fournier | 21 Jun 2010
gheat ported to C#
   5.00 (11 votes)

1

2

3

4
11 votes, 100.0%
5
5.00/5 - 11 votes
μ 5.00, σa 1.10 [?]
 
SampleWebPage.PNG - Click to enlarge image

Introduction

This is a fast and free heat mapping tool.

Background

I had the need for a flexible and scalable heat mapping application. A quick Google search lead me to gheat. While playing with the application, I found it slow and not flexible enough to suit my needs. There was no way to quickly and easily add points and the tile generating was slow. So I decided to attempt a port to C# and here I am now.

Using the Code

For simplicity, I have already written all of the code to get anyone started right out of the box.

UI (In VB)

I have made a session wrapper to store the points for each session. They are accessed through "SessionHandler.PointManager".

Map.aspx contains the code that adds the points to the map.

Dim pm As gheat.PointManager
pm = SessionHandler.PointManager
If pm.PointCount = 0 Then pm.LoadPointsFromFile("points.txt")

To add points dynamically, it is as simple as calling the AddPoint which is in the class PointManager.

pm.AddPoint(New GMap.NET.PointLatLng(30.123866, -92.070673))

If you wish to have to add points via post, I would suggest have a loop that adds each point. Below is an example:

'Post data would look like x,y|x,y
Dim lineSplit() As String
For Each line As String In Request("Address").Split("|")
lineSplit = line.Split(",")
pm.AddPoint(New GMap.NET.PointLatLng(lineSplit(0), lineSplit(1)))
Next

Displaying the Tiles

Tile.aspx contains the code necessary to display the tiles.

Dim image As System.Drawing.Bitmap
Dim stream As New System.IO.MemoryStream()
'Make sure nothing else is being sent
Response.Clear()
'Set the content type
Response.ContentType = "image/png"
'Get the tile image
image = gheat.GHeat.GetTile(SessionHandler.PointManager, _
Request("colorScheme"), CInt(Request("zoom")), CInt(Request("x")), CInt(Request("y")))

'Must save the image to a stream because png's need a stream that can seek back and forth.
image.Save(DirectCast(stream, System.IO.Stream), System.Drawing.Imaging.ImageFormat.Png)
stream.WriteTo(Response.OutputStream)
'Don't do anything else and send it to the requester
Response.Flush()

The Guts (In C#)

Internally the library Gheat.Net is about 99% identical to the python version. It supports the same color schemes, dots, file structure, and points files. The organization is slightly different in an attempt to streamline and make it more efficient. All of the tiles are generated as needed, but all empty tiles are cached and done so per color scheme, per zoom. Other caching was done for the color scheme and dots, so any additions will not be picked up unless the site is restarted. There is no config file but it is necessary to specify the directory where the dots and color schemes are. This MUST be done prior to any other GHeat code execution, so for web apps it must be done on Application_Start.

   Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim baseDirectory As String = HttpRuntime.AppDomainAppPath

        gheat.Settings.BaseDirectory = baseDirectory + "__\etc\"
    End Sub

How Does It Work? (The Idea)

  • Points are drawn onto tiles, the more points the more intense the color (Darker).
  • Based on the intensity of the color (darkness) in the tile on a scale of 0-255, it is mapped to the color scheme which is 255 pixels high.
  • Pixel by pixel is mapped and changed based on the color scheme
  • The tile is trimmed due to it having to be larger to accommodate points on the edges of a tile.
  • Tile is sent out.
  • Everyone now smiles. ;)

Bonus

While testing, I created a small winform app that goes though a bunch of tiles and stitches them together. This is located in the GheatDeskTop app.

TilesStiched.png
Dim pm As New gheat.PointManager()
Dim g As Graphics
Dim tempImage As System.Drawing.Bitmap
Dim zoom As Integer = 4
Dim startX As Integer = 2
Dim startY As Integer = 5
Dim maxX As Integer = startX + 10
Dim maxY As Integer = startY + 10
Dim canvasImage As New System.Drawing.Bitmap(maxX * 256 - (startX * 256), _
maxY * 256 - (startY * 256), System.Drawing.Imaging.PixelFormat.Format32bppArgb)

gheat.Settings.BaseDirectory = "..\..\..\gheatWeb\__\etc\"

g = Graphics.FromImage(canvasImage)

pm.LoadPointsFromFile("..\..\..\points.txt")

For x As Integer = startX To maxX
    For y As Integer = startY To maxY
        tempImage = gheat.GHeat.GetTile(pm, "classic", zoom, x, y)
        g.DrawImage(tempImage, New System.Drawing.PointF(x * 256 - _
		(startX * 256), y * 256 - (startY * 256)))
    Next
Next
PictureBox1.Image = canvasImage

Thanks

Thanks to the original gheat project, I would never have thought of how to implement it without the source code.

Image multiplication via a blending images project.

Mercator projection thanks to Great Maps. I would have included the source, but it was extremely large so I just compiled a DLL and included it with the project. I did modify the source slightly to allow for weighted points. Maybe I will one day explain how that works.

History

  • 21st June, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Corey Fournier

Software Developer

United States United States

Member
Graduate of University of Louisiana at Lafayette in computer science.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionBackground transparency PinmemberManuel Pazos6hrs 28mins ago 
AnswerRe: Background transparency PinmemberCorey Fournier3hrs 13mins ago 
QuestionlogScale Pinmembercdoger12:24 22 Dec '11  
AnswerRe: logScale PinmemberCorey Fournier16:22 22 Dec '11  
QuestionWeight Intensity PinmemberDemig0d12:41 25 Oct '11  
AnswerRe: Weight Intensity PinmemberCorey Fournier8:34 1 Nov '11  
GeneralParameter for dotsize PinmemberMember 78938397:57 24 May '11  
GeneralRe: Parameter for dotsize PinmemberCorey Fournier8:13 24 May '11  
QuestionGenerate colors based on weight range Pinmembermustafaa turab ali10:38 29 Mar '11  
AnswerRe: Generate colors based on weight range PinmemberCorey Fournier10:49 29 Mar '11  
GeneralRe: Generate colors based on weight range Pinmembermustafaa turab ali19:03 29 Mar '11  
GeneralRe: Generate colors based on weight range PinmemberCorey Fournier4:08 30 Mar '11  
GeneralRemove opacity change on zoom Pinmembermustafaa turab ali0:39 28 Mar '11  
GeneralRe: Remove opacity change on zoom PinmemberCorey Fournier8:15 28 Mar '11  
GeneralUsing Gheat for making an offline heat map PinmemberLeonard Anghelesuc7:07 29 Dec '10  
GeneralRe: Using Gheat for making an offline heat map PinmemberCorey Fournier7:14 29 Dec '10  
GeneralRe: Using Gheat for making an offline heat map PinmemberLeonard Anghelesuc9:08 29 Dec '10  
GeneralRe: Using Gheat for making an offline heat map PinmemberCorey Fournier12:57 29 Dec '10  
GeneralRe: Using Gheat for making an offline heat map PinmemberLeonard Anghelesuc5:12 30 Dec '10  
GeneralRe: Using Gheat for making an offline heat map PinmemberLeonard Anghelesuc5:13 30 Dec '10  
GeneralTile rendering is slow with many points Pinmemberfriism223:58 18 Oct '10  
GeneralRe: Tile rendering is slow with many points PinmemberCorey Fournier3:43 19 Oct '10  
GeneralRe: Tile rendering is slow with many points PinmemberGibb3h23:58 9 Mar '11  
GeneralRe: Tile rendering is slow with many points PinmemberCorey Fournier7:57 28 Mar '11  
GeneralRe: Tile rendering is slow with many points Pinmembercsd0036:42 18 Aug '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120217.1 | Last Updated 21 Jun 2010
Article Copyright 2010 by Corey Fournier
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid