Mario WKT simplificado

parent 280581a2
No preview for this file type
No preview for this file type
No preview for this file type
#!/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")
......@@ -34,4 +34,4 @@ class Command(BaseCommand):
# print ("_____________________________________________")
def strip_accents(self, s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
\ No newline at end of file
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2018-05-26 19:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('catalog', '0004_auto_20180522_0751'),
]
operations = [
migrations.AddField(
model_name='polygon',
name='wkt_polygon',
field=models.TextField(default=''),
),
]
......@@ -12,4 +12,5 @@ class Platform(models.Model):
class Polygon(models.Model):
name = models.CharField(verbose_name="Polygon Name", max_length=50)
json_info = models.TextField(verbose_name="JSON Info", default='')
\ No newline at end of file
json_info = models.TextField(verbose_name="JSON Info", default='')
wkt_polygon = models.TextField(default='')
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment