0

Currently dealing with two errors while programming the code shown below, I was wondering if anybody could help me or give some tips to solve them, thank you.

1) When trying to assign multiple LineString objects to a MultiLineString object I encountered difficulties. The idea is to create a name depending on the feeder (fdr) and use it as a variable in the MultiLineStringobject. Different methods were tried (e.g. globals()[], list comprehension,..), but generate errors or like in the code below gives an AssertionError. The problem is that namen produces string objects 'l_6_1', 'l_6_2', .. while variables l_6_1, l_6_2, .. are required. (SOLVED)

2) My second question is related to calculating lengths, where the gdf are read with espg:4326, I applied a conversion in the class object (which changes the LineString object! --this has been checked), meanwhile the result from print(lengte) isn't affected.. Any ideas where my mistakes are?

class Lines:
    def __init__(self, fdr, node, gdf):

        j= 1
        lines= {}
        gdf= gdf.to_crs({'init': 'epsg:3857'})

        for i, k in zip(node[0::2], node[1::2]):
            name= 'l_'+str(fdr)+'_'+str(j)
            lines[name]= LineString([gdf.loc[i][['X', 'Y']], gdf.loc[k][['X', 'Y']]])
            lengte= lines[name].length 
            print(lengte)
            j= j+1

        lines= pd.Series(lines)
        s= lines.values

        f= geometry.MultiLineString([l_6_0]+ list(s))
        self.f = ops.linemerge(f)

Further I append this to a map and save (& export) since I work with Spyder.

x= Lines(6, nodes_6, gdf6)  
folium.GeoJson(x.f).add_to(m)
m.save(os.path.join(dest, "grid.html"))
rclee
  • 85
  • 9
  • Is `l_6_0` a LineString ? In that cases the items in your namen list should be LineStrings as well .. So maybe if you did something like : `f = geometry.MultiLineString([l_6_0] + lines.values())` – Luis Blanche Jan 13 '20 at 15:13
  • @LuisBlanche Indeed it is a `LineString` and I did try this as well, but gave me a `TypeError: 'numpy.ndarray' object is not callable`. Maybe important to note: when using `lines['l_6_1'], lines['l_6_2'], ..., lines['l_6_X']` instead of `namen`, no error occurs and the map displays all the lines. – rclee Jan 13 '20 at 15:22
  • My mistake : try `f = geometry.MultiLineString([l_6_0] + list(lines.values())` then – Luis Blanche Jan 13 '20 at 15:28

1 Answers1

0

@LuisBlanche while checking for the properties of values() I noticed this was applicable for dataframes and not dictionaries. The answer for the first problem is solved by applying s= lines.values and further slice it as a list into the MultiLineString.

class Lines:
    def __init__(self, fdr, node, gdf):

        j= 1
        lines= {}
        gdf= gdf.to_crs({'init': 'epsg:3857'})
        for i, k in zip(node[0::2], node[1::2]):
            name= 'l_'+str(fdr)+'_'+str(j)
            lines[name]= LineString([gdf.loc[i][['X', 'Y']], gdf.loc[k][['X', 'Y']]])
            lengte= lines[name].length 
            print(lengte)
            j= j+1

        lines= pd.Series(lines)
        s= lines.values

        f= geometry.MultiLineString([l_6_0]+ list(s))
        self.f = ops.linemerge(f)
rclee
  • 85
  • 9