def get_roadevent_from_summary(summary):

    elem = E.event(E.id("%s/%s" % (JURISDICTION_ID, summary['id'])))

    elem.append(
        E.geography(
            geom_to_xml_element(
                Point(float(summary['lng']), float(summary['lat']), srid=4326)
            )
        )
    )

    url = BASE_DETAIL_URL + summary['id']
    resp = urlopen(url)
    time.sleep(0.5)

    def set_val(tag, val):
        if val not in (None, ''):
            e = etree.Element(tag)
            e.text = str(val)
            elem.append(e)

    set_val('status', 'ACTIVE')
    set_val('event_type', 'CONSTRUCTION')
    set_val('severity', 'MAJOR' if summary['id'].startswith('maj') else 'MODERATE')

    root = lxml.html.fragment_fromstring(resp.read().decode('utf8'))
    set_val('headline', _get_text_from_elems(root.cssselect('#tdIdentification')))

    description = _get_text_from_elems(root.cssselect('#tdDescriptionEntrave,#tdDetail'))
    affected_roads = _get_text_from_elems(root.cssselect('#tdLocalisation'))
    traffic_restrictions = _get_text_from_elems(root.cssselect('#tdRestrictionCamionnage'))

    if affected_roads:
        description += u'\n\nLocalisation: ' + affected_roads
    if traffic_restrictions:
        description += u'\n\nRestrictions: ' + traffic_restrictions
    set_val('description', description)

    start_date = _get_text_from_elems(root.cssselect('#tdDebut'))
    end_date = _get_text_from_elems(root.cssselect('#tdFin'))
    if start_date:
        sked = E.recurring_schedule()
        sked.append(E.start_date(str(_str_to_date(start_date))))
        if end_date:
            sked.append(E.end_date(str(_str_to_date(end_date))))
        elem.append(E.schedule(E.recurring_schedules(sked)))

    return elem
def feature_to_open511_element(feature):
    """Transform an OGR Feature from the KML input into an XML Element for a RoadEvent."""

    # Using a hash of the geometry for an ID. For proper production use,
    # there'll probably have to be some code in the importer
    # that compares to existing entries in the DB to determine whether
    # this is new or modified...
    id = hashlib.md5(feature.geom.wkt.encode('ascii')).hexdigest()
    while id in ids_seen:
        id += 'x'
    ids_seen.add(id)

    elem = E.event()

    def set_val(tag, val):
        if val not in (None, ''):
            e = etree.Element(tag)
            e.text = str(val)
            elem.append(e)

    def maybe_decode(s):
        if isinstance(s, str):
            return s
        return s.decode('utf8')

    set_val('id', "%s/%s" % (JURISDICTION_ID, id))
    set_val('status', 'ACTIVE')
    set_val('event_type', 'CONSTRUCTION')
    set_val('severity', 'UNKNOWN')

    set_val('headline', maybe_decode(feature.get('Name')))

    blob = lxml.html.fragment_fromstring(maybe_decode(feature.get('Description')),
        create_parent='content')

    description_label = blob.xpath('//strong[text()="Description"]')
    localisation = blob.cssselect('div#localisation p')
    if description_label or localisation:
        description_bits = []
        if description_label:
            el = description_label[0].getnext()
            while el.tag == 'p':
                description_bits.append(_get_el_text(el))
                el = el.getnext()

        if localisation:
            description_bits.append('Localisation: ' + '; '.join(
                _get_el_text(el) for el in localisation))

        set_val('description', '\n\n'.join(description_bits))

    try:
        link = blob.cssselect('#avis_residants a, #en_savoir_plus a')[0]
        e = etree.Element('link')
        e.set('rel', 'related')
        e.set('href', link.get('href'))
        if link.get('title'):
            e.set('title', link.get('title'))
        elem.append(E.attachments(e))
    except IndexError:
        pass

    facultatif = blob.cssselect('div#itineraire_facult p')
    if facultatif:
        set_val('detour', '\n\n'.join(_get_el_text(el) for el in facultatif))

    if blob.cssselect('div#dates strong'):
        try:
            start_date = blob.xpath(u'div[@id="dates"]/strong[text()="Date de d\xe9but"]')[0].tail
            end_date = blob.xpath(u'div[@id="dates"]/strong[text()="Date de fin"]')[0].tail
            start_date = _fr_string_to_date(start_date)
            end_date = _fr_string_to_date(end_date)
            if start_date:
                sked = E.recurring_schedule(E.start_date(str(start_date)))
                if end_date:
                    sked.append(E.end_date(str(end_date)))
                elem.append(E.schedule(E.recurring_schedules(sked)))
        except IndexError:
            pass

    elem.append(E.geography(
        geom_to_xml_element(feature.geom)
    ))

    return elem