I'm trying to label the polygon boundaries instead of an anchor point. Not sure if this is possible. For this example I wish to label these numbers in the top left corner of every polygon.
I know you're looking for QGIS answer, but I think you could tailor the method below to suit your needs.
- Loop through the vertices in each polygon
- Assign a score for each vertex, considering the distance from the polygon centroid to the vertex, and the angle from the centroid to the vertex. You can weight the components if you want to consider distance or angle more heavily (e.g. below, I only weight distance by half). My method below calculates a score by giving positive points for distance and subtracting a penalty for being far away from 45 degrees (NW). I think it's correct, but you should check the math.
- Write the highest scoring points to a new feature class
- Use those points to label
fc = 'Parcels' sr = arcpy.Describe(fc).spatialReference points =  with arcpy.da.SearchCursor(fc,'SHAPE@',spatial_reference=sr) as cursor: for row in cursor: score = 0 for part in row: for pnt in part: pnt_geom = arcpy.PointGeometry(pnt) dx = row.centroid.X - pnt.X dy = row.centroid.Y - pnt.Y ang = math.degrees(math.atan2(dy,dx)) cur_score = (pnt_geom.distanceTo(row.centroid)*0.5) - (pnt_geom.distanceTo(row.centroid)*0.5 * abs(ang+45)/180) if cur_score > score: score = cur_score point = pnt_geom points.append(point) arcpy.CopyFeatures_management(points,r'in_memory\points')
Sometimes can have just one point, but sometimes we need compromise. My idea will work in some cases and not in some.
So the idea is to export all the nodes into csv. vector-geometry tools-extract nodes. save into shape. save as csv.
In excel you need to find 2 points in each group of points (one group of points is one polygon - use ID for separation), the point with the smallest X and the point with the smallest Y. If both the same point than you good, if not I would go with the mid point . At the end you will have a point csv with all the mid or top/left points. Load back the point into qgis creating points and activate the label. https://stackoverflow.com/questions/17114443/excel-how-to-get-min-value-in-a-group