Django

Django

Explore django code snippets and tutorials

Django

Convert linestring string into linestring object

Convert linestring string into linestring object

python
 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
Django

Convert polygon string into polygon object

Convert polygon string into polygon object

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django.contrib.gis.geos import GEOSGeometry, GEOSException


def convert_polygon(data):
    '''
     Get the polygon from the  array
     Convert the polygon in Polygon object
    '''

    polygon_raw_string = ''
    for i in data:
        if i in polygon_raw_string:
            pass
        else:
            polygon_raw_string += i
            polygon_raw_string += ' , '

    polygon_raw_string += data[0]
    wkt = "POLYGON(("+polygon_raw_string+"))"
    try:
        polygon = GEOSGeometry(wkt)
        polygon.srid = 4326
    except GEOSException:
        polygon = None
    return polygon