示例#1
0
def ReadLinks(fileName, delimiter_=','):
    """ read link input file and set up link objects.
    
    This function will automatically create an internal link id for each link. 
    Internal link IDs are consecutive non-negative integers starting from 0.
    """
    global _dict_links
    global _dict_nodes

    with open(fileName) as f:
        # skip the header
        next(f)
        csvf = csv.reader(f, delimiter=delimiter_)
        # internal link id used for calculation
        linkID = 0
        for r in csvf:
            linkUID = r[0].strip()
            origNodeID = GetNodeID(r[1])
            destNodeID = GetNodeID(r[2])
            linkLen = float(r[3])
            pLink = Link(linkID, linkUID, origNodeID, destNodeID, linkLen)
            _dict_links[linkID] = pLink
            # update outgoing links from orig node
            _dict_nodes[origNodeID].AddOutgoingLinks(linkID)
            linkID += 1
示例#2
0
def embedly_link(URL):
    #Takes a URL, returns a populated Link object
    #Returns "error" if Embedly API fails
    client = Embedly(embedly_key)
    response = client.extract(URL)
    try:
        #Get link information from Embedly
        headline = response['title']
        URL = clean_url(response['url'])
        summary = response['description']
        source = response['provider_name']
        path = page_path(headline)
        keywords_response = response['keywords']
        keyword1 = str(keywords_response[0]['name'])
        keyword2 = str(keywords_response[1]['name'])
        keyword3 = str(keywords_response[2]['name'])
        keyword4 = str(keywords_response[3]['name'])
        keyword5 = str(keywords_response[4]['name'])

        #Create Link object with info from Embedly
        link = Link(headline=headline,
                    url=URL,
                    source=source,
                    summary=summary,
                    path=path,
                    keyword1=keyword1,
                    keyword2=keyword2,
                    keyword3=keyword3,
                    keyword4=keyword4,
                    keyword5=keyword5,
                    votes=0)
        return link
    except:
        return "error"
示例#3
0
def get_content(html):
    soup = BeautifulSoup(html, "html.parser")
    g_divs = soup.select('div[class="g"]')
    link_list = []
    for item in g_divs:
        desc = item.find("span", class_="aCOpRe").text
        name = item.find("h3", class_="LC20lb").text
        url = HOST + item.find("div", class_="TbwUpd").text
        link_list.append(Link(name, url, desc))
    return link_list
示例#4
0
def _read_links(input_dir, node_list, link_list, node_id_to_no_dict,
                link_id_to_no_dict):
    with open(input_dir + '/link.csv', 'r', encoding='utf-8') as link_fp:
        print('\nread link.csv')

        reader = csv.DictReader(link_fp)
        link_seq_no = 0
        for line in reader:
            # it can be an empty string
            link_id = line['link_id']

            # check the validity
            from_node_id = _convert_str_to_int(line['from_node_id'])
            if from_node_id is None:
                continue

            to_node_id = _convert_str_to_int(line['to_node_id'])
            if to_node_id is None:
                continue

            link_cost = _convert_str_to_float(line['link_cost'])
            if link_cost is None:
                continue

            try:
                from_node_seq_no = node_id_to_no_dict[from_node_id]
                to_node_seq_no = node_id_to_no_dict[to_node_id]
            except KeyError:
                print(f"EXCEPTION: Node ID {from_node_id} "
                      f"or/and Node ID {to_node_id} NOT IN THE NETWORK!!")
                continue

            try:
                geometry = line['geometry']
            except KeyError:
                geometry = ''

            link_id_to_no_dict[link_id] = link_seq_no

            # construct link object
            link = Link(link_id, link_seq_no, from_node_seq_no, to_node_seq_no,
                        from_node_id, to_node_id, link_cost, geometry)

            link_list.append(link)

            from_node = node_list[from_node_seq_no]
            to_node = node_list[to_node_seq_no]

            from_node.add_outgoing_link(link)
            to_node.add_incoming_link(link)

            link_seq_no += 1

        print(f"the number of links is {link_seq_no}")
示例#5
0
        XS_dat[i].len = 1
    list_km = []
# wykrycie polaczen przekroii od lewej do prawej, stworzenie listy obiektow typu link
linki = []
for object1 in XS_dat:
    lewa = object1.left
    for object2 in XS_dat:
        if abs(float(lewa[0]) - float(object2.right[0])) < 2 and abs(
                float(lewa[1]) - float(object2.right[1])
        ) < 2 and object1.river_code != object2.river_code:
            if "LTZ" in object1.river_code and "PTZ" in object2.river_code:
                print("blad: LTZ polaczony z PTZ")
            elif "PTZ" in object1.river_code and "LTZ" in object2.river_code:
                print("blad: PTZ polaczony z LTZ")
            else:
                linki.append(Link(object1, object2))
licz_lin = len(linki)
defined = 0
# Nadanie parametrow przekroja
# do refaktoryzacji - 4 krotne wywolanie tego samego
for element in linki:
    if "TZ_" not in element.river1 and "TZ_" in element.river2:
        # print(element.river1, element.chain1, element.river2, element.chain2)
        element.rzad = 1
        element.kolej = 1
        element.main_chan = str.upper(element.object1.river_code[:3])
        element.main_km = element.object1.km
        element.topo = element.object1.reach_code
        defined += 1
    elif "TZ_" in element.river1 and "TZ_" not in element.river2:
        element.rzad = 1
示例#6
0
def expand_product(shape_tool, product, shapes_simples, deep, doc_id,
                   product_root, product_id, file_path):
    """
    :param shape_tool: :class:`.OCC.XCAFDoc.XCAFDoc_ShapeTool`
    :param product: :class:`.Product` that will be expanded
    :param shapes_simples: list of :class:`.SimpleShape`
    :param deep: Depth of the node that we explore
    :param doc_id: id that references a :class:`.DocumentFile` of which we are generating the :class:`.Product`
    :param product_root: :class:`.Product` root of the arborescense


    We are going to expand a :class:`.Product` (**product**) from the :class:`.OCC.TDF.TDF_Label` who identifies it (**product**.label_reference)


    If the **product** is an assembly:

        * We generate the **list** of the :class:`.OCC.TDF.TDF_Label` that define it

        * for each :class:`.OCC.TDF.TDF_Label` in **list**:

            - We generate a new :class:`.document3D.classes.Link`  or if two or more :class:`.OCC.TDF.TDF_Label` of the list are partner, add an occurrence extra to the :class:`.document3D.classes.Link` that already was generated

            - The :class:`.document3D.classes.Link` is going to point at a new :class:`.Product` or, if the :class:`.Product` is already present in **product_root**, at the already definite product

            - If the :class:`.document3D.classes.Link` pointed at a new :class:`.Product` we need to expand it

                * The atribute **label_reference** of the new :class:`.Product` should be the :class:`.OCC.TDF.TDF_Label`

                * We expand the :class:`.Product` in a recursive call of method


    Else the **product** is a simple shape:

       * We look in the list of **shapes_simples** for his partner

       * We set the attribute **product**.geometry like the index+1 (>0) of the position of his partner in the list of **SimpleShape**

    """
    if shape_tool.IsAssembly(product.label_reference):
        l_c = TDF_LabelSequence()
        shape_tool.GetComponents(product.label_reference, l_c)
        for i in range(l_c.Length()):
            label = l_c.Value(i + 1)
            if shape_tool.IsReference(label):
                label_reference = TDF_Label()
                shape_tool.GetReferredShape(label, label_reference)
                reference_found = False
                matrix = TransformationMatrix(
                    get_matrix(shape_tool.GetLocation(label).Transformation()))
                shape = shape_tool.GetShape(label_reference)
                for link in product.links:
                    if shape_tool.GetShape(
                            link.product.label_reference).IsPartner(shape):
                        link.add_occurrence(get_label_name(label), matrix)
                        reference_found = True
                        break

                if not reference_found:
                    new_product = Product(get_label_name(label_reference),
                                          deep, label_reference, doc_id,
                                          product_id[0], file_path)
                    product_assembly = search_assembly(new_product,
                                                       product_root)
                    if product_assembly:
                        product.links.append(Link(product_assembly))
                    else:
                        product.links.append(Link(new_product))
                        product_id[0] += 1
                        expand_product(shape_tool, new_product, shapes_simples,
                                       deep + 1, doc_id, product_root,
                                       product_id, file_path)

                    product.links[-1].add_occurrence(get_label_name(label),
                                                     matrix)
    else:
        compShape = shape_tool.GetShape(product.label_reference)
        for index in range(len(shapes_simples)):
            if compShape.IsPartner(shapes_simples[index].shape):
                product.set_geometry(index + 1)  #to avoid index==0