10

I try to make plugin in QGIS that compare 2 bounding box data, ex:

(-32.61442167739459, -32.017601026186576, 13.015663782506067, 13.428145228091406)

and

(-38.6102467739459, -38.017601026186576, 33.01563382506067, 33.624945228091406)

and want to picture it in a square or polygon like shape.

Does QGIS have a module in Plugin that can do such a thing?

2
  • You are wanting to create the two polygon features, then add them to the map?
    – artwork21
    Mar 11, 2014 at 22:47
  • No... i mean i just want to make a object like a square from MINx, MAXx, MINy, MAXy and compare the size of 2 square if its match or not, maybe like a simulation Mar 11, 2014 at 22:54

3 Answers 3

11

See https://shapely.readthedocs.io/en/latest/manual.html#shapely.geometry.box

>>> import shapely.geometry
>>> bbox = (-125.056143, 31.8679566, -112.9017855, 42.4407224)
>>> polygon = shapely.geometry.box(*bbox, ccw=True)
>>> polygon
<shapely.geometry.polygon.Polygon object at 0x7fb070a9bf60>
>>> polygon.bounds
(-125.056143, 31.8679566, -112.9017855, 42.4407224)
>>> polygon.wkt
'POLYGON ((-112.9017855 31.8679566, -112.9017855 42.4407224, -125.056143 42.4407224, -125.056143 31.8679566, -112.9017855 31.8679566))'

For the example data given:

>>> bbox1 = (-32.61442167739459, -32.017601026186576, 13.015663782506067, 13.428145228091406)
>>> bbox2 = (-38.6102467739459, -38.017601026186576, 33.01563382506067, 33.624945228091406)
>>> polygon1 = shapely.geometry.box(*bbox1, ccw=True)
>>> polygon2 = shapely.geometry.box(*bbox2, ccw=True)
>>> polygon1.bounds
(-32.61442167739459, -32.017601026186576, 13.015663782506067, 13.428145228091406)
>>> polygon2.bounds
(-38.6102467739459, -38.017601026186576, 33.01563382506067, 33.624945228091406)
>>> polygon1.wkt
'POLYGON ((13.01566378250607 -32.01760102618658, 13.01566378250607 13.42814522809141, -32.61442167739459 13.42814522809141, -32.61442167739459 -32.01760102618658, 13.01566378250607 -32.01760102618658))'
>>> polygon2.wkt
'POLYGON ((33.01563382506067 -38.01760102618658, 33.01563382506067 33.62494522809141, -38.6102467739459 33.62494522809141, -38.6102467739459 -38.01760102618658, 33.01563382506067 -38.01760102618658))'
7

Yes, it's the QuickWKT plugin (WKT = "Well-Known Text"). The bounding box would be treated as a polygon, and WKT would look like:

'POLYGON(x1 y1, x1 y2, x2 y2, x2 y1, x1 y1)'

Note that any polygon shape is possible, but coordinates following this model will generate a rectangle, which is what you want for a bounding box. You can't get away specifying only two corners, as you have in the question, you have to specify all four corners, and the first and last coordinates have to be the same. That's how WKT indicates that the polygon is closed.

A screenshot with the WKT for your first example:

QuickWKT with Well-Known Text for a rectangle in WGS84.

1
  • 1
    Additionally: If you want to display your bounding box in projects with a different CRS, make sure to densify your polygon with Vector -> Geometry Tools -> Densify. Otherwise you will not get curved borders when they should occur.
    – AndreJ
    Mar 12, 2014 at 6:33
0

You can also use PostGIS through DB Manager in QGIS, by using the ST_MakeEnvelope function (https://postgis.net/docs/ST_MakeEnvelope.html). An example:

SELECT ST_SetSRID(ST_MakeEnvelope(36.104095,-6.145738,36.122635,-6.137925), 4326) as polygon_1;

And load your polygon geometry output as a layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.