Convert linestring string into linestring object

Django -- Posted on Oct. 8, 2017

Convert linestring string into linestring object

              
                from django.contrib.gis.geos import GEOSGeometry, GEOSException

def convert_linestring(data):
    '''
     Get the route from the  array
     Convert the route in LineString object
    '''

    linestring_raw = ''
    k = 0
    for i in data:
        k += 1
        if i in linestring_raw:
            pass
        else:
            linestring_raw += i
            linestring_raw += ','
    wkt = "LineString({})".format(linestring_raw).replace(',)', ')')
    try:
        linestring = GEOSGeometry(wkt)
        linestring.srid = 4326
    except GEOSException:
        linestring = None
    return linestring
                  
   
            

Related Posts