4

I need a quick way to create rectangles with set with & height in meters. Is there a straightforward way to do this? I am well aware that there are a number of ways to achieve this, but all that I have found (apart from the QAD plugin) are cumbersome workarounds. The keyword here is quick and easy.

Please note that the size of the rectangle is the priority. It will be moved around a bit until we decide on its final coordinates. So I would rather avoid solutions to my problem where the rectangle is drawn from coordinates.

Ideally I would like to click anywhere, enter height and with in meters and then move the polygon to its final resting place. I have used the QAD plugins rectangle tool to do this in the past but it seems to be currently broken.

  • If you create the polygon at one place and shift it, then height and width will change (except on same lat values). If you want a quick way to convert points to rectangle, provided your points are final, you can use CSV2Polygon utility in browser which uses js2shape.is from geospatialearth.in/Point2Poly – GeoSpatialEarth.in May 12 '17 at 13:49
3

Based heavily on this great answer by @xunilk, you can create a custom script which allows you select a feature from a point layer and insert the height and width parameters. You can create a script by going to:

Processing Toolbox > Scripts > Tools > Create new script

Then use something like:

##Example=name
##Point_layer=vector point
##Height=number 1
##Width=number 1

from qgis.core import QgsVectorLayer, QgsFeature, QgsPoint, QgsRectangle, QgsGeometry, QgsMapLayerRegistry

point_layer = processing.getObject(Point_layer)

feats = [ feat for feat in point_layer.selectedFeatures() ]
epsg = point_layer.crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer&field=x:real&field=y:real&field=point_id:integer""&index=yes"
mem_layer = QgsVectorLayer(uri, 'rectangular_buffer', 'memory')
prov = mem_layer.dataProvider()

for i, feat in enumerate(feats):
    point = feat.geometry().asPoint()
    new_feat = QgsFeature()
    new_feat.setAttributes([i, point[0], point[1], feat.id()])
    bbox = feat.geometry().buffer((Width/2), -1).boundingBox()
    tmp_feat = bbox.asWktPolygon()
    xmin1,ymin1,xmax1,ymax1 = bbox.toRectF().getCoords()
    xmin2,ymin2,xmax2,ymax2 = feat.geometry().buffer((Height/2), -1).boundingBox().toRectF().getCoords()
    p1 = QgsPoint(xmin1, ymax2)
    p2 = QgsPoint(xmax1, ymin2)
    new_ext = QgsRectangle(p1,p2)
    new_tmp_feat = new_ext.asWktPolygon()
    new_feat.setGeometry(QgsGeometry.fromWkt(new_tmp_feat))
    prov.addFeatures([new_feat])

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

Make sure to save it in your /.qgis2/processing/scripts/ directory.



Example:

  1. A simple point which has been added to the area of interest and selected. Then executing the custom script from the Processing Toolbox:

    Point layer


  1. A rectangular buffer is created around the point based on the height and width parameters:

    Result of script


  1. A quick check showing the perimeter (300 = (100 x 2) + (50 x 2)):

    Check result

    You can also use the Measuring Tool to do a quick check for the height and width.


  1. Then copy the rectangle polygon to your main polygon layer.
  • 1
    Thanks! That looks reasonably swift. I will definitely try this out! – Aron May 12 '17 at 15:06
  • @Aron - Most welcome, hope it works for you :) – Joseph May 12 '17 at 15:08

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.