1

I am trying to find MBR area, height, and width for polygon in QGIS. I have tried the below code. coordinate-system is geographic. As per my research for the correct value, I need some QgsPointXY but I do not have a clear idea of how to use them.

 Input 
for block in active_layer.getFeatures():
    geom = block.geometry()
    SMBR_geom, SMBR_area, SMBR_angle, SMBR_width, SMBR_height = geom.orientedMinimumBoundingBox()                
    mbr_area = SMBR_area               
    mbr_length = SMBR_height            
    mbr_width = SMBR_width
    print(f"mbr_area:",mbr_area)
    print(f"mbr_length:",mbr_length)
    print(f"mbr_width:",mbr_width)
Output:
mbr_area: 9.001931762626732e-07
mbr_length: 0.0013687640563659897
mbr_width: 0.0006576686259958109

Actual Correct Value mbr_area: 6878.7192332149 mbr_length: 98.8647855876 mbr_width: 71.6822574966

But this is not correct. What Python code do I need to use for calculating MBR values?

Vince
  • 20,017
  • 15
  • 45
  • 64
  • 1
    Hello, @Matt Thanks for the correction and I apologize for my question formate as I am in the initial level in QGIS, I updated my question as per your suggestion. – Hetal Solanki Apr 08 '22 at 10:29
  • Which CRS is that ? – Taras Apr 08 '22 at 10:36
  • It would appear the problem is that you are calculating the distances and area in the geographic coordinate system. You will need to project (or transform) your geometry to the appropriate UTM zone, or better yet, a national or regional grid system. – Matt Apr 08 '22 at 10:42

1 Answers1

1

You can transform your geometry before calculating the distances and area:

lyr = iface.activeLayer()

for block in lyr.getFeatures(): # geom has WGS84 Geographic CRS geom = block.geometry() SMBR_geom, SMBR_area, SMBR_angle, SMBR_width, SMBR_height = geom.orientedMinimumBoundingBox()

print(f"mbr_area:", SMBR_area)
print(f"mbr_length:", SMBR_height)
print(f"mbr_width:", SMBR_width)

# Output
# mbr_area: 0.0007064714941573944
# mbr_length: 0.028338646544007418
# mbr_width: 0.02492961310132813

# From here the geometry will be re-projected (transformed) to a UTM projection

# define the source and destination CRSs. You will need to find which EPSG code is appropriate for your study area
sourceCrs = lyr.crs()  # QgsCoordinateReferenceSystem(4326)
destCrs = QgsCoordinateReferenceSystem(32735)    # change this for an EPSG appropriate for your study area

# create a `QgsCoordinateTransform` instance
tr = QgsCoordinateTransform(sourceCrs, destCrs, QgsProject.instance())

# make a copy of the geometry so not to modify the original (not strictly necessary but good to note that the transformation is 'in-place')
geom_copy = QgsGeometry(geom)

# transform the copied geometry to the projected CRS by passing the `QgsCoordinateTransform` instance to the `transform` method of the geometry
geom_copy.transform(tr)

# get the Minimum Bounding Box attributes of the transformed geometry
SMBR_geom, SMBR_area, SMBR_angle, SMBR_width, SMBR_height = geom_copy.orientedMinimumBoundingBox()

print(f"mbr_area:", SMBR_area)
print(f"mbr_length:", SMBR_height)
print(f"mbr_width:", SMBR_width)

# Output
# mbr_area: 7944650.412031875
# mbr_length: 3135.254646155983
# mbr_width: 2533.9729331946

Refer to this post for a lengthier explanation.

Matt
  • 16,843
  • 3
  • 21
  • 52