Skip to main content
added 1799 characters in body
Source Link
alaybourn
  • 440
  • 4
  • 11

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)

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, 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 in.

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. I 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)
edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

How to automatically move a Moving polygon feature to be centered on a given point using pyQGIS?PyQGIS

I am trying to automate a process with inputs of:

  1. Multipart polygon geometry
  2. Four points - which are the actual center points of the four small rectangles within the larger polygon

How can I move the polygon to align with the four points using pyQGISPyQGIS? 

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, 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 in.

polygon to overlay on four centerpoints

How to automatically move a polygon feature to be centered on a given point using pyQGIS?

I am trying to automate a process with inputs of:

  1. Multipart polygon geometry
  2. Four points - which are the actual center points of the four small rectangles within the larger polygon

How can I move the polygon to align with the four points using pyQGIS? 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, 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 in.

polygon to overlay on four centerpoints

Moving polygon feature to be centered on given point using PyQGIS

I am trying to automate a process with inputs of:

  1. Multipart polygon geometry
  2. Four points - which are the actual center points of the four small rectangles within the larger polygon

How can I move the polygon to align with the four points using PyQGIS? 

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, 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 in.

polygon to overlay on four centerpoints

Source Link
alaybourn
  • 440
  • 4
  • 11

How to automatically move a polygon feature to be centered on a given point using pyQGIS?

I am trying to automate a process with inputs of:

  1. Multipart polygon geometry
  2. Four points - which are the actual center points of the four small rectangles within the larger polygon

How can I move the polygon to align with the four points using pyQGIS? 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, 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 in.

polygon to overlay on four centerpoints