miércoles, 25 de julio de 2018

REJILLA EXAGONAL


Abrimos la consola pyton y escribimos el código siguiente y ejecutamos.


import numpy as np

bufferLength = 1000
polygonSides = 6

inc_x = 2 * bufferLength * np.cos(np.pi*30/180)
inc_y = inc_x * np.cos(np.pi*30/180)

p1 = QgsPoint(354972.451507, 4473426.04508)

points = []

x = p1.x() + inc_x/2
y = p1.y()

rows = 8
cols = 10
   
for i in range(rows):
    for j in range(cols):
        point = QgsPoint(x, y)
        points.append(point)
        x += inc_x
    y -= inc_y
    ver = i%2

    if ver ==0:
        h = 0
    else:
        h = inc_x/2
    x = p1.x() + h

epsg = 32612

uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,
                           'buffer',
                           'memory')

prov = mem_layer.dataProvider()

for i, point in enumerate(points):
    outFeat = QgsFeature()

    outFeat.setGeometry(QgsGeometry.fromPolygon([[ QgsPoint(point[0] + np.sin(angle)*bufferLength, point[1] + np.cos(angle)*bufferLength)
                        for angle in np.linspace(0, 2*np.pi, polygonSides + 1, endpoint = True) ]]))
   
    outFeat.setAttributes([i])
    prov.addFeatures([outFeat])

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

No hay comentarios:

Publicar un comentario