def displayClicked(self): #add an SVG object to the main window, make this re-render that self.statusBar.showMessage("Displaying, please wait...") css = open('world2.css').read() cfg = options.read_map_config(open('world.json')) self.K.generate(cfg, outfile='world.svg', stylesheet=css, preview = False) self.webview.load(QtCore.QUrl("world.svg")) self.statusBar.showMessage("Map Displayed.") if self.CountryInput.text() and not self.CountryInput.text() in self.existingChecks: tempWidget = QtGui.QCheckBox(self.CountryInput.text(), self) tempWidget.toggle() tempWidget.stateChanged.connect(self.toggleDisplay) self.grid.addWidget(tempWidget, self.bottomCheck, 1) self.bottomCheck += 1 self.existingChecks.append(self.CountryInput.text()) self.updateCountryText()
print 'I need a shapefile to test with. Will download one from naturalearthdata.com\n' from subprocess import call call(['wget', 'http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip']) print '\nUnzipping...\n' call(['unzip', 'ne_50m_admin_0_countries.zip', '-d', 'data']) passed = 0 failed = 0 log = open('log.txt', 'w') for fn in glob('configs/*.*'): fn_parts = splitext(basename(fn)) print 'running text', basename(fn), '...', try: cfg = read_map_config(open(fn)) K = Kartograph() css_url = 'styles/' + fn_parts[0] + '.css' css = None if exists(css_url): css = open(css_url).read() svg_url = 'results/' + fn_parts[0] + '.svg' if exists(svg_url): remove(svg_url) K.generate(cfg, 'results/' + fn_parts[0] + '.svg', preview=False, format='svg', stylesheet=css) print 'ok.' passed += 1 except Exception, e: import traceback ignore_path_len = len(__file__) - 7 exc = sys.exc_info()
def generate_zipcodes_map(data, zipcode, identifier, key_field, algorithm): """ Generate a map with provided data. It will cache the maps based on the identifier, which will be the name. The zipcode is used to find the kml. The key_field will be the field taken into account for the heat map. The algorithm must be one of the "Algorithms" (e.g., Algorithms.LINEAR). Example. Providing this data: { '28004' : { # Each external zipcode 'key1' : 154, 'key2' : 153, 'key3' : 155, }, '28009' : { # Each external zipcode 'key1' : 300, 'key2' : 100, 'key3' : 50, } } A zipcode called '28008', an identifier 'heatmap_incomes', and a key_field 'key1', it will generate and cache a map where the zipcodes 28004 and 28009 will appear, being the latter closer to red since its 'key1' is higher. """ data_hash = hashlib.new("md5", repr(data)).hexdigest() svg_file = 'intellidata/static/geo/%s_%s_%s_%s_%s.svg' % (zipcode, identifier, key_field, algorithm, data_hash) json_svg_file = 'intellidata/static/geo/%s_%s_%s_%s_%s.svg.json' % (zipcode, identifier, key_field, algorithm, data_hash) if CACHE_MAP and os.path.exists(svg_file): return svg_file shp_file_path = obtain_shp_file(data, zipcode, identifier, key_field, data_hash, algorithm) # TODO: improve this. It's already done before all_fields = set() for zdata in data.values(): for field in zdata: all_fields.add(field) sorted_fields = sorted(list(all_fields)) attributes = {'Position' : 'Position', 'ZCode' : 'ZCode'} for sorted_field in sorted_fields: attributes[sorted_field.title()] = sorted_field.title() kartograph_settings = OrderedDict() kartograph_settings["proj"] = { "id": "sinusoidal", "lon0": 20 } kartograph_settings["layers"] = OrderedDict() kartograph_settings["layers"]["background"] = {"special": "sea"} kartograph_settings["layers"]["graticule"] = { "special": "graticule", "latitudes": 1, "longitudes": 1, "styles": { "stroke-width": "0.3px" } } kartograph_settings["layers"]["world"] = { "src": "data/geo/ne_10m_admin_0_countries_cropped.shp" } kartograph_settings["layers"]["provinces"] = { "src" : "data/geo/provincias.shp" } # kartograph_settings["layers"]["world"] = { # "src": "data/geo/ne_10m_admin_0_countries.shp" # } kartograph_settings["layers"]["zipcodes"] = { "src": shp_file_path, "attributes": attributes } kartograph_settings["bounds"] = { "mode": "bbox", "data": [-10, 35, 5, 45], "crop": [-12, 33, 7, 47] } json_svg_file_sio = StringIO.StringIO(json.dumps(kartograph_settings, indent = 4)) json_svg_file_sio.name = json_svg_file K = Kartograph() css = open("intellidata/static/geo/maps.css").read() cfg = read_map_config(json_svg_file_sio) K.generate(cfg, outfile=svg_file, format='svg', stylesheet=css) return svg_file
from kartograph import Kartograph from kartograph.options import read_map_config import sys cfg=read_map_config(open("map.json")) K = Kartograph() K.generate(cfg, outfile='level3_wgs84.svg',format='svg')