# QGIS calculate corners and create polygon

I would like to create a polygon in QGIS. I have photos with four points (`X`) I can see, which are not supposed to be the corners of the polygon, but are needed to measure these corners (`Y`).

The setup looks like this:

``````X   Y     Y   X

X   Y     Y   X
``````

What I know is where the four points `X` are, and that when I connect the two upper `X`, on this line, at 1/6 and 5/6 are the upper points `Y` (the same applies for the lower points). I would like to load the photo into QGIS, tell it where the four points X are, and get a polygon with four corners at points `Y`.

I have hundreds of these photos, so a semi-automatic or quick way would help a lot.

• Hi Felix, welcome to GisSE! Are these photos already georeferenced? Would it be possible to crop images before importing them into QGIS? Commented Nov 21, 2019 at 15:26

I just write the code below to create a new memory vector layer (Polygon type) to contain your "photo polygons".

The remaining part is to loop over the photos (one layer for one photo ? have a list of all photo coordinates ? it's a point to clarify for more), I indicated in the code the different places for the loop.

I also considered that the top-left x-coordinate equals the top-right x-coordinate.

``````#!/usr/bin/env python3

from qgis.PyQt.QtCore import QVariant

vl = QgsVectorLayer("Polygon", "photo_polygons", "memory")
pr = vl.dataProvider()
vl.updateFields()

# for coordinates in photo_layer_or_list:
topleft_photo_x = 0
topleft_photo_y = 1
bottomright_photo_x = 1
bottomright_photo_y = 0

new_tl_x = (topleft_photo_x + bottomright_photo_x) * 1 / 6
new_tl_y = (topleft_photo_y + bottomright_photo_y) * 1 / 6
new_br_x = (topleft_photo_x + bottomright_photo_x) * 5 / 6
new_br_y = (topleft_photo_y + bottomright_photo_y) * 5 / 6

feat = QgsFeature()

topleft_corner = QgsPointXY(new_tl_x, new_tl_y)
topright_corner = QgsPointXY(new_br_x, new_tl_y)
bottomright_corner = QgsPointXY(new_br_x, new_br_y)
bottomleft_corner = QgsPointXY(new_tl_x, new_br_y)

geom = QgsGeometry.fromPolygonXY([[
topleft_corner,
topright_corner,
bottomright_corner,
bottomleft_corner,
topleft_corner  # for closing polygon
]])
feat.setGeometry(geom)
feat.setAttributes(["photo 1"])