def get_roadevent_from_summary(summary):

    elem = E.RoadEvent(id=JURISDICTION + ":" + summary["id"])

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

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

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

    root = lxml.html.fragment_fromstring(resp.read().decode("utf8"))
    set_val("Title", _get_text_from_elems(root.cssselect("#tdIdentification")))
    set_val("Description", _get_text_from_elems(root.cssselect("#tdDescriptionEntrave,#tdDetail")))
    set_val("AffectedRoads", _get_text_from_elems(root.cssselect("#tdLocalisation")))
    set_val("TrafficRestrictions", _get_text_from_elems(root.cssselect("#tdRestrictionCamionnage")))

    start_date = _get_text_from_elems(root.cssselect("#tdDebut"))
    end_date = _get_text_from_elems(root.cssselect("#tdFin"))
    if start_date:
        set_val("StartDate", _str_to_date(start_date))
    if end_date:
        set_val("EndDate", _str_to_date(end_date))

    return elem
Example #2
0
 def to_full_xml_element(self):
     el = deepcopy(self.xml_elem)
     geom = etree.Element('Geometry')
     geom.append(serialization.geom_to_xml_element(self.geom))
     el.append(geom)
     el.set('id', self.compound_id)
     return el
Example #3
0
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...
    geom_hash = hashlib.md5(feature.geom.wkt).hexdigest()
    id = JURISDICTION + ':' + geom_hash
    while id in ids_seen:
        id += 'x'
    ids_seen.add(id)

    elem = E.RoadEvent(id=id)

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

    set_val('Title', feature.get('Name').decode('utf8'))

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

    description_label = blob.xpath('//strong[text()="Description"]')
    if description_label:
        description_bits = []
        el = description_label[0].getnext()
        while el.tag == 'p':
            description_bits.append(_get_el_text(el))
            el = el.getnext()
        set_val('Description', '\n\n'.join(description_bits))

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

    try:
        set_val('ExternalURL', blob.cssselect('#avis_residants a, #en_savoir_plus a')[0].get('href'))
    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
            if start_date and end_date:
                set_val('StartDate', _fr_string_to_date(start_date))
                set_val('EndDate', _fr_string_to_date(end_date))
        except IndexError:
            pass

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

    return elem