Changeset 360

Show
Ignore:
Timestamp:
10/28/07 17:48:11 (1 year ago)
Author:
migurski
Message:

Remembering to account for center/corner difference in use of map.offset point

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/py/ModestMaps/__init__.py

    r327 r360  
     1""" 
     2>>> m = Map(Microsoft.RoadProvider(), Core.Point(600, 600), Core.Coordinate(3165, 1313, 13), Core.Point(-144, -94)) 
     3>>> p = m.locationPoint(Geo.Location(37.804274, -122.262940)) 
     4>>> p 
     5(370.724, 342.549) 
     6>>> m.pointLocation(p) 
     7(37.804, -122.263) 
     8""" 
     9 
    110import PIL.Image, urllib, StringIO, math, thread, time 
    211 
     
    143152                 
    144153            offset 
    145                 Position of base tile, instance of Point 
     154                Position of base tile relative to map center, instance of Point 
    146155        """ 
    147156        self.provider = provider 
     
    152161    def __str__(self): 
    153162        return 'Map(%(provider)s, %(dimensions)s, %(coordinate)s, %(offset)s)' % self.__dict__ 
    154          
     163 
     164    def locationPoint(self, location): 
     165        """ Return an x, y point on the map image for a given geographical location. 
     166        """ 
     167        point = Core.Point(self.offset.x, self.offset.y) 
     168        coord = self.provider.locationCoordinate(location).zoomTo(self.coordinate.zoom) 
     169         
     170        # distance from the known coordinate offset 
     171        point.x += self.provider.tileWidth() * (coord.column - self.coordinate.column) 
     172        point.y += self.provider.tileHeight() * (coord.row - self.coordinate.row) 
     173         
     174        # because of the center/corner business 
     175        point.x += self.dimensions.x/2 
     176        point.y += self.dimensions.y/2 
     177         
     178        return point 
     179         
     180    def pointLocation(self, point): 
     181        """ Return a geographical location on the map image for a given x, y point. 
     182        """ 
     183        hizoomCoord = self.coordinate.zoomTo(Core.Coordinate.MAX_ZOOM) 
     184         
     185        # because of the center/corner business 
     186        point = Core.Point(point.x - self.dimensions.x/2, 
     187                           point.y - self.dimensions.y/2) 
     188         
     189        # distance in tile widths from reference tile to point 
     190        xTiles = (point.x - self.offset.x) / self.provider.tileWidth(); 
     191        yTiles = (point.y - self.offset.y) / self.provider.tileHeight(); 
     192         
     193        # distance in rows & columns at maximum zoom 
     194        xDistance = xTiles * math.pow(2, (Core.Coordinate.MAX_ZOOM - self.coordinate.zoom)); 
     195        yDistance = yTiles * math.pow(2, (Core.Coordinate.MAX_ZOOM - self.coordinate.zoom)); 
     196         
     197        # new point coordinate reflecting that distance 
     198        coord = Core.Coordinate(round(hizoomCoord.row + yDistance), 
     199                                round(hizoomCoord.column + xDistance), 
     200                                hizoomCoord.zoom) 
     201 
     202        coord = coord.zoomTo(self.coordinate.zoom) 
     203         
     204        location = self.provider.coordinateLocation(coord) 
     205         
     206        return location 
     207 
    155208    def draw(self, verbose=False): 
    156209        """ Draw map out to a PIL.Image and return it. 
     
    200253         
    201254        return mapImg 
     255 
     256if __name__ == '__main__': 
     257    import doctest 
     258    doctest.testmod()