Skip to content

Commit

Permalink
Updated flattener so that it doesn't create intermediary objects. Muc…
Browse files Browse the repository at this point in the history
…h more efficient.
  • Loading branch information
Nathan Villaescusa committed Feb 1, 2011
1 parent 023eefb commit 495ff71
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions gcs/tools/flattener.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ def from_latlngbounds(cls, bounds):
def initialize(self):
pass

def gis_to_cart_coords(self, coords):
def gis_to_cart_coord(self, coords):
raise Exception("Not Implemented")

def cart_to_gis_coord(self, coords):
raise Exception("Not Implemented")

def gis_to_cart_coords(self, coords):
'''Converts latlng coords to cartesian coords'''

return (self.gis_to_cart_coord(c) for c in coords)

def cart_to_gis_coords(self, coords):
raise Exception("Not Implemented")
'''Converts cartesian coords to a latlng'''

return (self.cart_to_gis_coord(c) for c in coords)

def gis_to_cart_shape(self, shape):
'''Converts a GIS shape to a cartesian shape'''
Expand All @@ -52,28 +62,28 @@ def cart_to_gis_shape(self, shape):
coords = shape.coords
except:
coords = shape.__geo_interface__['coordinates']

return shape.__class__(tuple(self.cart_to_gis_coords(coords)))

def gis_to_cart_point(self, point):
'''Converts a lat,lng to a cartesian point'''

try:
coords = point.coords
coord = point.coords[0]
except:
coords = (point.__geo_interface__['coordinates'], )
coord = (point.__geo_interface__['coordinates'], )

return Point(tuple(self.gis_to_cart_coords(coords)))
return Point(self.gis_to_cart_coord(coord))

def cart_to_gis_point(self, point):
'''Converts a cartesian point to a latlng'''

try:
coords = point.coords
coord = point.coords[0]
except:
coords = (point.__geo_interface__['coordinates'], )
coord = (point.__geo_interface__['coordinates'], )

return Point(tuple(self.cart_to_gis_coords(coords)))
return Point(self.cart_to_gis_coord(coord))

class InterpolatedFlattener(GeoWindow):

Expand All @@ -88,21 +98,23 @@ def initialize(self):
self.translate_y = -self.min_lat
self.translate_x = -self.min_lng

def gis_to_cart_coords(self, coords):
'''Converts latlng coords to cartesian coords'''

def gis_to_cart_coord(self, coord):
'''Converts a latlng coord to cartesian coord'''

x, y = coord
x = (x + self.translate_x) * self.scale_x
y = (y + self.translate_y) * self.scale_y
return (x, y)

def cart_to_gis_coord(self, coord):
'''Converts a cartesian coords to a latlng'''

x, y = coord
x = (x / self.scale_x) - self.translate_x
y = (y / self.scale_y) - self.translate_y
return (x, y)

for x, y in coords:
x = (x + self.translate_x) * self.scale_x
y = (y + self.translate_y) * self.scale_y

yield (x, y)

def cart_to_gis_coords(self, coords):
'''Converts cartesian coords to a latlng'''

for x, y in coords:
x = (x / self.scale_x) - self.translate_x
y = (y / self.scale_y) - self.translate_y

yield (x, y)

0 comments on commit 495ff71

Please sign in to comment.