#!/usr/bin/python # -*- coding: utf-8 -*- from django.core.management.base import BaseCommand, CommandError from catalog.models import Polygon import json, unicodedata from osgeo import ogr import math class Command(BaseCommand): def handle(self, *args, **options): pList = Polygon.objects.all() for p in pList: j = json.loads(p.json_info) coordinates = j['geometry']['coordinates'] areaMax = -1 polyMax = None polyPointListMax = None for c in coordinates: poly = ogr.Geometry(ogr.wkbPolygon) ring = ogr.Geometry(ogr.wkbLinearRing) delta = int(math.ceil(len(c[0])/75)) points = c[0] if delta>0: pointsTmp = list() for i in range (0, len(points), delta): tmpList = c[0][i:i+delta] x = sum([ pp[0] for pp in tmpList ] )/ len(tmpList) y = sum([ pp[1] for pp in tmpList ] )/ len(tmpList) pointsTmp.append([x, y]) points = pointsTmp for point in points: ring.AddPoint(point[0], point[1]) poly.AddGeometry(ring) if areaMax<poly.Area(): areaMax = poly.Area() polyMax = poly polyPointListMax = points stringlist = "" for pi in range( len(polyPointListMax) ): x = str(polyPointListMax[pi][0]) y = str(polyPointListMax[pi][1]) stringlist+=x+" "+y+"," x = str(polyPointListMax[0][0]) y = str(polyPointListMax[0][1]) stringlist+=x+" "+y wkt = "POLYGON(("+ stringlist +"))" p.wkt_polygon = wkt p.save() # print(wkt) # print ("\n\r\n\r")