使用heatmap.py生成kml文件

时间:2012-07-06 17:44:38

标签: python kml heatmap

我目前正在尝试使用heatmap.py为Google地图生成热图叠加层。描述heatmap.py(http://jjguy.com/heatmap/)的网站显示了华盛顿特区美丽热图的图片以及用于生成它的代码。但是,在运行代码之后,我得到以下KML输出:

<?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>

这只是一个矩形框。此外,我调查了源代码并发现了以下内容:

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)

这似乎与输出结果完全一致。有没有人能够使用heatmap.py生成类似于图示的热图。如果没有,您是否能够使用其他方法生成类似的热图?感谢。

1 个答案:

答案 0 :(得分:1)

这不是“只是一个矩形框”。这正是KML中定义叠加的方式。 Google文档报告此示例:

<?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>

您可以将其保存为kml文件并检查它是否有效。与您的问题中的代码相比的主要区别是<color>标记:此示例使用Alpha通道来降低图像的不透明度。 <Icon>部分包含对要显示的图片的引用,<LatLonBox>包含图像的坐标。

Check the google documentation about GroundOverlay for more details