Dimitris Kougioumtzis Blog About technology

Convert linestring string into linestring object

Category Django

Posted on Oct. 8, 2017



Convert linestring string into linestring object


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

About

My name is Dimitris Kougioumtzis and i work as a Web developer at Rapidbounce Company

Elsewhere