Dimitris Kougioumtzis Blog About technology

Convert polygon string into polygon object

Category Django

Posted on Oct. 8, 2017



Convert polygon string into polygon 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
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

About

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

Elsewhere