Tag Processing Snippets

From OpenStreetMap Wiki
Jump to: navigation, search

We collect here JavaScript snippets that generate useful text from the tags and context of an object.

    function getIdentificatorString(osm_element)
    {
        if (osm_element.tags)
        {
            if (osm_element.tags.name)
                return osm_element.tags.name;
            else if (osm_element.tags.operator)
                return osm_element.tags.operator;
        }
            
        return null;
    }
    var nominatim_identfier = "email=MY@EMAIL";

    function getPostalAdress(osm_element)
    {
        if (osm_element.tags)
        {
            if (osm_element.tags["addr:street"] && osm_element.tags["addr:housenumber"])
                return osm_element.tags["addr:street"] + " "
                    + osm_element.tags["addr:housenumber"];
            else if (osm_element.type && osm_element.id)
            {
                var type_letter = (osm_element.type == "node" ? "N"
                    : (osm_element.type == "way" ? "W" : "R"));
                var nominatim_req = OpenLayers.Request.GET({
                    url: "http://nominatim.openstreetmap.org/reverse?"
                        + nominatim_identfier + "&format=json"
                        + "&osm_type=" + type_letter + "&osm_id=" + osm_element.id,
                    async: false
                });
                var nominatim_result = JSON.parse(nominatim_req.responseText);
                
                var nominatim_road = "";
                if (nominatim_result.address && nominatim_result.address.road)
                    nominatim_road = nominatim_result.address.road;
                else if (nominatim_result.address && nominatim_result.address.pedestrian)
                    nominatim_road = nominatim_result.address.pedestrian;
                else if (nominatim_result.address && nominatim_result.address.footway)
                    nominatim_road = nominatim_result.address.footway;
                else if (nominatim_result.address && nominatim_result.address.cycleway)
                    nominatim_road = nominatim_result.address.cycleway;
                else if (nominatim_result.address && nominatim_result.address.path)
                    nominatim_road = nominatim_result.address.path;
                    
                var nominatim_house_number = "";
                if (nominatim_result.address && nominatim_result.address.house_number)
                    nominatim_house_number = nominatim_result.address.house_number;
                    
                return nominatim_road + " " + nominatim_house_number;
            }
        }
    }