Click here to Skip to main content
11,429,154 members (60,756 online)
Click here to Skip to main content

GHeat .NET

, 21 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
gheat ported to C#

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. Wink | ;)

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.

GHeat_Net/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. The modified file is now included as a separate download. Maybe I will one day explain how that works.

History

  • 21st June, 2010: Initial post
  • 9th September, 2014: Added Points.cs and PointLatLng.cs from Great Maps

License

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

Share

About the Author

Corey Fournier
Software Developer
United States United States
Graduate of University of Louisiana at Lafayette in computer science.

Comments and Discussions

 
QuestionGMap.NET.Core.dll Source Pin
Tsuroo10-Nov-13 11:23
memberTsuroo10-Nov-13 11:23 
AnswerRe: GMap.NET.Core.dll Source Pin
Corey Fournier9-Sep-14 6:14
memberCorey Fournier9-Sep-14 6:14 
GeneralRe: GMap.NET.Core.dll Source Pin
aposition12-Sep-14 2:29
memberaposition12-Sep-14 2:29 
GeneralRe: GMap.NET.Core.dll Source Pin
Chopper123426-Sep-14 12:56
memberChopper123426-Sep-14 12:56 
GeneralRe: GMap.NET.Core.dll Source Pin
Corey Fournier10-Oct-14 4:08
memberCorey Fournier10-Oct-14 4:08 
QuestionPoints in form of pixel coordinates. Pin
lucky895325-Jun-13 0:14
memberlucky895325-Jun-13 0:14 
AnswerRe: Points in form of pixel coordinates. Pin
Corey Fournier25-Jun-13 3:01
memberCorey Fournier25-Jun-13 3:01 
QuestionGridded points and weight Pin
om20125-Oct-12 1:01
memberom20125-Oct-12 1:01 
QuestionTraffic light color-scheme and categorisation Pin
om20124-Oct-12 23:58
memberom20124-Oct-12 23:58 
QuestionHow to maintain heatmap when zooming Pin
om20124-Oct-12 23:41
memberom20124-Oct-12 23:41 
QuestionHeatmap not showing (decimal point problem) Pin
om20124-Oct-12 2:28
memberom20124-Oct-12 2:28 
AnswerRe: Heatmap not showing (decimal point problem) Pin
Corey Fournier4-Oct-12 10:59
memberCorey Fournier4-Oct-12 10:59 
QuestionWinRT Version Pin
rollie_pt28-Jul-12 9:10
memberrollie_pt28-Jul-12 9:10 
AnswerRe: WinRT Version Pin
Corey Fournier1-Aug-12 4:23
memberCorey Fournier1-Aug-12 4:23 
QuestionGreat job! Pin
Member 839626520-May-12 1:38
memberMember 839626520-May-12 1:38 
AnswerRe: Great job! Pin
Alan Novitskiy19-Jun-12 13:15
memberAlan Novitskiy19-Jun-12 13:15 
QuestionHow to clear overlay between data changes Pin
Member 35401722-Apr-12 15:02
memberMember 35401722-Apr-12 15:02 
AnswerRe: How to clear overlay between data changes Pin
Corey Fournier10-Apr-12 12:31
memberCorey Fournier10-Apr-12 12:31 
AnswerRe: How to clear overlay between data changes Pin
Corey Fournier10-Apr-12 12:33
memberCorey Fournier10-Apr-12 12:33 
Adding points it will show up. Make a call to the ClearPointList when you want to clear them all and load all new points. Thats in the Point Manager.
GeneralRe: How to clear overlay between data changes Pin
Member 354017210-Apr-12 13:14
memberMember 354017210-Apr-12 13:14 
GeneralRe: How to clear overlay between data changes Pin
SeanJCo10-Apr-12 14:08
memberSeanJCo10-Apr-12 14:08 
GeneralRe: How to clear overlay between data changes Pin
Corey Fournier11-Apr-12 3:13
memberCorey Fournier11-Apr-12 3:13 
GeneralRe: How to clear overlay between data changes Pin
SeanJCo11-Apr-12 3:50
memberSeanJCo11-Apr-12 3:50 
GeneralRe: How to clear overlay between data changes Pin
Corey Fournier11-Apr-12 4:06
memberCorey Fournier11-Apr-12 4:06 
GeneralRe: How to clear overlay between data changes Pin
SeanJCo11-Apr-12 6:15
memberSeanJCo11-Apr-12 6:15 
GeneralRe: How to clear overlay between data changes Pin
Corey Fournier11-Apr-12 11:27
memberCorey Fournier11-Apr-12 11:27 
GeneralRe: How to clear overlay between data changes Pin
SeanJCo11-Apr-12 12:24
memberSeanJCo11-Apr-12 12:24 
GeneralRe: How to clear overlay between data changes Pin
SeanJCo22-Apr-12 7:07
memberSeanJCo22-Apr-12 7:07 
GeneralRe: How to clear overlay between data changes Pin
Corey Fournier20-Jun-12 3:00
memberCorey Fournier20-Jun-12 3:00 
GeneralRe: How to clear overlay between data changes Pin
SeanJCo20-Jun-12 11:57
memberSeanJCo20-Jun-12 11:57 
QuestionBackground transparency Pin
Manuel Pazos22-Feb-12 6:37
memberManuel Pazos22-Feb-12 6:37 
AnswerRe: Background transparency Pin
Corey Fournier22-Feb-12 9:52
memberCorey Fournier22-Feb-12 9:52 
GeneralRe: Background transparency Pin
Manuel Pazos10-Apr-12 6:00
memberManuel Pazos10-Apr-12 6:00 
GeneralRe: Background transparency Pin
Alan Novitskiy19-Jun-12 14:07
memberAlan Novitskiy19-Jun-12 14:07 
QuestionlogScale Pin
cdoger22-Dec-11 12:24
membercdoger22-Dec-11 12:24 
AnswerRe: logScale Pin
Corey Fournier22-Dec-11 16:22
memberCorey Fournier22-Dec-11 16:22 
QuestionWeight Intensity Pin
Demig0d25-Oct-11 12:41
memberDemig0d25-Oct-11 12:41 
AnswerRe: Weight Intensity Pin
Corey Fournier1-Nov-11 8:34
memberCorey Fournier1-Nov-11 8:34 
GeneralParameter for dotsize Pin
Member 789383924-May-11 7:57
memberMember 789383924-May-11 7:57 
GeneralRe: Parameter for dotsize Pin
Corey Fournier24-May-11 8:13
memberCorey Fournier24-May-11 8:13 
QuestionGenerate colors based on weight range Pin
mustafaa turab ali29-Mar-11 10:38
membermustafaa turab ali29-Mar-11 10:38 
AnswerRe: Generate colors based on weight range Pin
Corey Fournier29-Mar-11 10:49
memberCorey Fournier29-Mar-11 10:49 
GeneralRe: Generate colors based on weight range Pin
mustafaa turab ali29-Mar-11 19:03
membermustafaa turab ali29-Mar-11 19:03 
GeneralRe: Generate colors based on weight range Pin
Corey Fournier30-Mar-11 4:08
memberCorey Fournier30-Mar-11 4:08 
GeneralRemove opacity change on zoom Pin
mustafaa turab ali28-Mar-11 0:39
membermustafaa turab ali28-Mar-11 0:39 
GeneralRe: Remove opacity change on zoom Pin
Corey Fournier28-Mar-11 8:15
memberCorey Fournier28-Mar-11 8:15 
GeneralUsing Gheat for making an offline heat map Pin
Leonard Anghelesuc29-Dec-10 7:07
memberLeonard Anghelesuc29-Dec-10 7:07 
GeneralRe: Using Gheat for making an offline heat map Pin
Corey Fournier29-Dec-10 7:14
memberCorey Fournier29-Dec-10 7:14 
GeneralRe: Using Gheat for making an offline heat map Pin
Leonard Anghelesuc29-Dec-10 9:08
memberLeonard Anghelesuc29-Dec-10 9:08 
GeneralRe: Using Gheat for making an offline heat map Pin
Corey Fournier29-Dec-10 12:57
memberCorey Fournier29-Dec-10 12:57 

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.

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