Skip to content

Commit

Permalink
Ability to save GoogleMap to file (#174)
Browse files Browse the repository at this point in the history
New function to `saveGoogleMap()`

Required making the mapData a class variable, so that it was readily
available
  • Loading branch information
jarvisteach committed Jun 21, 2017
1 parent 6eda6e1 commit 61b8b5a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
26 changes: 20 additions & 6 deletions appJar/appjar.py
Expand Up @@ -4354,6 +4354,10 @@ def getGoogleMapLocation(self, title):

def getGoogleMapSize(self, title):
return self.__verifyItem(self.n_maps, title).params["size"]

def saveGoogleMap(self, title, fileLocation):
gMap = self.__verifyItem(self.n_maps, title)
return gMap.saveTile(fileLocation)


#####################################
Expand Down Expand Up @@ -10354,11 +10358,11 @@ def __init__(self, parent):
self.__setMapParams()

imgObj = None
mapData = self.getMapData()
self.mapData = self.getMapData()
# if we got some map data then load it
if mapData is not None:
if self.mapData is not None:
try:
imgObj = PhotoImage(data=mapData)
imgObj = PhotoImage(data=self.mapData)
self.h = imgObj.height()
self.w = imgObj.width()
# python 3.3 fails to load data
Expand Down Expand Up @@ -10469,6 +10473,16 @@ def addMarker(self, location):
self.markers.append(location)
self.parent.after(0, self.updateMap())

def saveTile(self, location):
try:
with open(location, "wb") as fh:
fh.write(self.mapData)
logging.getLogger("appJar").info("Map data written to file: " + str(location))
return True
except Exception as e:
logging.getLogger("appJar").exception(e)
return False

def setSize(self, size):
if size != self.params["size"]:
self.params["size"] = size.lower()
Expand Down Expand Up @@ -10502,9 +10516,9 @@ def zoom(self, mod):
self.parent.after(0, self.updateMap())

def updateMap(self):
mapData = self.getMapData()
if mapData is not None:
imgObj = PhotoImage(data=mapData)
self.mapData = self.getMapData()
if self.mapData is not None:
imgObj = PhotoImage(data=self.mapData)
self.canvas.itemconfig(self.image_on_canvas, image=imgObj)
self.canvas.img = imgObj

Expand Down
6 changes: 6 additions & 0 deletions docs/mkdocs/docs/pythonDevWidgets.md
Expand Up @@ -94,6 +94,12 @@ app.go()
* `.getGoogleMapSize(title)`
Returns the current size of the map tile.

#### Save GoogleMaps

* `.saveGoogleMap(title, fileName)`
Saves the currently displayed map to the named location.
By default, all map tiles are GIFs.

###PieChart
---
Widget to depict a Pie Chart.
Expand Down
4 changes: 3 additions & 1 deletion examples/issues/issue136.py
Expand Up @@ -13,6 +13,8 @@ def press(btn):
app.setGoogleMapSize("m1", app.getEntry("e1"))
elif btn == "MARK":
app.setGoogleMapMarker("m1", app.getEntry("e1"))
elif btn == "SAVE":
app.saveGoogleMap("m1", app.getEntry("e1"))
else:
app.zoomGoogleMap("m1", btn)

Expand All @@ -23,7 +25,7 @@ def press(btn):
app.setGoogleMapMarker("m1", "swindon")
app.setGoogleMapSize("m1", "340x500")
app.addEntry("e1")
app.addButtons(["SEARCH", "+", "-", "ZOOM", "SIZE", "TERRAIN", "MARK"], press)
app.addButtons(["SAVE", "SEARCH", "+", "-", "ZOOM", "SIZE", "TERRAIN", "MARK"], press)

app.go()

0 comments on commit 61b8b5a

Please sign in to comment.