Exemplo n.º 1
0
def _find_coordinates_by_address(dictionary):
    if "district_number" in dictionary:
        if dictionary["district_number"] != "":
            dictionary[
                "district_number"] = "Praha " + dictionary["district_number"]

    first = True
    sql = "SELECT " + ADDRESSPOINTS_COLUMNS_FIND_COORD + \
          " FROM " + ADDRESSPOINTS_TABLE_NAME + \
          " WHERE "
    for key in dictionary:
        if dictionary[key] != "":
            if first:
                sql += ITEM_TO_FIELD[key] + " = '" + dictionary[key] + "'"
                first = False
            else:
                sql += " AND " + ITEM_TO_FIELD[key] + " = '" + dictionary[
                    key] + "'"

    sql += "LIMIT " + str(MAX_COUNT)
    cur = execute_sql(DATABASE_NAME_RUIAN, sql)
    rows = cur.fetchall()
    coordinates = []
    localities = []
    for row in rows:
        if (row[0] is not None) and (row[1] is not None):
            (house_number,
             record_number) = analyse_row(row[7], number_to_string(row[6]))
            coordinates.append(
                (str("{:10.2f}".format(row[0])).strip(),
                 str("{:10.2f}".format(row[1])).strip(), row[2], row[3],
                 none_to_string(row[4]), none_to_string(row[5]),
                 house_number, record_number, number_to_string(row[8]),
                 none_to_string(row[9]), number_to_string(row[10]),
                 number_value(none_to_string(row[11]))))
            # latitude, longitude, gid, nazev_obce, nazev_casti_obce, nazev_ulice, cislo_domovni, typ_so,
            # cislo_orientacni, znak_cisla_orientacniho, psc, nazev_mop
            coord = Coordinates(row[0], row[1])
            addr = Address(row[5], house_number, record_number, row[8], row[9],
                           row[10], row[3], row[4], None, row[11], row[2])
            loc = Locality(addr, coord)
            localities.append(loc)
        else:
            # co se ma stat kdyz adresa nema souradnice?
            pass
    cur.close()
    # return coordinates
    return localities
Exemplo n.º 2
0
def validate_address(text_format, street, house_number, record_number,
                     orientation_number, orientation_number_character,
                     zip_code, locality, locality_part, district_number,
                     district_name):
    (street, house_number, record_number, orientation_number,
     orientation_number_character, zip_code, locality, locality_part,
     district_number, district_name) = none_to_string(
         (street, house_number, record_number, orientation_number,
          orientation_number_character, zip_code, locality, locality_part,
          district_number, district_name))
    if not right_address(street, house_number, record_number,
                         orientation_number, orientation_number_character,
                         zip_code, locality, locality_part, district_number,
                         district_name):
        return "False"

    print("right_address OK")
    dictionary = build_validate_dict(street, house_number, record_number,
                                     orientation_number,
                                     orientation_number_character, zip_code,
                                     locality, locality_part, district_number,
                                     district_name)

    result = _validate_address(dictionary)
    return list_to_response_text(text_format, result)
Exemplo n.º 3
0
def compile_address_as_dictionary_en(
        street, house_number, record_number, orientation_number, orientation_number_character, zip_code,
        locality, locality_part, district_number, ruian_id=""):
    (
        street, house_number, record_number, orientation_number, orientation_number_character, zip_code,
        locality, locality_part, district_number, ruian_id) = none_to_string(
        (street, house_number, record_number, orientation_number, orientation_number_character, zip_code,
         locality, locality_part, district_number, ruian_id))
    out = {}
    if house_number != "":
        sign = "house_number"
        address_number = house_number
    else:
        sign = "record_number"
        address_number = record_number
    if orientation_number != "":
        out[sign] = address_number
        out["orientation_number"] = orientation_number + orientation_number_character
    else:
        out[sign] = address_number
    if street != "":
        out["street"] = street
    if district_number != "":
        out["district_number"] = district_number
    else:
        out["district_number"] = ""

    if locality == locality_part or locality_part == "":
        out["locality"] = "%s %s" % (locality, str(district_number))
    else:
        out["locality"] = "%s %s" % (locality, str(district_number))
        out["locality_part"] = locality_part
    if ruian_id != "":
        out["ruian_id"] = ruian_id
    return out
Exemplo n.º 4
0
def _find_coordinates(identifier):
    sql = "SELECT " + ADDRESSPOINTS_COLUMNS_FIND_COORD + \
          " FROM " + ADDRESSPOINTS_TABLE_NAME + \
          " WHERE gid = " + str(identifier)
    cur = execute_sql(DATABASE_NAME_RUIAN, sql)
    row = cur.fetchone()
    if row and row[0] is not None and row[1] is not None:
        (house_number, record_number) = analyse_row(row[7],
                                                    number_to_string(row[6]))
        c = (str("{:10.2f}".format(row[1])).strip(),
             str("{:10.2f}".format(row[0])).strip(), row[2], row[3],
             none_to_string(row[4]), none_to_string(row[5]), house_number,
             record_number, number_to_string(row[8]), none_to_string(row[9]),
             number_to_string(row[10]), number_value(none_to_string(row[11])))
        return [c]
    else:
        return []
Exemplo n.º 5
0
def addresses_to_text(list_of_addresses, line_separator="\n"):
    result = ""
    for line in list_of_addresses:
        orientation_number = none_to_string(line[6])
        if orientation_number != "":
            house_numbers = none_to_string(
                line[5]) + "/" + orientation_number + none_to_string(line[7])
        else:
            house_numbers = none_to_string(line[5])
        street = none_to_string(line[3])
        if street != "":
            street += " "
        town = none_to_string(line[1])
        district = none_to_string(line[2])
        if town == district:
            town_district = town
        else:
            town_district = town + "-" + district
        result += none_to_string(line[0]) + " " + street + none_to_string(
            line[4]
        ) + " " + house_numbers + ", " + town_district + ", " + none_to_string(
            line[8]) + line_separator
    return result
Exemplo n.º 6
0
def addresses_to_xml(list_of_addresses, line_separator="\n", tag="Adresa"):
    result = '<?xml version="1.0" encoding="UTF-8"?>' + line_separator + "<xml>" + line_separator

    index = 0
    for line in list_of_addresses:
        orientation_number = none_to_string(line[6])
        sign = none_to_string(line[4])
        if orientation_number != "":
            house_numbers = "\t<" + sign + ">" + none_to_string(line[5]) + \
                            "</" + sign + ">" + line_separator + "\t<orientacni_cislo>" + \
                            orientation_number + none_to_string(line[7]) + "</orientacni_cislo>"
        else:
            house_numbers = "\t<" + sign + ">" + none_to_string(
                line[5]) + "</" + sign + ">"

        index = index + 1
        street = none_to_string(line[3])

        if street != "":
            street = "\t<ulice>" + street + "</ulice>" + line_separator

        town = none_to_string(line[1])
        district = none_to_string(line[2])

        if town == district or district == "":
            town_district = "\t<obec>" + town + "</obec>"
        else:
            town_district = "\t<obec>" + town + "</obec>" + line_separator + \
                            "\t<cast_obce>" + district + "</cast_obce>"

        result += "<" + tag + str(index) + ">" + line_separator + "<ID>" + none_to_string(line[0]) + \
                  "</ID>" + line_separator + town_district + line_separator + street + house_numbers + \
                  line_separator + "\t<PSČ>" + none_to_string(line[8]) + \
                  "</PSČ>" + line_separator + "</" + tag + str(index) + ">" + line_separator
    result += "</xml>"
    return result
Exemplo n.º 7
0
def addresses_to_json(list_of_addresses, line_separator="\n", tag="Adresa"):
    out: str = "{"
    index = 0
    for line in list_of_addresses:
        index += 1
        if index > 1:
            out += ','

        orientation_number = none_to_string(line[6])
        sign = none_to_string(line[4])
        if orientation_number != "":
            house_numbers = '\t"' + sign + '": ' + none_to_string(
                line[5]
            ) + ',' + line_separator + '\t"orientační_číslo":' + orientation_number + none_to_string(
                line[7]) + ','
        else:
            house_numbers = '\t"' + sign + '": ' + none_to_string(
                line[5]) + ','

        street = none_to_string(line[3])

        if street != "":
            street = '\t"ulice": ' + street + "," + line_separator

        town = none_to_string(line[1])
        district = none_to_string(line[2])

        if town == district or district == "":
            town_district = '\t"obec" : ' + town + ","
        else:
            town_district = '\t"obec" : ' + town + "," + line_separator + '\t"část_obce": ' + district + ","

        out += line_separator + '"' + tag + str(
            index
        ) + '" : {' + line_separator + '\t"ID": ' + none_to_string(
            line[0]
        ) + line_separator + town_district + line_separator + street + house_numbers + line_separator + '\t"PSČ" :' + none_to_string(
            line[8]) + line_separator + "\t}"
    out += line_separator + "}"
    return out
Exemplo n.º 8
0
def _find_address(identifier):
    sql = "SELECT " + ADDRESSPOINTS_COLUMNS_FIND + \
          " FROM " + ADDRESSPOINTS_TABLE_NAME + \
          " WHERE gid = " + str(identifier)
    cur = execute_sql(DATABASE_NAME_RUIAN, sql)
    row = cur.fetchone()
    if row:
        (house_number, record_number) = analyse_row(row[2],
                                                    number_to_string(row[1]))
        a = number_value(none_to_string(row[8]))
        address = Address(none_to_string(row[0]), house_number, record_number,
                          number_to_string(row[3]), none_to_string(row[4]),
                          number_to_string(row[5]), none_to_string(row[6]),
                          none_to_string(row[7]), a, none_to_string(row[8]),
                          identifier)
        return address
    else:
        return None
Exemplo n.º 9
0
def build_address(builder, candidates, with_id, with_distance=False):
    items = []
    for item in candidates:
        if item[4] == "č.p.":
            house_number = str(item[5])
            record_number = ""
        else:
            house_number = ""
            record_number = str(item[5])

        mop = none_to_string(item[9])
        if mop != "":
            pom = mop.split()
            district_number = pom[1]
        else:
            district_number = ""

        # TODO compiled address
        sub_str = compile_address(text_format=builder,
                                  street=none_to_string(item[3]),
                                  house_number=house_number,
                                  record_number=record_number,
                                  orientation_number=none_to_string(item[6]),
                                  orientation_number_character=none_to_string(
                                      item[7]),
                                  zip_code=str(item[8]),
                                  locality=none_to_string(item[1]),
                                  locality_part=none_to_string(item[2]),
                                  district_number=district_number,
                                  district_name=mop)

        if with_id:
            sub_str = add_id("id", str(item[0]), sub_str, builder)
        if with_distance:
            sub_str = add_id("distance", str(item[10]), sub_str, builder)
        items.append(sub_str)
    return items
Exemplo n.º 10
0
def compile_address_to_one_row(
        street, house_number, record_number, orientation_number, orientation_number_character, zip_code,
        locality, locality_part, district_number, ruian_id=""):
    street, house_number, record_number, orientation_number, orientation_number_character, zip_code, locality, locality_part, district_number, ruian_id = none_to_string(
        (street, house_number, record_number, orientation_number, orientation_number_character, zip_code,
         locality, locality_part, district_number, ruian_id))

    address_str = ""
    zip_code = formatzip_code(zip_code)
    house_number = empty_string_if_no_number(house_number)
    orientation_number = empty_string_if_no_number(orientation_number)
    district_number = empty_string_if_no_number(district_number)
    orientation_number_character = alpha_check(orientation_number_character)

    town_info = zip_code + " " + locality
    # unicode(locality, "utf-8")
    if district_number != "":
        town_info += " " + district_number

    if house_number != "":
        house_number_str = " " + house_number
        if orientation_number != "":
            house_number_str += u"/" + orientation_number + orientation_number_character
    elif record_number != "":
        house_number_str = u" č.ev. " + record_number
    else:
        house_number_str = ""

    if locality.upper() == "PRAHA":
        if str_is_not_empty(street):
            address_str += street + house_number_str + ", " + locality_part + ", " + town_info
        else:
            address_str += locality_part + house_number_str + ", " + town_info
    else:
        if str_is_not_empty(street):
            address_str += street + house_number_str + ", "
            if locality_part != locality:
                address_str += locality_part + ", "
            address_str += town_info
        else:
            if locality_part != locality:
                address_str += locality_part + house_number_str + ", "
            else:
                if house_number != "":
                    address_str += u"č.p." + house_number_str + ", "
                else:
                    address_str += house_number_str[1:] + ", "
            address_str += town_info

    if ruian_id != "":
        address_str = "%s, %s" % (str(ruian_id), address_str)

    return address_str