from string import capwords

'''
Translate function used with ogr2osm for Miami-Date Large Building Footprints import.

https://github.com/jlevente/MiamiOSM-buildings

Fields used:

FIELD           DESC                    OSM_TAG
objectid        id of feature           miami_buildings:objectid
height          height of building      height
zip             zip code                addr:postcode
city            mailing municipality    addr:city
street          street                  addr:street
house_num       house number            addr:housenumber
'''

def pretty_type(type):
    types_dict = {
        "CSWY": "Causeway",
        "AVE": "Avenue",
        "ST": "Street",
        "RD": "Road",
        "TER": "Terrace",
        "PKWY": "Parkway",
        "PLZ": "Plaza",
        "LN": "Lane",
        "PSGE": "Passage",
        "TRL": "Trail",
        "PATH": "Path",
        "HWY": "Highway",
        "CIR": "Circle",
        "BLVD": "Boulevard",
        "WAY": "Way",
        "DR": "Drive",
        "EXT": "Extension",
        "PL": "Place",
        "CONC": "Concession",
        "PASS": "Pass",
        "CT": "Court"
    }
    return types_dict[type]

def filterTags(attrs):
    if not attrs:
        return
    tags = {}
    
    if 'height' in attrs:
        tags['height'] = attrs['height']

    if 'objectid' in attrs:
        tags['miami_buildings:objectid'] = attrs['objectid']

    if 'zip' in attrs:
        tags['addr:postcode'] = attrs['zip']

    if 'city' in attrs:
        if len(attrs['city']) > 0:
            tags['addr:city'] = capwords(attrs['city'])

    street = []

    if 'pre_dir' in attrs:
        if len(attrs['pre_dir']) > 0:
            street.append(attrs['pre_dir'])

    if 'st_name' in attrs:
        if len(attrs['st_name']) > 0:
            street.append(capwords(attrs['st_name'].lower()))

    if 'st_type' in attrs:
        if len(attrs['st_type']) > 0:
            street.append(pretty_type(attrs['st_type']))

    if 'suf_dir' in attrs:
        if len(attrs['suf_dir']) > 0:
            street.append(attrs['suf_dir'])

    street_name = ' '.join(street)
    if street_name is not '':
        tags['addr:street'] = street_name

    if 'house_num' in attrs:
        tags['addr:housenumber'] = attrs['house_num']

    tags['building'] = 'yes'
    tags['source'] = 'Miami Building Import 2016'

    return tags

@@ -1248,32 +1248,120 @@
 ight' in attrs:%0A
+        if attrs%5B'height'%5D is not None:%0A            # Convert feet to meters, round%0A    
         tags%5B'he
@@ -1365,24 +1365,30 @@
 'height'%5D = 
+round(
 attrs%5B'heigh
@@ -1390,16 +1390,29 @@
 height'%5D
+ * 0.3048, 1)
 %0A%0A    if
@@ -1510,32 +1510,70 @@
 'zip' in attrs:%0A
+        if len(attrs%5B'zip'%5D) %3E 0:%0A    
         tags%5B'ad
@@ -2335,32 +2335,76 @@
 _num' in attrs:%0A
+        if len(attrs%5B'house_num'%5D) %3E 0:%0A    
         tags%5B'ad

convert ft to m, dont export addr if not set