My first attempt was to start with the center points and construct the polygons around them, calculating the orientation of the points and rotating the polygons accordingly (see below), but I'll be receiving different polygons (with varying complexity) and hard-coding the dimensions will be too cumbersome. My next thought is to calculate centroids of those inner rectangles, but then I don't know how to snap them to the actual center points. This is where you all come inI don't necessarily need the code, but a general approach would be helpful.
layer=my_layer
if not layer:
print ("Layer failed to load!")
#loop through features
points = layer.getFeatures()
metrics={}
d=QgsDistanceArea()
for point in points:
#calculate pairwise distances and bearings
compares = layer.getFeatures()
for compare in compares:
geom1=point.geometry().asPoint()
geom2=compare.geometry().asPoint()
dist=d.measureLine(geom1,geom2)
bear=d.bearing(geom1,geom2)
metrics.update({dist:bear})
break
#sort dictionary and extract third value, which is the bearing of vessel in radians
i=1
rad=[]
for j in sorted (metrics):
# print ((j, metrics[j]), end =" ")
if i == 3:
rad.append(metrics[j])
i+=1
features = layer.getFeatures()
for feature in features:
# retrieve every feature with its geometry and attributes
# fetch geometry
geom = feature.geometry()
geomSingleType = QgsWkbTypes.isSingleType(geom.wkbType())
if geom.type() == QgsWkbTypes.PointGeometry:
ptx=geom.asPoint().x()
pty=geom.asPoint().y()
pol = QgsGeometry.fromPolygonXY([[QgsPointXY(ptx+6.625,pty+3.225),QgsPointXY(ptx+5.825,pty+4.025),QgsPointXY(ptx-5.825,pty+4.025),QgsPointXY(ptx-6.625,pty+3.225),QgsPointXY(ptx-6.625,pty-3.225),QgsPointXY(ptx-5.825,pty-4.025),QgsPointXY(ptx+5.825,pty-4.025),QgsPointXY(ptx+6.625,pty-3.225)]])
centroid = pol.centroid().asPoint()
pol.rotate(rad[0]*57.295779513-90, centroid)
#add geometry to layer
vl = QgsVectorLayer("Polygon?crs=epsg:6348", "temp", "memory")
pr=vl.dataProvider()
f = QgsFeature()
f.setGeometry(pol)
pr.addFeature(f)
vl.updateExtents()
QgsProject.instance().addMapLayer(vl)