def make_thing_map(query, symbols, filename, legend=False): global lat, lng # symbols: [(uri, color, name), ...] themap = config.make_map_from_cli_args() symbols = { uri: (themap.add_symbol(random_id(), color, '#000000', 1, title=pick_name(name)), pick_name(name)) for (uri, color, name) in symbols } other = themap.add_symbol(random_id(), '#000000', '#000000', 1, title='Other') unclassified = {} for (s, title, thing, lat, lng) in sparqllib.query_for_rows(query): try: (symbol, name) = symbols[thing] except KeyError: unclassified[thing] = unclassified.get(thing, 0) + 1 symbol = other name = thing themap.add_marker(lat, lng, title + ': ' + name, symbol) for (thing, count) in unclassified.items(): print 'Unclassified: %s %s' % (thing, count) themap.set_legend(legend) themap.render_to(config.get_file() or filename)
def color_scale_map_data(data, outfile, legend=False, symbol_count=10, label_formatter=None, the_range=None): themap = config.make_map_from_cli_args() if the_range: (smallest, biggest) = the_range else: smallest = min([v for (lat, lng, title, v) in data]) biggest = max([v for (lat, lng, title, v) in data]) increment = (biggest - smallest) / (symbol_count - 1) symbols = [ themap.add_symbol('id%s' % ix, '#' + color(ix, symbol_count), '#000000', strokeweight=1, title=label_formatter( smallest + increment * ix, smallest + increment * (ix + 1)) #,scale = (ix / float(symbol_count)) * max_scale ) for ix in range(symbol_count) ] for (lat, lng, title, org_value) in data: ratio = org_value index = (int((ratio - smallest) / increment)) symbol = symbols[index] themap.add_marker(lat, lng, title, symbol, 'Value: %s' % org_value) themap.set_legend(legend) themap.render_to(outfile)
def make_term_map(termprop, symbols, filename, usemap=None, scale=None, language='en'): global lat, lng # symbols: [(regex, color, name), ...] stroke = '#000000' themap = usemap or config.make_map_from_cli_args() symbols = [(regex, themap.add_symbol(random_id(), color, stroke, strokeweight=1, title=name, scale=scale)) for (regex, color, name) in symbols] othername = { 'en': 'Other', 'no': 'Annet', }[language] OTHER = themap.add_symbol('black', '#000000', stroke, strokeweight=1, title=othername, scale=scale) unmatched = [] for (title, lat, lng, term) in sparqllib.query_for_rows(query % termprop): symbol = OTHER for (regex, s) in symbols: if matches(regex, term): symbol = s break themap.add_marker(lat, lng, title + ': ' + term, symbol) if symbol == OTHER: unmatched.append(term) themap.set_legend(True) themap.render_to(filename) unmatched.sort() for term in unmatched: print term
def make_boolean_map(query, filename, labels=None): themap = config.make_map_from_cli_args() if labels: themap.set_legend(True) labels = labels or {'borderline': '', 'true': '', 'false': ''} gray = themap.add_symbol('gray', '#999999', title=labels['borderline']) symbols = { 'true': themap.add_symbol('white', '#FFFF00', title=labels['true']), 'false': themap.add_symbol('black', '#000000', title=labels['false']), 'http://www.garshol.priv.no/2014/neg/both': gray, 'http://www.garshol.priv.no/2014/neg/borderline': gray } for (s, lat, lng, title, value) in sparqllib.query_for_rows(query): symbol = symbols[value] themap.add_marker(lat, lng, title, symbol) themap.render_to(filename)
import config import maplib import sparqllib themap = config.make_map_from_cli_args() symbols = { '1' : themap.add_symbol('white', '#FFFFFF', '#000000'), '0' : themap.add_symbol('black', '#000000', '#000000'), 'true' : themap.add_symbol('white', '#FFFFFF', '#000000'), 'false' : themap.add_symbol('black', '#000000', '#000000') } query = ''' prefix dc: <http://purl.org/dc/elements/1.1/> prefix neg: <http://www.garshol.priv.no/2014/neg/> prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> prefix tb: <http://www.garshol.priv.no/2014/trad-beer/> SELECT DISTINCT ?s ?lat ?lng ?title ?mead WHERE { ?s dc:title ?title; geo:lat ?lat; geo:long ?lng; tb:strong-beer ?mead. }''' for (s, lat, lng, title, mead) in sparqllib.query_for_rows(query): themap.add_marker(lat, lng, title, symbols[mead])
import sys, string import config import colorsys import maputils import sparqllib fileformat = sys.argv[4] if len(sys.argv) >= 5 else 'png' speciesfile = sys.argv[3] if len(sys.argv) >= 4 else None themap = config.make_map_from_cli_args(speciesfile=speciesfile) herb = sys.argv[2] white = themap.add_symbol('white', '#FFFFFF', '#000000', strokeweight=1, title=string.upper(herb[0]) + herb[1:]) black = themap.add_symbol('black', '#000000', '#000000', strokeweight=1, title='No ' + herb) query = ''' prefix neg: <http://www.garshol.priv.no/2014/neg/> prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> prefix tb: <http://www.garshol.priv.no/2014/trad-beer/> SELECT ?s ?title ?lat ?lng ?herbs WHERE { ?s dc:title ?title;
?s tb:juniper-infusion ?inf. } }''' accounts = {} for (s, inf, herb, districtn) in sparqllib.query_for_rows(query): inf = (inf == 'true') already_juniper = accounts.get(s, (None, False, None))[1] accounts[s] = (inf, already_juniper or herb == JUNIPER, districtn) accounts = [(districtn, (0.75 + (0.25 if inf else 0)) if juniper else 0) for (inf, juniper, districtn) in accounts.values()] district_index = utils.index(accounts) from pprint import pprint # pprint(district_index) district_to_value = { name: utils.average(points) for (name, points) in district_index.items() } for (district_name) in sparqllib.query_for_list(district_query): if district_name not in district_to_value: district_to_value[district_name] = None # means: no data pprint(district_to_value) themap = config.make_map_from_cli_args(map_type='choropleth') themap.render_to('juniper-choropleth-map', district_to_value)