Convert polygon string into polygon object

Django -- Posted on Oct. 8, 2017

Convert polygon string into polygon object

              
                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
                  
   
            

Related Posts