Skip to content

Instantly share code, notes, and snippets.

@ToferC
Created December 22, 2013 02:34
Show Gist options
  • Save ToferC/8077804 to your computer and use it in GitHub Desktop.
Save ToferC/8077804 to your computer and use it in GitHub Desktop.
Map Writer
# Script to identify the coordinates for locations on an image map by having the user tap on the appropriate locations and saving the results into a dictionary that can be called by another script.
# Requires an initial dictionary with at least the location name as a key and x, y coordinates as list values 0 and 1.
import math, random
import city_list
from scene import *
offset = 100
city_dict = city_list.city_dict
global city_dict
class MyScene (Scene):
global city_dict
def setup (self):
# Set up the root layer, the data layer and our variables.
self.root_layer = Layer(self.bounds)
center = self.bounds.center()
self.layer = Layer(Rect(0, offset))
self.layer.background = Color(1, 0, 0)
self.layer.image = '_ACKS_ipad_map'
self.data_layer = Layer(Rect(0,0,1024,offset))
self.data_layer.background = Color(0,0,0)
self.x = 0
self.y = 0
self.counter = 0
self.new_list = []
for i in city_dict.iterkeys():
self.new_list.append([i,city_dict[i][0],city_dict[i][1]])
def draw (self):
# Renders the scene and requests user to select locations on the map.
if self.counter == len(self.new_list):
text("You are done!",font_name='Helvetica',font_size=70,x=412,y=350,alignment=6)
else:
self.root_layer.update(self.dt)
self.root_layer.draw()
self.layer.update(self.dt)
self.layer.draw()
self.data_layer.update(self.dt)
self.data_layer.draw()
self.call = text('Please select '+str(self.new_list[self.counter][0])+' on the map',font_name='Helvetica',font_size=15,x=10,y=60,alignment=6)
self.result = text(str(self.new_list[self.counter]),font_name='Helvetica',font_size=15,x=10,y=10,alignment=6)
def touch_began(self, touch):
# If the list hasnt been completed, returns the selected locations and writes the x, y coordinates to new_list.
if self.counter < len(self.new_list):
self.x, self.y = touch.location.x, touch.location.y
self.new_list[self.counter][1] = self.x
self.new_list[self.counter][2] = self.y
self.counter += 1
else:
# If the list has been completed, takes the x, y coords, inputs them back into city_dict and saves the dictionary in a new file.
for i in range(len(self.new_list)):
city_dict[self.new_list[i][0]][0] = self.new_list[i][1]
city_dict[self.new_list[i][0]][1] = self.new_list[i][2]
doc = open('city_list3.py','r+')
doc.write('city_dict = '+str(city_dict))
doc.close()
def touch_moved(self, touch):
pass
def touch_ended(self, touch):
pass
run(MyScene())
@cclauss
Copy link

cclauss commented Dec 22, 2013

Writing the file with "r+" instead of "w" did not make much sense to me.
Adding sublayers to the root_layer simplifies the update() and draw() code.
Reused the city_dict for data storage so new_list could be eliminated.
Added reading and writing to a .json file which is a more standard approach than using city_list.py as a data file.
(I could never really figure out how to create the city_list.py file in a way that would work with the main file.)

# Script to identify the coordinates for locations on an image map by having the user tap on the appropriate locations and saving the results into a dictionary that can be called by another script.

import json, scene

image_file_name = '_Ursina'  # '_ACKS_ipad_map'
json_file_name  = 'city_list.json'

city_dict = { 'Dusseldorf' : None,
              'Brussels'   : None,
              'Copenhagen' : None,
              'Amsterdam'  : None }
try:     # try to read in a better city list from a .json file
    with open(json_file_name) as in_file:
        city_dict = json.load(in_file)
except:  # but if not, go with the default dict above
    pass
#print(city_dict)

class MyScene (scene.Scene):
    def setup(self):
        # Set up the root layer, imagelayer, data layer and variables.
        self.root_layer = scene.Layer(self.bounds)
        offset = 100
        self.layer = scene.Layer(scene.Rect(0, offset))
        self.root_layer.add_layer(self.layer)
        self.layer.background = scene.Color(1, 0, 0)
        self.layer.image = image_file_name
        self.data_layer = scene.Layer(scene.Rect(0,0,1024,offset))
        self.root_layer.add_layer(self.data_layer)
        self.data_layer.background = scene.Color(0.1,0.1,0.1)
        self.counter = 0

    def current_city(self):
        return sorted(city_dict.keys())[self.counter]

    def draw(self):
        # Renders the scene and requests user to select locations on the map.
        self.root_layer.update(self.dt)
        self.root_layer.draw()
        if self.counter == len(city_dict):  # self.new_list):
            scene.text("You are done!",font_name='Helvetica',font_size=70,x=412,y=350,alignment=6)
        else:
            #self.root_layer.update(self.dt)
            #self.root_layer.draw()
            the_city = self.current_city()
            prompt_message = 'Please select {} on the map'.format(the_city)
            city_and_location = '{} = {}'.format(the_city, city_dict[the_city])
            scene.text(prompt_message, font_name='Helvetica', font_size=15, x=10, y=60, alignment=6)
            scene.text(city_and_location, font_name='Helvetica', font_size=15, x=10, y=10, alignment=6)

    def touch_began(self, touch):
        # While the list is not yet completed, write the x, y coordinates into city_dict.
        if self.counter < len(city_dict):
            city_dict[self.current_city()] = touch.location.as_tuple()
            self.counter += 1
        # When the list is completed, save the dictionary to a new file.
        if self.counter == len(city_dict):
            #with open('city_list3.py','w') as doc:
            #    doc.write('city_dict = '+str(city_dict))
            with open(json_file_name, 'w') as json_doc:
                json.dump(city_dict, json_doc, indent=4)

scene.run(MyScene())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment