4

I have a list of 7130 values which are from 0 to 1, these values represent a "heat value" on a city map at corresponding GPS coordinates and I can extract the coordinates from a data file. Now I want to use python to generate a heat map like the picture shown here. Does anyone know how to do that? Thank you very much

enter image description here

gladys0313
  • 2,569
  • 6
  • 27
  • 51
  • To plot over a map you should checkout `basemap` toolkit in `matplotlib`. Specifically something like [this](http://nbviewer.ipython.org/url/bagrow.com/dsv/heatmap_basemap.ipynb) doesn't seem that hard to do. To get the smooth map like you have, you could also check out `scipy`, specifically [`interpolate`](http://docs.scipy.org/doc/scipy/reference/interpolate.html) to smooth out your data. – ljetibo Mar 09 '15 at 21:44
  • Have you managed to do this? If so, would you care to share your code? – Alexis Eggermont Nov 18 '15 at 05:19
  • @AlexisEggermont hi, yes I managed to do this at end, but not with python. I used google table actually......then it is not a issue about code, it is just about handling data – gladys0313 Nov 18 '15 at 06:55

3 Answers3

5

There are a lot of details that would go into answering this question.

The first would be, what have you got so far?

Do you have a map image?

Have you plotted the gps coordinates to coincide with the pixel positions in your image?

Do you have a Look Up Table to correspond with your different "temp" values?

Are you looking to create a static output image, or do you want it to update dynamically? (This may determine the method you use to generate the heat-map overlay.)

Once you have those details ironed out it should be fairly simple to generate a heat map like the one above using any of the various imaging libraries available in Python (PIL/OpenCV).

This is a rough (overly simplified) outline of how I would generate the heat map from the initial data using OpenCV:
I would start with two images, the map image, and a zero valued (black) image of the same size. You can then add the appropriate values to all three color channels of your blank image at the gps/pixel locations which will give you a 3 channeled gray image. (So if the value is 0.25, you set RG and B to 0.25)
Initial Map image Initial 3 channel gray value map

Then, apply a gaussian blur with a large kernel size; appropriate to the amount of blending you want between points and the size of your image.
Blurred value map
You will likely need to multiply your blurred image by some factor (depending on kernel size) after blurring to brighten the colors.
Multiplied Blurred value map

Next apply your Look Up Table to your Blurred mapped values:
Value map with LUT

Then you could merge the two images into one output image using any number of combinations (add, multiply, addWeighted(), etc). Or if you want to involve an alpha channel for a cleaner overlay you could use the method described here.
Final Heat map

Community
  • 1
  • 1
BHawk
  • 2,382
  • 1
  • 16
  • 24
  • In case of console based heat map generation in static can we have a map as base for the heat map ?? Could you pls share some details in that context –  May 08 '15 at 19:43
2

Here is a great library HEATMAP.

Code snippet:

import heatmap
import random

pts = []
for x in range(400):
    pts.append((random.random(), random.random() ))

print "Processing %d points..." % len(pts)

hm = heatmap.Heatmap()
img = hm.heatmap(pts)
img.save("classic.png")

Result image

ICapalija
  • 31
  • 5
2

If you use PYTHON language there is a very short way to do it. You need to install just one library which is gmaps. You need a Google Map Key to use this library.

Link of the library: https://jupyter-gmaps.readthedocs.io/en/stable/tutorial.html#weighted-heatmaps

Here is an example:

import gmaps
import gmaps.datasets
from ipywidgets.embed import embed_minimal_html
import pandas as pd
columns = ["latitude","longitude","magnitude"]
a = []

for i in your_arr:
    a.append([your_arr[0], your_arr[1], your_arr[2]])
df = pd.DataFrame(a,columns=columns)
gmaps.configure(api_key="YOUR_GOOGLE_MAPS_API_KEY_HERE")

fig = gmaps.figure()
heatmap_layer = gmaps.heatmap_layer(
    df[['latitude', 'longitude']], weights=df['magnitude'],max_intensity=210,point_radius=30
)
fig.add_layer(heatmap_layer)

embed_minimal_html('export.html', views=[fig])

Here how it looks like: enter image description here

After that you can open export.html in your browser and can see your heatmap easily :)

Mehmet nuri
  • 840
  • 7
  • 7