<reponame>ttsda/incendios.ttsda.cc<filename>generate_hazard.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
generate_hazard.py -- Generates the hazard.json file with all the fire hazard information
Copyright (C) 2014 <NAME>
"""

# Logging
import logging

# System
import os

# HTTP
import requests

# Processing
from PIL import Image, ImageFile

try:
    from StringIO import StringIO
except ImportError:
    from io import BytesIO as StringIO
import operator

# Serializing
import json
import yaml

# Load the config
config = yaml.load(open(os.path.dirname(os.path.realpath(__file__)) + '/config.yml', 'r'))

# Set the loglevel
log_level = logging.ERROR
if config['debug']:
    log_level = logging.INFO
logging.basicConfig(format='[%(levelname)s] %(message)s', level=log_level)

# Download today's fire hazard image from the ipma website
logging.info('Downloading image')
r = requests.get('http://www.ipma.pt/resources.www/transf/indices/rcm_dh.jpg', timeout=10)

# Load the image's pixels into an array
ImageFile.LOAD_TRUNCATED_IMAGES = True
logging.info('Loading image')
im = Image.open(StringIO(r.content))
image = im.load()


def get_level(x, y):
    """
    Get the fire hazard level from a pixel by checking which level is closer to it's color
    """

    levels = {
        5: image[350, 570],
        4: image[350, 590],
        3: image[350, 610],
        2: image[350, 630],
        1: image[350, 650]
    }

    closest = (0, 196609)  # Level, Difference

    # Level, Level color
    try:
        items = levels.iteritems()
    except AttributeError:
        items = levels.items()

    for level, lc in items:
        pc = image[x, y]  # Pixel color
        difference = (pc[0] - lc[0]) ** 2 + (pc[1] - lc[1]) ** 2 + (pc[2] - lc[2]) ** 2

        if difference < closest[1]:
            closest = (level, difference)

    return closest[0]

# List of municipalities and location of a pixel that's within it's limits
concelhos = {  # Lisboa
               'Cascais': (118, 429),
               'Oeiras': (130, 430),
               'Sintra': (123, 419),
               'Amadora': (135, 426),
               'Lisboa': (142, 428),
               'Mafra': (127, 402),
               '<NAME>': (131, 388),
               'Lourinhã': (131, 369),
               'Cadaval': (151, 371),
               'Alenquer': (153, 385),
               'Azambuja': (166, 388),
               'Odivelas': (138, 422),
               'Loures': (140, 415),
               '<NAME>': (145, 402),
               'Sobral de Mont<NAME>': (143, 397),
               '<NAME>': (155, 409),  # Leiria
               'Peniche': (129, 358),
               'Bombarral': (143, 365),
               'Óbidos': (137, 355),
               '<NAME> Rainha': (150, 350),
               'Alcobaça': (162, 339),
               'Nazaré': (154, 328),
               '<NAME>': (159, 308),
               'Leiria': (177, 308),
               'Pombal': (185, 290),
               '<NAME>': (174, 336),
               'Batalha': (180, 324),
               'Ansião': (206, 288),
               'Alvaiázere': (210, 303),
               '<NAME>': (222, 297),
               '<NAME>': (228, 282),
               '<NAME>': (230, 291),  #Coimbra
               'Arganil': (250, 256),
               'Cantanhede': (190, 241),
               'Coimbra': (207, 257),
               'Condeixa-a-Nova': (201, 270),
               '<NAME>': (174, 257),
               'Góis': (238, 268),
               'Lousã': (226, 267),
               'Mira': (181, 233),
               '<NAME>': (217, 272),
               'Montemor-o-Velho': (187, 254),
               'Oliveira do Hospital': (260, 240),
               'P<NAME> Serra': (257, 271),
               'Penacova': (222, 248),
               'Penela': (214, 281),
               'Soure': (188, 272),
               'Tábua': (243, 245),
               'Vila Nova de Poiares': (224, 257),  #Aveiro
               'Águeda': (211, 215),
               'Albergaria-a-Velha': (205, 197),
               'Anadia': (208, 229),
               'Arouca': (226, 175),
               'Aveiro': (189, 204),
               '<NAME>': (221, 164),
               'Espinho': (191, 167),
               'Estarreja': (195, 195),
               'Ílhavo': (183, 210),
               'Mealhada': (206, 240),
               'Murtosa': (187, 195),
               '<NAME>': (204, 185),
               '<NAME>': (196, 223),
               'Ovar': (191, 181),
               '<NAME>': (200, 170),
               #'<NAME>': (203, 177), Removed because the image doesn't have enough resolution
               '<NAME>': (213, 195),
               'Vagos': (184, 221),
               '<NAME>': (216, 183),  #Porto
               'Amarante': (242, 134),
               'Baião': (247, 147),
               'Felgueiras': (229, 125),
               'Gondomar': (202, 152),
               'Lousada': (223, 133),
               'Maia': (192, 136),
               '<NAME>': (234, 146),
               'Matosinhos': (188, 142),
               '<NAME>': (214, 133),
               'Paredes': (212, 142),
               'Penafiel': (220, 149),
               'Porto': (193, 147),
               '<NAME>': (180, 117),
               '<NAME>': (207, 128),
               'Trofa': (192, 129),
               'Valongo': (203, 141),
               '<NAME>': (185, 127),
               'Vila Nova de Gaia': (194, 156),  # Braga
               'Amares': (215, 91),
               'Barcelos': (189, 102),
               'Braga': (208, 103),
               '<NAME>': (248, 103),
               '<NAME>': (242, 119),
               'Esposende': (180, 103),
               'Fafe': (232, 111),
               'Guimarães': (220, 112),
               '<NAME>': (223, 97),
               '<NAME>': (230, 80),
               '<NAME>': (238, 91),
               '<NAME>ova de Famalicão': (205, 116),
               '<NAME>': (204, 88),
               'Vizela': (221, 124),  #Viana do Castelo
               '<NAME>': (213, 61),
               'Caminha': (178, 67),
               'Melgaço': (225, 42),
               'Monção': (208, 47),
               '<NAME>': (196, 59),
               '<NAME>': (222, 72),
               'Ponte de Lima': (196, 75),
               'Valença': (194, 49),
               '<NAME>': (181, 82),
               'Vila Nova de Cerveira': (185, 59),  #Setúbal
               'Alcochete': (167, 431),
               '<NAME>': (206, 474),
               'Almada': (138, 439),
               'Barreiro': (153, 443),
               'Grândola': (197, 494),
               'Moita': (155, 437),
               'Montijo': (188, 428),
               'Palmela': (172, 440),
               '<NAME>': (198, 520),
               'Seixal': (144, 443),
               'Sesimbra': (144, 456),
               'Setúbal': (155, 453),
               'Sines': (177, 527),  #Beja
               'Aljustrel': (228, 527),
               'Almodôvar': (236, 573),
               'Alvito': (242, 487),
               'Barrancos': (339, 497),
               'Beja': (257, 516),
               '<NAME>': (242, 546),
               'Cuba': (255, 494),
               '<NAME>': (230, 503),
               'Moura': (305, 493),
               'Mértola': (272, 557),
               'Odemira': (193, 561),
               'Ourique': (220, 548),
               'Serpa': (292, 524),
               'Vidigueira': (276, 495),  #Évora
               'Alandroal': (303, 444),
               'Arraiolos': (252, 423),
               'Borba': (291, 424),
               'Estremoz': (285, 416),
               'Montemor-o-Novo': (229, 435),
               'Mora': (229, 435),
               'Mourão': (307, 472),
               'Portel': (270, 479),
               'Redondo': (281, 438),
               '<NAME>': (292, 467),
               '<NAME>': (206, 434),
               '<NAME>': (226, 468),
               '<NAME>': (301, 425),
               'Évora': (254, 452),  #Portalegre
               '<NAME>': (268, 370),
               'Arronches': (314, 384),
               'Avis': (256, 392),
               '<NAME>': (335, 398),
               '<NAME>': (292, 345),
               'Crato': (279, 361),
               'Elvas': (319, 406),
               'Fronteira': (283, 388),
               'Gavião': (258, 352),
               'Marvão': (303, 352),
               'Monforte': (298, 392),
               'Nisa': (275, 338),
               '<NAME>': (235, 378),
               'Portalegre': (299, 368),
               'Sousel': (266, 402),  #Faro
               'Albufeira': (222, 616),
               'Alcoutim': (276, 585),
               'Aljezur': (174, 593),
               '<NAME>': (287, 595),
               'Faro': (253, 627),
               'Lagoa': (206, 616),
               'Lagos': (179, 611),
               'Loulé': (240, 606),
               'Monchique': (195, 590),
               'Olhão': (265, 624),
               'Portimão': (193, 609),
               'Silves': (212, 600),
               'São Brás de Alportel': (256, 609),
               'Tavira': (276, 606),
               '<NAME>': (166, 618),
               '<NAME> de Santo António': (288, 603),  #Santarém
               'Abrantes': (230, 349),
               'Alcanena': (186, 345),
               'Almeirim': (188, 377),
               'Alpiarça': (194, 371),
               'Benavente': (173, 414),
               'Cartaxo': (176, 381),
               'Chamusca': (211, 367),
               'Constância': (218, 344),
               'Coruche': (203, 407),
               'Entroncamento': (204, 344),
               '<NAME>': (220, 313),
               'Golegã': (200, 354),
               'Mação': (251, 326),
               'Ourém': (197, 315),
               '<NAME>': (164, 360),
               '<NAME>': (184, 394),
               'Santarém': (183, 359),
               'Sardoal': (234, 332),
               'Tomar': (212, 328),
               '<NAME>': (195, 341),
               '<NAME>': (208, 342),  #Bragança
               '<NAME>': (338, 126),
               'Bragança': (360, 74),
               '<NAME>': (306, 140),
               'Freixo de Espada À Cinta': (347, 157),
               '<NAME>': (346, 105),
               '<NAME>': (383, 111),
               'Mirandela': (320, 110),
               'Mogadouro': (371, 123),
               '<NAME>': (332, 142),
               '<NAME>': (321, 127),
               'Vimioso': (372, 94),
               'Vinhais': (326, 68),  #Vila Real
               'Alijó': (293, 130),
               'Boticas': (274, 86),
               'Chaves': (292, 81),
               '<NAME>': (258, 142),
               '<NAME>': (255, 121),
               'Montalegre': (261, 72),
               'Murça': (295, 117),
               '<NAME>': (270, 146),
               '<NAME>': (260, 109),
               'Sabrosa': (280, 140),
               '<NAME>': (264, 140),
               'Valpaços': (302, 97),
               '<NAME> de Aguiar': (276, 108),
               'Vila Real': (269, 127),  #Guarda
               '<NAME>': (290, 190),
               'Almeida': (339, 209),
               '<NAME>': (300, 210),
               '<NAME>': (340, 180),
               '<NAME>': (290, 205),
               'Gouveia': (282, 221),
               'Guarda': (309, 220),
               'Manteigas': (287, 236),
               'Mêda': (312, 173),
               'Pinhel': (329, 196),
               'Sabugal': (340, 239),
               'Seia': (272, 236),
               'Trancoso': (305, 188),
               '<NAME>': (320, 159),  #Viseu
               'Armamar': (274, 153),
               '<NAME>': (247, 229),
               '<NAME>': (250, 177),
               'Cinfães': (238, 159),
               'Lamego': (262, 156),
               'Mangualde': (271, 213),
               '<NAME>': (280, 169),
               'Mortágua': (224, 231),
               'Nelas': (258, 219),
               '<NAME>': (223, 197),
               '<NAME>': (278, 202),
               'Penedono': (300, 170),
               'Resende': (250, 156),
               '<NAME>': (236, 236),
               'Sernancelhe': (292, 178),
               'Sátão': (275, 193),
               'São João da Pesqueira': (295, 147),
               'São Pedro do Sul': (239, 189),
               'Tabuaço': (284, 157),
               'Tarouca': (266, 165),
               'Tondela': (237, 219),
               'Vila Nova de Paiva': (267, 180),
               'Viseu': (254, 202),
               'Vouzela': (236, 204),  #Castelo Branco
               'Belmonte': (305, 243),
               'Castelo Branco': (290, 298),
               'Covilhã': (290, 250),
               'Fundão': (297, 266),
               'Idanha-a-Nova': (322, 289),
               'Oleiros': (258, 289),
               'Penamacor': (318, 261),
               'Proença-a-Nova': (259, 311),
               'Sertã': (239, 302),
               'Vila de Rei': (232, 318),
               'Vila Velha de Ródão': (275, 317),
}

# Process each pixel
logging.info('Processing pixels')
hazard = {}
for concelho, posicao in concelhos.items():
    hazard[concelho] = get_level(posicao[0], posicao[1])

logging.info('Saving json')
json.dump(hazard, open(os.path.dirname(os.path.realpath(__file__)) + '/html/hazard.json', 'w'))

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
generate_hazard.py -- Generates the hazard.json file with all the fire hazard information
Copyright (C) 2014 <NAME>
"""

# Logging
import logging

# System
import os

# HTTP
import requests

# Processing
from PIL import Image, ImageFile

try:
    from StringIO import StringIO
except ImportError:
    from io import BytesIO as StringIO
import operator

# Serializing
import json
import yaml

# Load the config
config = yaml.load(open(os.path.dirname(os.path.realpath(__file__)) + '/config.yml', 'r'))

# Set the loglevel
log_level = logging.ERROR
if config['debug']:
    log_level = logging.INFO
logging.basicConfig(format='[%(levelname)s] %(message)s', level=log_level)

# Download today's fire hazard image from the ipma website
logging.info('Downloading image')
r = requests.get('http://www.ipma.pt/resources.www/transf/indices/rcm_dh.jpg', timeout=10)

# Load the image's pixels into an array
ImageFile.LOAD_TRUNCATED_IMAGES = True
logging.info('Loading image')
im = Image.open(StringIO(r.content))
image = im.load()


def get_level(x, y):
    """
    Get the fire hazard level from a pixel by checking which level is closer to it's color
    """

    levels = {
        5: image[350, 570],
        4: image[350, 590],
        3: image[350, 610],
        2: image[350, 630],
        1: image[350, 650]
    }

    closest = (0, 196609)  # Level, Difference

    # Level, Level color
    try:
        items = levels.iteritems()
    except AttributeError:
        items = levels.items()

    for level, lc in items:
        pc = image[x, y]  # Pixel color
        difference = (pc[0] - lc[0]) ** 2 + (pc[1] - lc[1]) ** 2 + (pc[2] - lc[2]) ** 2

        if difference < closest[1]:
            closest = (level, difference)

    return closest[0]

# List of municipalities and location of a pixel that's within it's limits
concelhos = {  # Lisboa
               'Cascais': (118, 429),
               'Oeiras': (130, 430),
               'Sintra': (123, 419),
               'Amadora': (135, 426),
               'Lisboa': (142, 428),
               'Mafra': (127, 402),
               '<NAME>': (131, 388),
               'Lourinhã': (131, 369),
               'Cadaval': (151, 371),
               'Alenquer': (153, 385),
               'Azambuja': (166, 388),
               'Odivelas': (138, 422),
               'Loures': (140, 415),
               '<NAME>': (145, 402),
               'Sobral de Mont<NAME>': (143, 397),
               '<NAME>': (155, 409),  # Leiria
               'Peniche': (129, 358),
               'Bombarral': (143, 365),
               'Óbidos': (137, 355),
               '<NAME> Rainha': (150, 350),
               'Alcobaça': (162, 339),
               'Nazaré': (154, 328),
               '<NAME>': (159, 308),
               'Leiria': (177, 308),
               'Pombal': (185, 290),
               '<NAME>': (174, 336),
               'Batalha': (180, 324),
               'Ansião': (206, 288),
               'Alvaiázere': (210, 303),
               '<NAME>': (222, 297),
               '<NAME>': (228, 282),
               '<NAME>': (230, 291),  #Coimbra
               'Arganil': (250, 256),
               'Cantanhede': (190, 241),
               'Coimbra': (207, 257),
               'Condeixa-a-Nova': (201, 270),
               '<NAME>': (174, 257),
               'Góis': (238, 268),
               'Lousã': (226, 267),
               'Mira': (181, 233),
               '<NAME>': (217, 272),
               'Montemor-o-Velho': (187, 254),
               'Oliveira do Hospital': (260, 240),
               'P<NAME> Serra': (257, 271),
               'Penacova': (222, 248),
               'Penela': (214, 281),
               'Soure': (188, 272),
               'Tábua': (243, 245),
               'Vila Nova de Poiares': (224, 257),  #Aveiro
               'Águeda': (211, 215),
               'Albergaria-a-Velha': (205, 197),
               'Anadia': (208, 229),
               'Arouca': (226, 175),
               'Aveiro': (189, 204),
               '<NAME>': (221, 164),
               'Espinho': (191, 167),
               'Estarreja': (195, 195),
               'Ílhavo': (183, 210),
               'Mealhada': (206, 240),
               'Murtosa': (187, 195),
               '<NAME>': (204, 185),
               '<NAME>': (196, 223),
               'Ovar': (191, 181),
               '<NAME>': (200, 170),
               #'<NAME>': (203, 177), Removed because the image doesn't have enough resolution
               '<NAME>': (213, 195),
               'Vagos': (184, 221),
               '<NAME>': (216, 183),  #Porto
               'Amarante': (242, 134),
               'Baião': (247, 147),
               'Felgueiras': (229, 125),
               'Gondomar': (202, 152),
               'Lousada': (223, 133),
               'Maia': (192, 136),
               '<NAME>': (234, 146),
               'Matosinhos': (188, 142),
               '<NAME>': (214, 133),
               'Paredes': (212, 142),
               'Penafiel': (220, 149),
               'Porto': (193, 147),
               '<NAME>': (180, 117),
               '<NAME>': (207, 128),
               'Trofa': (192, 129),
               'Valongo': (203, 141),
               '<NAME>': (185, 127),
               'Vila Nova de Gaia': (194, 156),  # Braga
               'Amares': (215, 91),
               'Barcelos': (189, 102),
               'Braga': (208, 103),
               '<NAME>': (248, 103),
               '<NAME>': (242, 119),
               'Esposende': (180, 103),
               'Fafe': (232, 111),
               'Guimarães': (220, 112),
               '<NAME>': (223, 97),
               '<NAME>': (230, 80),
               '<NAME>': (238, 91),
               '<NAME>ova de Famalicão': (205, 116),
               '<NAME>': (204, 88),
               'Vizela': (221, 124),  #Viana do Castelo
               '<NAME>': (213, 61),
               'Caminha': (178, 67),
               'Melgaço': (225, 42),
               'Monção': (208, 47),
               '<NAME>': (196, 59),
               '<NAME>': (222, 72),
               'Ponte de Lima': (196, 75),
               'Valença': (194, 49),
               '<NAME>': (181, 82),
               'Vila Nova de Cerveira': (185, 59),  #Setúbal
               'Alcochete': (167, 431),
               '<NAME>': (206, 474),
               'Almada': (138, 439),
               'Barreiro': (153, 443),
               'Grândola': (197, 494),
               'Moita': (155, 437),
               'Montijo': (188, 428),
               'Palmela': (172, 440),
               '<NAME>': (198, 520),
               'Seixal': (144, 443),
               'Sesimbra': (144, 456),
               'Setúbal': (155, 453),
               'Sines': (177, 527),  #Beja
               'Aljustrel': (228, 527),
               'Almodôvar': (236, 573),
               'Alvito': (242, 487),
               'Barrancos': (339, 497),
               'Beja': (257, 516),
               '<NAME>': (242, 546),
               'Cuba': (255, 494),
               '<NAME>': (230, 503),
               'Moura': (305, 493),
               'Mértola': (272, 557),
               'Odemira': (193, 561),
               'Ourique': (220, 548),
               'Serpa': (292, 524),
               'Vidigueira': (276, 495),  #Évora
               'Alandroal': (303, 444),
               'Arraiolos': (252, 423),
               'Borba': (291, 424),
               'Estremoz': (285, 416),
               'Montemor-o-Novo': (229, 435),
               'Mora': (229, 435),
               'Mourão': (307, 472),
               'Portel': (270, 479),
               'Redondo': (281, 438),
               '<NAME>': (292, 467),
               '<NAME>': (206, 434),
               '<NAME>': (226, 468),
               '<NAME>': (301, 425),
               'Évora': (254, 452),  #Portalegre
               '<NAME>': (268, 370),
               'Arronches': (314, 384),
               'Avis': (256, 392),
               '<NAME>': (335, 398),
               '<NAME>': (292, 345),
               'Crato': (279, 361),
               'Elvas': (319, 406),
               'Fronteira': (283, 388),
               'Gavião': (258, 352),
               'Marvão': (303, 352),
               'Monforte': (298, 392),
               'Nisa': (275, 338),
               '<NAME>': (235, 378),
               'Portalegre': (299, 368),
               'Sousel': (266, 402),  #Faro
               'Albufeira': (222, 616),
               'Alcoutim': (276, 585),
               'Aljezur': (174, 593),
               '<NAME>': (287, 595),
               'Faro': (253, 627),
               'Lagoa': (206, 616),
               'Lagos': (179, 611),
               'Loulé': (240, 606),
               'Monchique': (195, 590),
               'Olhão': (265, 624),
               'Portimão': (193, 609),
               'Silves': (212, 600),
               'São Brás de Alportel': (256, 609),
               'Tavira': (276, 606),
               '<NAME>': (166, 618),
               '<NAME> de Santo António': (288, 603),  #Santarém
               'Abrantes': (230, 349),
               'Alcanena': (186, 345),
               'Almeirim': (188, 377),
               'Alpiarça': (194, 371),
               'Benavente': (173, 414),
               'Cartaxo': (176, 381),
               'Chamusca': (211, 367),
               'Constância': (218, 344),
               'Coruche': (203, 407),
               'Entroncamento': (204, 344),
               '<NAME>': (220, 313),
               'Golegã': (200, 354),
               'Mação': (251, 326),
               'Ourém': (197, 315),
               '<NAME>': (164, 360),
               '<NAME>': (184, 394),
               'Santarém': (183, 359),
               'Sardoal': (234, 332),
               'Tomar': (212, 328),
               '<NAME>': (195, 341),
               '<NAME>': (208, 342),  #Bragança
               '<NAME>': (338, 126),
               'Bragança': (360, 74),
               '<NAME>': (306, 140),
               'Freixo de Espada À Cinta': (347, 157),
               '<NAME>': (346, 105),
               '<NAME>': (383, 111),
               'Mirandela': (320, 110),
               'Mogadouro': (371, 123),
               '<NAME>': (332, 142),
               '<NAME>': (321, 127),
               'Vimioso': (372, 94),
               'Vinhais': (326, 68),  #Vila Real
               'Alijó': (293, 130),
               'Boticas': (274, 86),
               'Chaves': (292, 81),
               '<NAME>': (258, 142),
               '<NAME>': (255, 121),
               'Montalegre': (261, 72),
               'Murça': (295, 117),
               '<NAME>': (270, 146),
               '<NAME>': (260, 109),
               'Sabrosa': (280, 140),
               '<NAME>': (264, 140),
               'Valpaços': (302, 97),
               '<NAME> de Aguiar': (276, 108),
               'Vila Real': (269, 127),  #Guarda
               '<NAME>': (290, 190),
               'Almeida': (339, 209),
               '<NAME>': (300, 210),
               '<NAME>': (340, 180),
               '<NAME>': (290, 205),
               'Gouveia': (282, 221),
               'Guarda': (309, 220),
               'Manteigas': (287, 236),
               'Mêda': (312, 173),
               'Pinhel': (329, 196),
               'Sabugal': (340, 239),
               'Seia': (272, 236),
               'Trancoso': (305, 188),
               '<NAME>': (320, 159),  #Viseu
               'Armamar': (274, 153),
               '<NAME>': (247, 229),
               '<NAME>': (250, 177),
               'Cinfães': (238, 159),
               'Lamego': (262, 156),
               'Mangualde': (271, 213),
               '<NAME>': (280, 169),
               'Mortágua': (224, 231),
               'Nelas': (258, 219),
               '<NAME>': (223, 197),
               '<NAME>': (278, 202),
               'Penedono': (300, 170),
               'Resende': (250, 156),
               '<NAME>': (236, 236),
               'Sernancelhe': (292, 178),
               'Sátão': (275, 193),
               'São João da Pesqueira': (295, 147),
               'São Pedro do Sul': (239, 189),
               'Tabuaço': (284, 157),
               'Tarouca': (266, 165),
               'Tondela': (237, 219),
               'Vila Nova de Paiva': (267, 180),
               'Viseu': (254, 202),
               'Vouzela': (236, 204),  #Castelo Branco
               'Belmonte': (305, 243),
               'Castelo Branco': (290, 298),
               'Covilhã': (290, 250),
               'Fundão': (297, 266),
               'Idanha-a-Nova': (322, 289),
               'Oleiros': (258, 289),
               'Penamacor': (318, 261),
               'Proença-a-Nova': (259, 311),
               'Sertã': (239, 302),
               'Vila de Rei': (232, 318),
               'Vila Velha de Ródão': (275, 317),
}

# Process each pixel
logging.info('Processing pixels')
hazard = {}
for concelho, posicao in concelhos.items():
    hazard[concelho] = get_level(posicao[0], posicao[1])

logging.info('Saving json')
json.dump(hazard, open(os.path.dirname(os.path.realpath(__file__)) + '/html/hazard.json', 'w'))
#!/usr/bin/env python # -*- coding: utf-8 -*- generate_hazard.py -- Generates the hazard.json file with all the fire hazard information Copyright (C) 2014 <NAME> # Logging # System # HTTP # Processing # Serializing # Load the config # Set the loglevel # Download today's fire hazard image from the ipma website # Load the image's pixels into an array Get the fire hazard level from a pixel by checking which level is closer to it's color # Level, Difference # Level, Level color # Pixel color # List of municipalities and location of a pixel that's within it's limits # Lisboa # Leiria #Coimbra #Aveiro #'<NAME>': (203, 177), Removed because the image doesn't have enough resolution #Porto # Braga #Viana do Castelo #Setúbal #Beja #Évora #Portalegre #Faro #Santarém #Bragança #Vila Real #Guarda #Viseu #Castelo Branco # Process each pixel