Kaardi värvimise näide
This commit is contained in:
parent
fff3257207
commit
3cde07e23a
69
kaart.py
Normal file
69
kaart.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
import GeoIP
|
||||||
|
import sys
|
||||||
|
from numpy import interp
|
||||||
|
from collections import Counter
|
||||||
|
from lxml import etree
|
||||||
|
from lxml.cssselect import CSSSelector
|
||||||
|
|
||||||
|
# apt install python-geoip python-numpy geoip-database
|
||||||
|
# dnf install python2-GeoIP
|
||||||
|
|
||||||
|
try:
|
||||||
|
blank_map_path, = sys.argv[1:]
|
||||||
|
except ValueError:
|
||||||
|
print "Anna argumentideks teed BlankMap-World6.svg ning access.log failideni"
|
||||||
|
sys.exit(254)
|
||||||
|
|
||||||
|
# Loe sisse värvimata kaardifail kasutades lxml moodulit
|
||||||
|
# https://upload.wikimedia.org/wikipedia/commons/0/03/BlankMap-World6.svg
|
||||||
|
document = etree.parse(open(blank_map_path))
|
||||||
|
|
||||||
|
# Lae GeoIP andmebaas
|
||||||
|
gi = GeoIP.open("/usr/share/GeoIP/GeoIP.dat", GeoIP.GEOIP_MEMORY_CACHE)
|
||||||
|
|
||||||
|
# Loo tühi Counter objekt
|
||||||
|
hits = Counter()
|
||||||
|
|
||||||
|
# Käi ridahaaval üle standardsisendi
|
||||||
|
for line in sys.stdin:
|
||||||
|
# Kui reas ei esine GET sõnet, hüppa järgmisse tsükklisse
|
||||||
|
if "GET" not in line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Löö rida tühikute järgi juppideks
|
||||||
|
fields = line.split()
|
||||||
|
|
||||||
|
# IP aadress esimeses tulbas
|
||||||
|
remote_addr = fields[0]
|
||||||
|
|
||||||
|
# Proovi teisendada IP riigikoodiks GeoIP abil
|
||||||
|
country = gi.country_code_by_addr(remote_addr)
|
||||||
|
if not country:
|
||||||
|
# IP aadressi ei saa vastendada riigiga kui tegu on nt sisevõrgu aadressidega,
|
||||||
|
# lisaks /usr/share/GeoIP/GeoIP.dat andmebaasis pole IPv6 aadresse
|
||||||
|
continue
|
||||||
|
hits[country] += 1
|
||||||
|
|
||||||
|
for country, count in hits.items():
|
||||||
|
# Interpoleeri külastuste arv vahemikus 0 kuni kõige enam külastusi saanud riik
|
||||||
|
# värvitoonide vahemikku 120 (roheline) kuni 0 (punane)
|
||||||
|
hue = interp(count, [0, max(hits.values())], [120, 0])
|
||||||
|
|
||||||
|
# Leia XPath abil elemendid mille id attribuut on pandud riigikoodiks
|
||||||
|
for element in document.xpath("//*[@id='%s']" % country.lower()):
|
||||||
|
|
||||||
|
# Pane riigi <g> tagile külge stiili attribuut
|
||||||
|
# kus värvikood võetakse HSL värviruumist (hue ehk toon, külluslikkus 60% ja heledus 60%)
|
||||||
|
element.set("style", "fill:hsl(%.2f, 60%%, 60%%)" % hue)
|
||||||
|
|
||||||
|
# Eemalda kõigilt alamelementidelt class attribuut kus sisaldub viide hallile värvile
|
||||||
|
for subelement in element:
|
||||||
|
subelement.attrib.pop("class", "")
|
||||||
|
|
||||||
|
# Kirjuta väljund faili top.svg
|
||||||
|
# Proovi avada see fail Firefox või Chrome abil nt
|
||||||
|
with open("top.svg", "w") as fh:
|
||||||
|
fh.write(etree.tostring(document))
|
Loading…
Reference in New Issue
Block a user