# Path: irish_townlands/utils.py
# def m2_to_arp(area_m2):
#     area_acres = area_m2 / 4046.85642
# 
#     acres = int(area_acres)
#     subacres = area_acres - acres
# 
#     roods = int(subacres * 4)
#     perches = int((subacres * 4 - roods) * 40)
# 
#     return (acres, roods, perches)
# 
# def remove_prefixes(string, prefixes):
#     for i in range(1000):
#         for prefix in prefixes:
#             if string.startswith(prefix):
#                 string = string[len(prefix):]
#                 continue
#         else:
#             break
# 
#     return string
# 
# def remove_accents(string):
#     for a, b in [ (u"Á", "A"), (u"É", "E"), (u"Í", "I"), (u"Ó", "O"), (u"Ú", "U"),
#                  (u"á", "a"), (u"é", "e"), (u"í", "i"), (u"ó", "o"), (u"ú", "u") ]:
# 
#         string = string.replace(a, b)
# 
#     return string
# 
# def historic_names(tags):
#     table = []
#     for k, v in tags.items():
#         if not k.startswith("name:"):
#             continue
#         k = k[5:]
# 
#         period = parse_period(k)
#         if period is None:
#             continue
# 
#         # FIXME include source, and source url link
# 
#         table.append((period, v))
# 
#     table.sort()
# 
#     table = {'header': ['When', 'Name'], 'body': table, 'has_results': len(table) > 0 }
# 
#     # TODO add other tags, not just 'name'
#     return table


from collections import defaultdict
from django.db import models
from django.db.models import Sum, Q
from django.template.defaultfilters import slugify
from django.utils.translation import ungettext, ugettext
from django.utils import translation
from django.utils.html import format_html, mark_safe
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models
from .utils import m2_to_arp, remove_prefixes, remove_accents, historic_names
import math
import json
        elif desc == 'short':
            this_desc = self.short_desc
        else:
            raise NotImplementedError()

        # An area called "Foo or Bar" should be expanded to 2 entries, "Foo"
        # and "Bar". This code split it that way.
        def split_string(input_string):
            strings_to_split = [" and ", " or ", " agus ", u" nó ", ";", "/"]
            results = []
            for s in strings_to_split:
                if s in input_string:
                    names = input_string.split(s)
                    for name in names[1:]:
                        results.append(name.strip())

            for s in ['North', 'South', 'East', 'West', 'Upper', 'Lower']:
                if input_string.lower().endswith(" "+s.lower()):
                    # X North
                    other_part = input_string[0:-len(" "+s)]
                    results.append(s + " " + other_part)
                elif input_string.lower().startswith(" "+s.lower()):
                    # North X
                    other_part = input_string[len(" "+s)]
                    results.append(other_part + " " + s)

            return results

        # Given a name, convert it to sortable key
        def name_to_key(name):

# Path: irish_townlands/utils.py
# def m2_to_arp(area_m2):
#     area_acres = area_m2 / 4046.85642
# 
#     acres = int(area_acres)
#     subacres = area_acres - acres
# 
#     roods = int(subacres * 4)
#     perches = int((subacres * 4 - roods) * 40)
# 
#     return (acres, roods, perches)
# 
# def remove_prefixes(string, prefixes):
#     for i in range(1000):
#         for prefix in prefixes:
#             if string.startswith(prefix):
#                 string = string[len(prefix):]
#                 continue
#         else:
#             break
# 
#     return string
# 
# def remove_accents(string):
#     for a, b in [ (u"Á", "A"), (u"É", "E"), (u"Í", "I"), (u"Ó", "O"), (u"Ú", "U"),
#                  (u"á", "a"), (u"é", "e"), (u"í", "i"), (u"ó", "o"), (u"ú", "u") ]:
# 
#         string = string.replace(a, b)
# 
#     return string
# 
# def historic_names(tags):
#     table = []
#     for k, v in tags.items():
#         if not k.startswith("name:"):
#             continue
#         k = k[5:]
# 
#         period = parse_period(k)
#         if period is None:
#             continue
# 
#         # FIXME include source, and source url link
# 
#         table.append((period, v))
# 
#     table.sort()
# 
#     table = {'header': ['When', 'Name'], 'body': table, 'has_results': len(table) > 0 }
# 
#     # TODO add other tags, not just 'name'
#     return table


# Path: irish_townlands/models.py
from collections import defaultdict
from django.db import models
from django.db.models import Sum, Q
from django.template.defaultfilters import slugify
from django.utils.translation import ungettext, ugettext
from django.utils import translation
from django.utils.html import format_html, mark_safe
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models
from .utils import m2_to_arp, remove_prefixes, remove_accents, historic_names
import math
import json

        elif desc == 'short':
            this_desc = self.short_desc
        else:
            raise NotImplementedError()

        # An area called "Foo or Bar" should be expanded to 2 entries, "Foo"
        # and "Bar". This code split it that way.
        def split_string(input_string):
            strings_to_split = [" and ", " or ", " agus ", u" nó ", ";", "/"]
            results = []
            for s in strings_to_split:
                if s in input_string:
                    names = input_string.split(s)
                    for name in names[1:]:
                        results.append(name.strip())

            for s in ['North', 'South', 'East', 'West', 'Upper', 'Lower']:
                if input_string.lower().endswith(" "+s.lower()):
                    # X North
                    other_part = input_string[0:-len(" "+s)]
                    results.append(s + " " + other_part)
                elif input_string.lower().startswith(" "+s.lower()):
                    # North X
                    other_part = input_string[len(" "+s)]
                    results.append(other_part + " " + s)

            return results

        # Given a name, convert it to sortable key
        def name_to_key(name):

            key = remove_prefixes(name, ['An t-', 'An t', 'An ', 'Na h-', 'Na h', 'Na '])