abw333
abw333

Reputation: 5961

generating kml files using heatmap.py

I am currently trying to generate a heatmap overlay to Google Maps by using heatmap.py. The site describing heatmap.py (http://jjguy.com/heatmap/) shows a picture of a beautiful heatmap over Washington D.C. and the code used to generate it. After running the code, however, I get the following KML output:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Folder>
    <GroundOverlay>
      <Icon>
        <href>classic.png</href>
      </Icon>
      <LatLonBox>
        <north>38.9096822126249293</north>
        <south>38.8880342183292171</south>
        <east>-77.0127326291108432</east>
        <west>-77.0498038539626435</west>
        <rotation>0</rotation>
      </LatLonBox>
    </GroundOverlay>
  </Folder>
</kml>

This is just a rectangular box. Moreover, I investigated the source code and found the following:

KML = """<?xml version="1.0" encoding="UTF-8"?>                                                                                                                        
<kml xmlns="http://www.opengis.net/kml/2.2">                                                                                                                           
  <Folder>                                                                                                                                                             
    <GroundOverlay>                                                                                                                                                    
      <Icon>                                                                                                                                                           
        <href>%s</href>                                                                                                                                                
      </Icon>                                                                                                                                                          
      <LatLonBox>                                                                                                                                                      
        <north>%2.16f</north>                                                                                                                                          
        <south>%2.16f</south>                                                                                                                                          
        <east>%2.16f</east>                                                                                                                                            
        <west>%2.16f</west>                                                                                                                                            
        <rotation>0</rotation>   
    <GroundOverlay>                                                                                                                                                    
      <Icon>                                                                                                                                                           
        <href>%s</href>                                                                                                                                                
      </Icon>                                                                                                                                                          
      <LatLonBox>                                                                                                                                                      
        <north>%2.16f</north>                                                                                                                                          
        <south>%2.16f</south>                                                                                                                                          
        <east>%2.16f</east>                                                                                                                                            
        <west>%2.16f</west>                                                                                                                                            
        <rotation>0</rotation>                                                                                                                                         
      </LatLonBox>                                                                                                                                                     
    </GroundOverlay>                                                                                                                                                   
  </Folder>                                                                                                                                                            
</kml>"""

def saveKML(self, kmlFile):
    """                                                                                                                                                            
    Saves a KML template to use with google earth.  Assumes x/y coordinates                                                                                        
    are lat/long, and creates an overlay to display the heatmap within Google                                                                                      
    Earth.                                                                                                                                                         

    kmlFile ->  output filename for the KML.                                                                                                                       
    """

    tilePath = os.path.basename(self.imageFile)
    north = self.maxXY[1]
    south = self.minXY[1]
    east = self.maxXY[0]
    west = self.minXY[0]

    bytes = KML % (tilePath, north, south, east, west)
    file(kmlFile, "w").write(bytes)

Which seems to do exactly what the output suggests. Has anyone been able to generate a heatmap similar to the pictured one using heatmap.py. If not, have you been able to generate a similar heatmap using another method? Thanks.

Upvotes: 0

Views: 1340

Answers (1)

Giuseppe Cardone
Giuseppe Cardone

Reputation: 5393

That is not "just a rectangular box". That's exactly how overlays are defined in KML. The Google documentations reports this example:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<GroundOverlay>
   <name>GroundOverlay.kml</name>
   <color>7fffffff</color>
   <drawOrder>1</drawOrder>
   <Icon>
      <href>http://www.google.com/intl/en/images/logo.gif</href>
      <refreshMode>onInterval</refreshMode>
      <refreshInterval>86400</refreshInterval>
      <viewBoundScale>0.75</viewBoundScale>
   </Icon>
   <LatLonBox>
      <north>37.83234</north>
      <south>37.832122</south>
      <east>-122.373033</east>
      <west>-122.373724</west>
      <rotation>45</rotation>
   </LatLonBox>
</GroundOverlay>
</kml>

You can save it as kml file and check that it works. The main difference compared to the code in your question is the <color> tag: this example uses the alpha channel to reduce the opacity of the image. The <Icon> section contains a reference to the image to show, the <LatLonBox> contains the coordinates of the image.

Check the google documentation about GroundOverlay for more details.

Upvotes: 1

Related Questions